本指南将详细介绍如何将Google Sheets与Claude集成,帮助你轻松实现相关功能。
在开始集成之前,你需要完成一些准备工作,并按照特定步骤进行操作。
在开始之前,请确保安装了以下工具:
service-account-key.json)。export SERVICE_ACCOUNT_PATH="/path/to/service-account-key.json"
export DRIVE_FOLDER_ID="your-shared-folder-id"
http://localhost:8000/callback)。export CREDENTIALS_PATH="/path/to/credentials.json"
export TOKEN_PATH="/path/to/token.json"
base64命令或在线转换器。echo '{"type":"service_account","project_id":"your-project-id","private_key_id":"key-id","private_key":"your-private-key","client_email":"service-account@your-project.iam.gserviceaccount.com","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/certs"}' | base64 -w 0 > credentials_base64.txt
export CREDENTIALS_CONFIG="Base64-encoded-string"
export DRIVE_FOLDER_ID="your-shared-folder-id"
pip install google-api-python-client fastmcp
from fastmcp import FastMCP
from googleapiclient.discovery import build
# 初始化FastMCP客户端
client = FastMCP(
auth_method="service_account",
service_account_key_path="/path/to/service-account-key.json"
)
# 创建Sheets API服务
sheets_service = build('sheets', 'v4', credentials=client.credentials)
# 获取电子表格数据
spreadsheet_id = "your-spreadsheet-id"
result = sheets_service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range="A1:B2"
).execute()
print(result)
from fastmcp import FastMCP
from googleapiclient.discovery import build
# 初始化FastMCP客户端
client = FastMCP(
auth_method="service_account",
service_account_key_path="/path/to/service-account-key.json"
)
# 创建Sheets API服务
sheets_service = build('sheets', 'v4', credentials=client.credentials)
# 新建工作表请求体
new_sheet_request = {
"addSheet": {
"properties": {
"title": "New Sheet",
"sheetType": "GRID"
}
}
}
# 执行API调用
result = sheets_service.spreadsheets().batchUpdate(
spreadsheetId="your-spreadsheet-id",
body={"requests": [new_sheet_request]}
).execute()
print("新工作表已创建")
{
"configurations": {
"google_sheets": {
"auth_method": "service_account",
"service_account_key_path": "/path/to/service-account-key.json",
"spreadsheet_id": "your-spreadsheet-id",
"drive_folder_id": "your-shared-folder-id"
}
},
"enabled_providers": ["google_sheets"]
}
python your_script.py
{
"provider": "google_sheets",
"operation": "read_range",
"params": {
"spreadsheet_id": "your-spreadsheet-id",
"range": "A1:A10"
}
}
{
"status": "success",
"data": {
"values": [
["Header"],
["Row 1 Data"],
...
]
}
}
通过以上步骤,您可以成功地将Google Sheets与Claude集成,并根据需要进行扩展和定制。