π¦ ClawHub
gspread-sheets
by @nuokunkeji
Batch read/write Google Sheets using the gspread Python library with service account authentication. Use when the user needs to: (1) read/write/update/clear...
π‘ Examples
Initialize Client
import gspread
from google.oauth2.service_account import CredentialsSCOPES = ['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']
Option 1: From JSON file path
creds = Credentials.from_service_account_file('key.json', scopes=SCOPES)
gc = gspread.authorize(creds)Option 2: From JSON string (e.g. env var)
import json, os
creds_info = json.loads(os.environ['GOOGLE_SERVICE_ACCOUNT_JSON'])
creds = Credentials.from_service_account_info(creds_info, scopes=SCOPES)
gc = gspread.authorize(creds)
Open Spreadsheet
# By name
sh = gc.open("My Spreadsheet")By URL
sh = gc.open_by_url("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit")By key
sh = gc.open_by_key("SPREADSHEET_ID")
βοΈ Configuration
pip install gspread google-auth
Service account JSON key file required. Set path via:
export GOOGLE_SERVICE_ACCOUNT_JSON=/path/to/key.jsonShare the target spreadsheet with the service account email (found in the JSON key file).
π Tips & Best Practices
batch_update over multiple update calls β fewer API requestsvalue_input_option="USER_ENTERED" parses formulas and number formatsvalue_input_option="RAW" writes literal stringsinclude_empty=False in get_all_values omits trailing empty cellsgc.open_by_key() (more reliable than name)TERMINAL
clawhub install gspread-sheets