Google Sheets
GoogleSheetsTools enable an Agent to interact with Google Sheets API for reading, creating, updating, and duplicating spreadsheets.
Prerequisites
You need to install the required Google API client libraries:
1uv pip install google-api-python-client google-auth-httplib2 google-auth-oauthlibSet up the following environment variables:
1export GOOGLE_CLIENT_ID=your_client_id_here2export GOOGLE_CLIENT_SECRET=your_client_secret_here3export GOOGLE_PROJECT_ID=your_project_id_here4export GOOGLE_REDIRECT_URI=your_redirect_uri_hereHow to Get Credentials
-
Go to Google Cloud Console (https://console.cloud.google.com)
-
Create a new project or select an existing one
-
Enable the Google Sheets API:
- Go to "APIs & Services" > "Enable APIs and Services"
- Search for "Google Sheets API"
- Click "Enable"
-
Create OAuth 2.0 credentials:
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth client ID"
- Go through the OAuth consent screen setup
- Give it a name and click "Create"
- You'll receive:
- Client ID (GOOGLE_CLIENT_ID)
- Client Secret (GOOGLE_CLIENT_SECRET)
- The Project ID (GOOGLE_PROJECT_ID) is visible in the project dropdown at the top of the page
Example
The following agent will use Google Sheets to read and update spreadsheet data.
1from kern.agent import Agent2from kern.tools.googlesheets import GoogleSheetsTools34SAMPLE_SPREADSHEET_ID = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"5SAMPLE_RANGE_NAME = "Class Data!A2:E"67google_sheets_tools = GoogleSheetsTools(8 spreadsheet_id=SAMPLE_SPREADSHEET_ID,9 spreadsheet_range=SAMPLE_RANGE_NAME,10)1112agent = Agent(13 tools=[google_sheets_tools],14 instructions=[15 "You help users interact with Google Sheets using tools that use the Google Sheets API",16 "Before asking for spreadsheet details, first attempt the operation as the user may have already configured the ID and range in the constructor",17 ],18)19agent.print_response("Please tell me about the contents of the spreadsheet")Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
scopes | Optional[List[str]] | None | Custom OAuth scopes. If None, uses write scope by default. |
spreadsheet_id | Optional[str] | None | ID of the target spreadsheet. |
spreadsheet_range | Optional[str] | None | Range within the spreadsheet. |
creds | Optional[Credentials] | None | Pre-existing credentials. |
creds_path | Optional[str] | None | Path to credentials file. |
token_path | Optional[str] | None | Path to token file. |
oauth_port | int | 0 | Port to use for OAuth authentication. |
enable_read_sheet | bool | True | Enable reading from a sheet. |
enable_create_sheet | bool | False | Enable creating a sheet. |
enable_update_sheet | bool | False | Enable updating a sheet. |
enable_create_duplicate_sheet | bool | False | Enable creating a duplicate sheet. |
all | bool | False | Enable all tools. |
Toolkit Functions
| Function | Description |
|---|---|
read_sheet | Read values from a Google Sheet. Parameters include spreadsheet_id (Optional[str]) for fallback spreadsheet ID and spreadsheet_range (Optional[str]) for fallback range. Returns JSON of list of rows. |
create_sheet | Create a new Google Sheet. Parameters include title (str) for the title of the Google Sheet. Returns the ID of the created Google Sheet. |
update_sheet | Update data in a Google Sheet. Parameters include data (List[List[Any]]) for the data to update, spreadsheet_id (Optional[str]) for the ID of the Google Sheet, and range_name (Optional[str]) for the range to update. Returns success or failure message. |
create_duplicate_sheet | Create a duplicate of an existing Google Sheet. Parameters include source_id (str) for the ID of the source spreadsheet, new_title (Optional[str]) for new title, and copy_permissions (bool, default=True) for whether to copy permissions. Returns link to duplicated spreadsheet. |
Developer Resources
- View Tools