Google Spreadsheet
Google Spreadsheet is a wrapper that allows you to use the Google Sheets API
- JavaScript
- Python
Credential configuration
To configure this credential you need to:
-
Enable the Google Sheets API for your project in Google Cloud.
-
Obtain the spreadsheet ID: You can find it in the URL
https://docs.google.com/spreadsheets/d/----SPREADSHEET-ID----/edit
-
Obtain the JSON key of a service account. This key is downloaded as a JSON file when you create it. It should look like this:
{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL"
}
If you don't have one, create a service account and then, a JSON key for that service account. You need to share the spreadsheet with the service account to ensure proper functionality.
Here is an example of a filled credential configuration form in YepCode:
Google Spreadsheet Snippets available in Editor
The title is the triggering text for YepCode to autocomplete the script.
- JavaScript
- Python
Integration
const googleSpreadsheetClient = await yepcode.integration.googleSpreadsheet("credential-slug");
const { GoogleSpreadsheet } = require("google-spreadsheet");
const googleSpreadsheetServiceAccountJson = {
type: "service_account",
project_id: "yepcode",
private_key_id: "XXXXX",
private_key: \`-----BEGIN PRIVATE KEY-----\nx\n-----END PRIVATE KEY-----\`,
client_email: "yepcode@example.org",
client_id: "1234567890",
auth_uri: "https://example.org",
token_uri: "https://example.org",
auth_provider_x509_cert_url: "https://example.org",
client_x509_cert_url: "https://example.org",
};
const googleSpreadsheetClient = new GoogleSpreadsheet("spreadsheet-id");
await googleSpreadsheetClient.useServiceAccountAuth(googleSpreadsheetServiceAccountJson);
Load Document Properties and Worksheets
await googleSpreadsheetClient.loadInfo();
console.log(googleSpreadsheetClient.title);
const sheet = googleSpreadsheetClient.sheetsByTitle["sheetTitle"];
console.log(sheet.title);
console.log(sheet.rowCount);
Create New Sheet and Add Rows
await googleSpreadsheetClient.loadInfo();
const sheet = await googleSpreadsheetClient.addSheet({ title: "sheetTitle", headerValues: ["columnName", "columnName"] });
await sheet.addRow({ "columnName": "value", "columnName": "value" });
Update and Remove Rows
await googleSpreadsheetClient.loadInfo();
const sheet = googleSpreadsheetClient.sheetsByTitle["sheetTitle"];
const rows = await sheet.getRows();
rows[1].columnName = newValue;
await rows[1].save();
await rows[2].delete();
Work with Cells
await googleSpreadsheetClient.loadInfo();
const sheet = googleSpreadsheetClient.sheetsByTitle["sheetTitle"];
await sheet.loadCells("A1:E10");
console.log(sheet.cellStats); // total cells, loaded, how many non-empty
const a2 = sheet.getCell(1, 0);
const a3 = sheet.getCellByA1("A3");
a2.value = newValue
a3.value = newValue
await sheet.saveUpdatedCells();
We use gspread library to interact with Google Sheets. If you prefer, you can use google-api-python-client using the spreadsheet service as you also have this library available.
Integration
google_spreadsheet = yepcode.integration.googleSpreadsheet("credential-slug")
import gspread
from google.oauth2.service_account import Credentials
scopes = ["https://www.googleapis.com/auth/spreadsheets"]
credentials = {
"type": "service_account",
"project_id": "yepcode",
"private_key_id": "XXXXX",
"private_key": "-----BEGIN PRIVATE KEY-----\nx\n-----END PRIVATE KEY-----",
"client_email": "yepcode@example.org",
"client_id": "1234567890",
"auth_uri": "https://example.org",
"token_uri": "https://example.org",
"auth_provider_x509_cert_url": "https://example.org",
"client_x509_cert_url": "https://example.org"
}
gc = gspread.authorize(
Credentials.from_service_account_info(service_account_info, scopes=scopes)
)
google_spreadsheet = gc.open_by_key('your_sheet_id')
Create New Sheet and Add Rows
sh.add_worksheet(title="A worksheet", rows=100, cols=20)
Get Worksheet
worksheet = google_spreadsheet.worksheet("sheetTitle")
# or
worksheet = google_spreadsheet.get_worksheet(0)
Add Row (append)
worksheet.append_row(["valueColumnA", "valueColumnB", "valueColumnC"])
Add Rows (at start)
worksheet.insert_row(["valueColumnA", "valueColumnB", "valueColumnC"], 1)
Get All Rows
records = worksheet.get_all_records()
for row in records:
print(row)
Update Rows
worksheet.update('A1:B1', [[1, 2]])
worksheet.update('A2:B4', [[1, 2], [3,4]])