GoogleSheetsTools 允许代理使用 Google Sheets API 进行电子表格的读取、创建、更新和复制操作。
前提条件
需要安装所需的 Google API 客户端库:
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
设置以下环境变量:
export GOOGLE_CLIENT_ID=your_client_id_here
export GOOGLE_CLIENT_SECRET=your_client_secret_here
export GOOGLE_PROJECT_ID=your_project_id_here
export GOOGLE_REDIRECT_URI=your_redirect_uri_here
如何获取凭据
-
前往 Google Cloud Console (https://console.cloud.google.com)
-
创建新项目或选择现有项目
-
启用 Google Sheets API:
- 前往“APIs & Services” > “Enable APIs and Services”
- 搜索“Google Sheets API”
- 点击“Enable”
-
创建 OAuth 2.0 凭据:
- 前往“APIs & Services” > “Credentials”
- 点击“Create Credentials” > “OAuth client ID”
- 完成 OAuth 同意屏幕设置
- 给予名称并点击“Create”
- 您将获得:
- Client ID (GOOGLE_CLIENT_ID)
- Client Secret (GOOGLE_CLIENT_SECRET)
- 项目 ID (GOOGLE_PROJECT_ID) 可在页面顶部的项目下拉菜单中找到
以下代理将使用 Google Sheets 来读取和更新电子表格数据。
cookbook/tools/googlesheets_tools.py
from agno.agent import Agent
from agno.tools.googlesheets import GoogleSheetsTools
SAMPLE_SPREADSHEET_ID = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
SAMPLE_RANGE_NAME = "Class Data!A2:E"
google_sheets_tools = GoogleSheetsTools(
spreadsheet_id=SAMPLE_SPREADSHEET_ID,
spreadsheet_range=SAMPLE_RANGE_NAME,
)
agent = Agent(
tools=[google_sheets_tools],
instructions=[
"You help users interact with Google Sheets using tools that use the Google Sheets API",
"Before asking for spreadsheet details, first attempt the operation as the user may have already configured the ID and range in the constructor",
],
)
agent.print_response("Please tell me about the contents of the spreadsheet")
参数 | 类型 | 默认值 | 描述 |
---|
scopes | List[str] | None | 自定义 OAuth 作用域。如果为 None,则由操作决定。 |
spreadsheet_id | str | None | 目标电子表格的 ID。 |
spreadsheet_range | str | None | 电子表格内的范围。 |
creds | Credentials | None | 预先存在的凭据。 |
creds_path | str | None | 凭据文件的路径。 |
token_path | str | None | Token 文件的路径。 |
read | bool | True | 启用读取操作。 |
create | bool | False | 启用创建操作。 |
update | bool | False | 启用更新操作。 |
duplicate | bool | False | 启用复制操作。 |
函数 | 描述 |
---|
read_sheet | 从 Google Sheet 读取值 |
create_sheet | 创建新的 Google Sheet |
update_sheet | 更新 Google Sheet 中的数据 |
create_duplicate_sheet | 创建现有 Google Sheet 的副本 |
开发者资源
Responses are generated using AI and may contain mistakes.