Sure! Below is the updated README with the added prerequisite instructions for creating a service account in Google Cloud Platform (GCP) and adding it as an editor to your Google Sheet.
This module provides functions to interact with Google Sheets using the Google Sheets API. It includes the following functionalities:
- Authentication with Google Sheets API
- Reading data from a cell
- Writing data to a cell
- Batch writing data to multiple cells
- Clearing data from specified cell ranges
- Create a service account in Google Cloud Platform (GCP) and download the private key.
- Share your Google Sheet with the service account's email, giving it editor permissions.
- Go to the Google Cloud Console.
- Select your project or create a new one.
- Navigate to the IAM & Admin section and click on Service accounts.
- Click + CREATE SERVICE ACCOUNT.
- Enter the service account name, ID and description, then click CREATE AND CONTINUE.
- Assign the Editor role to this service account and click CONTINUE.
- Click DONE.
- Click the newly created service account to open its settings.
- Navigate to the KEYS tab.
- Click ADD KEY and select Create new key.
- Choose the JSON key type and click Create. This will download the private key file. Store this securely as you will need information from this file to authenticate.
- Open your Google Sheet.
- Click Share in the top-right corner.
- In the "Share with people and groups" window, enter the service account's email address.
- Set the permissions to Editor.
- Click Send.
Install the necessary packages using npm:
npm install googleapis dotenvCreate a .env file in your project root directory and add the following environment variables:
SHEET_ID=your-google-sheet-id
SERVICE_EMAIL=your-service-account-email
EMAIL_PKEY=your-private-key
Create a constants.js file to read your environment variables and store your configuration constants required for Google Sheets API interaction:
// constants.js
require("dotenv").config();
const SHEET_ID = process.env.SHEET_ID;
const SERVICE_EMAIL = process.env.SERVICE_EMAIL;
const EMAIL_PKEY = process.env.EMAIL_PKEY;
const ACCESS_SCOPES = ["https://www.googleapis.com/auth/spreadsheets"];
module.exports = {
SHEET_ID,
SERVICE_EMAIL,
ACCESS_SCOPES,
EMAIL_PKEY,
};Authenticate the Google Sheets client using the provided service account details.
const { authenticateGoogleSheetsClient } = require('./path-to-your-module');
(async () => {
const client = await authenticateGoogleSheetsClient();
// Now you can use the authenticated client
})();Reads data from a specified cell range.
const { readFromCellInSpreadsheet } = require('./path-to-your-module');
(async () => {
const client = await authenticateGoogleSheetsClient();
const response = await readFromCellInSpreadsheet(client, 'your-spreadsheet-id', 'Sheet1!A1');
console.log(response.data);
})();Writes data to a specified cell range.
const { writeToCellInSpreadsheet } = require('./path-to-your-module');
(async () => {
const client = await authenticateGoogleSheetsClient();
const response = await writeToCellInSpreadsheet(client, 'your-spreadsheet-id', 'Sheet1!A1', [['Hello, World!']]);
console.log(response.data);
})();Writes data to multiple specified cell ranges in a batch process.
const { batchWriteToCellInSpreadsheet } = require('./path-to-your-module');
(async () => {
const client = await authenticateGoogleSheetsClient();
const ranges = ['Sheet1!A1', 'Sheet1!B1'];
const values = [['Hello'], ['World']];
const response = await batchWriteToCellInSpreadsheet(client, 'your-spreadsheet-id', ranges, values);
console.log(response);
})();Clears data from the specified cell ranges.
const { clearSheetCells } = require('./path-to-your-module');
(async () => {
const client = await authenticateGoogleSheetsClient();
const response = await clearSheetCells(client, 'your-spreadsheet-id', 'Sheet1!A1:B2');
console.log(response);
})();module.exports = {
authenticateGoogleSheetsClient,
readFromCellInSpreadsheet,
writeToCellInSpreadsheet,
batchWriteToCellInSpreadsheet,
clearSheetCells,
};- Ensure the service account used has access to the Google Sheet you are interacting with.
- The
spreadsheetIdparameter can default to theSHEET_IDspecified in yourconstants.js, or you can provide it explicitly for each function call. - The
EMAIL_PKEYshould be the private key for the service account, typically provided in JSON format, and should be handled securely.