snippets_py
tip
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
New integration from credential
google_spreadsheet = yepcode.integration.googleSpreadsheet("credential-slug")
New integration from plain authentication data
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
Create new sheet and add rows
sh.add_worksheet(title="A worksheet", rows=100, cols=20)
Get Worksheet
Get worksheet
worksheet = google_spreadsheet.worksheet("sheetTitle")
# or
worksheet = google_spreadsheet.get_worksheet(0)
Add Row (append)
Add row (append)
worksheet.append_row(["valueColumnA", "valueColumnB", "valueColumnC"])
Add Rows (at start)
Add rows (at start)
worksheet.insert_row(["valueColumnA", "valueColumnB", "valueColumnC"], 1)
Get All Rows
Get all rows
records = worksheet.get_all_records()
for row in records:
print(row)
Update Rows
Update rows
worksheet.update('A1:B1', [[1, 2]])
worksheet.update('A2:B4', [[1, 2], [3,4]])