|
| 1 | +--- |
| 2 | +title: Google Sheets |
| 3 | +--- |
| 4 | + |
| 5 | +**GoogleSheetsTools** enable an Agent to interact with Google Sheets API for reading, creating, updating, and duplicating spreadsheets. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +You need to install the required Google API client libraries: |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib |
| 13 | +``` |
| 14 | + |
| 15 | +Set up the following environment variables: |
| 16 | + |
| 17 | +```bash |
| 18 | +export GOOGLE_CLIENT_ID=your_client_id_here |
| 19 | +export GOOGLE_CLIENT_SECRET=your_client_secret_here |
| 20 | +export GOOGLE_PROJECT_ID=your_project_id_here |
| 21 | +export GOOGLE_REDIRECT_URI=your_redirect_uri_here |
| 22 | +``` |
| 23 | + |
| 24 | +## How to Get Credentials |
| 25 | + |
| 26 | +1. Go to Google Cloud Console (https://console.cloud.google.com) |
| 27 | +2. Create a new project or select an existing one |
| 28 | +3. Enable the Google Sheets API: |
| 29 | + - Go to "APIs & Services" > "Enable APIs and Services" |
| 30 | + - Search for "Google Sheets API" |
| 31 | + - Click "Enable" |
| 32 | + |
| 33 | +4. Create OAuth 2.0 credentials: |
| 34 | + - Go to "APIs & Services" > "Credentials" |
| 35 | + - Click "Create Credentials" > "OAuth client ID" |
| 36 | + - Go through the OAuth consent screen setup |
| 37 | + - Give it a name and click "Create" |
| 38 | + - You'll receive: |
| 39 | + * Client ID (GOOGLE_CLIENT_ID) |
| 40 | + * Client Secret (GOOGLE_CLIENT_SECRET) |
| 41 | + - The Project ID (GOOGLE_PROJECT_ID) is visible in the project dropdown at the top of the page |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +The following agent will use Google Sheets to read and update spreadsheet data. |
| 46 | + |
| 47 | +```python cookbook/tools/googlesheets_tools.py |
| 48 | +from agno.agent import Agent |
| 49 | +from agno.tools.googlesheets import GoogleSheetsTools |
| 50 | + |
| 51 | +SAMPLE_SPREADSHEET_ID = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms" |
| 52 | +SAMPLE_RANGE_NAME = "Class Data!A2:E" |
| 53 | + |
| 54 | +google_sheets_tools = GoogleSheetsTools( |
| 55 | + spreadsheet_id=SAMPLE_SPREADSHEET_ID, |
| 56 | + spreadsheet_range=SAMPLE_RANGE_NAME, |
| 57 | +) |
| 58 | + |
| 59 | +agent = Agent( |
| 60 | + tools=[google_sheets_tools], |
| 61 | + instructions=[ |
| 62 | + "You help users interact with Google Sheets using tools that use the Google Sheets API", |
| 63 | + "Before asking for spreadsheet details, first attempt the operation as the user may have already configured the ID and range in the constructor", |
| 64 | + ], |
| 65 | +) |
| 66 | +agent.print_response("Please tell me about the contents of the spreadsheet") |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +## Toolkit Params |
| 71 | + |
| 72 | +| Parameter | Type | Default | Description | |
| 73 | +| ------------------ | --------------- | ------- | --------------------------------------------------------------------------- | |
| 74 | +| `scopes` | `List[str]` | `None` | Custom OAuth scopes. If None, determined by operations. | |
| 75 | +| `spreadsheet_id` | `str` | `None` | ID of the target spreadsheet. | |
| 76 | +| `spreadsheet_range`| `str` | `None` | Range within the spreadsheet. | |
| 77 | +| `creds` | `Credentials` | `None` | Pre-existing credentials. | |
| 78 | +| `creds_path` | `str` | `None` | Path to credentials file. | |
| 79 | +| `token_path` | `str` | `None` | Path to token file. | |
| 80 | +| `read` | `bool` | `True` | Enable read operations. | |
| 81 | +| `create` | `bool` | `False` | Enable create operations. | |
| 82 | +| `update` | `bool` | `False` | Enable update operations. | |
| 83 | +| `duplicate` | `bool` | `False` | Enable duplicate operations. | |
| 84 | + |
| 85 | +## Toolkit Functions |
| 86 | + |
| 87 | +| Function | Description | |
| 88 | +| ----------------------- | ----------------------------------------------- | |
| 89 | +| `read_sheet` | Read values from a Google Sheet | |
| 90 | +| `create_sheet` | Create a new Google Sheet | |
| 91 | +| `update_sheet` | Update data in a Google Sheet | |
| 92 | +| `create_duplicate_sheet`| Create a duplicate of an existing Google Sheet | |
| 93 | + |
| 94 | +## Developer Resources |
| 95 | + |
| 96 | +- View [Tools](https://github.com/agno-agi/agno/blob/main/libs/agno/agno/tools/googlesheets.py) |
| 97 | +- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/tools/googlesheets_tools.py) |
0 commit comments