跳转到内容

Google 文档集成

让你的代理通过文本操作和格式设置来创建、编辑和管理 Google 文档。自动化文档创建、插入和替换文本、管理内容范围,并借助 AI 驱动的自动化简化文档工作流。

在使用 Google 文档集成前,请确保你已经具备:

  • 一个拥有有效订阅的 CrewAI AMP 账号
  • 一个可访问 Google 文档的 Google 账号
  • 已通过 集成页面 连接你的 Google 账号
  1. 进入 CrewAI AMP Integrations
  2. 在身份验证集成部分找到 Google Docs
  3. 点击 Connect 并完成 OAuth 流程
  4. 授予文档访问所需的权限
  5. Integration Settings 复制你的 Enterprise Token
Terminal window
uv add crewai-tools
Terminal window
export CREWAI_PLATFORM_INTEGRATION_TOKEN="your_enterprise_token"

或者将其添加到你的 .env 文件中:

CREWAI_PLATFORM_INTEGRATION_TOKEN=your_enterprise_token
google_docs/create_document

描述: 创建新的 Google 文档。

参数:

  • title (string, optional): 新文档的标题。
google_docs/get_document

描述: 获取 Google 文档的内容和元数据。

参数:

  • documentId (string, required): 要检索的文档 ID。
  • includeTabsContent (boolean, optional): 是否包含标签页内容。默认值为 false
  • suggestionsViewMode (string, optional): 应用于文档的建议视图模式。枚举值:DEFAULT_FOR_CURRENT_ACCESS, PREVIEW_SUGGESTIONS_ACCEPTED, PREVIEW_WITHOUT_SUGGESTIONS。默认值为 DEFAULT_FOR_CURRENT_ACCESS
google_docs/batch_update

描述: 对 Google 文档应用一个或多个更新。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • requests (array, required): 要应用于文档的更新列表。每一项都是表示一个请求的对象。
  • writeControl (object, optional): 提供对写入请求执行方式的控制。包含 requiredRevisionId (string) 和 targetRevisionId (string)。
google_docs/insert_text

描述: 在 Google 文档的特定位置插入文本。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • text (string, required): 要插入的文本。
  • index (integer, optional): 插入文本的零基索引。默认值为 1
google_docs/replace_text

描述: 替换 Google 文档中的所有文本实例。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • containsText (string, required): 要查找并替换的文本。
  • replaceText (string, required): 用于替换的文本。
  • matchCase (boolean, optional): 搜索是否区分大小写。默认值为 false
google_docs/delete_content_range

描述: 删除 Google 文档中特定范围的内容。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • startIndex (integer, required): 要删除范围的起始索引。
  • endIndex (integer, required): 要删除范围的结束索引。
google_docs/insert_page_break

描述: 在 Google 文档的特定位置插入分页符。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • index (integer, optional): 插入分页符的零基索引。默认值为 1
google_docs/create_named_range

描述: 在 Google 文档中创建命名范围。

参数:

  • documentId (string, required): 要更新的文档 ID。
  • name (string, required): 命名范围的名称。
  • startIndex (integer, required): 范围起始索引。
  • endIndex (integer, required): 范围结束索引。
google_docs/create_document_with_content

描述: 一次性创建包含内容的新 Google 文档。

参数:

  • title (string, required): 新文档的标题。显示在文档顶部和 Google Drive 中。
  • content (string, optional): 要插入到文档中的文本内容。使用 \n 表示新段落。
google_docs/append_text

描述: 将文本追加到 Google 文档末尾。无需指定索引,系统会自动追加到文档结尾。

参数:

  • documentId (string, required): 来自 create_document 响应或 URL 的文档 ID。
  • text (string, required): 要追加到文档末尾的文本。使用 \n 表示新段落。
google_docs/set_text_bold

描述: 在 Google 文档中将文本设为粗体或移除粗体格式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • bold (boolean, required): 设为 true 表示加粗,false 表示移除粗体。
google_docs/set_text_italic

描述: 在 Google 文档中将文本设为斜体或移除斜体格式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • italic (boolean, required): 设为 true 表示斜体,false 表示移除斜体。
google_docs/set_text_underline

描述: 在 Google 文档中为文本添加或移除下划线格式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • underline (boolean, required): 设为 true 表示添加下划线,false 表示移除下划线。
google_docs/set_text_strikethrough

描述: 在 Google 文档中为文本添加或移除删除线格式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • strikethrough (boolean, required): 设为 true 表示添加删除线,false 表示移除。
google_docs/set_font_size

描述: 更改 Google 文档中文本的字体大小。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • fontSize (number, required): 以磅为单位的字体大小。常见尺寸:10、11、12、14、16、18、24、36。
google_docs/set_text_color

描述: 在 Google 文档中使用 RGB 值(0-1 范围)更改文本颜色。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要格式化的文本起始位置。
  • endIndex (integer, required): 要格式化的文本结束位置(不含)。
  • red (number, required): 红色分量(0-1)。示例:1 表示完全红。
  • green (number, required): 绿色分量(0-1)。示例:0.5 表示半绿。
  • blue (number, required): 蓝色分量(0-1)。示例:0 表示无蓝。
google_docs/create_hyperlink

描述: 将现有文本变为 Google 文档中的可点击超链接。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要设为链接的文本起始位置。
  • endIndex (integer, required): 要设为链接的文本结束位置(不含)。
  • url (string, required): 链接应指向的 URL。示例:"https://example.com"
google_docs/apply_heading_style

描述: 为 Google 文档中的文本范围应用标题或段落样式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要设置样式的段落起始位置。
  • endIndex (integer, required): 要设置样式的段落结束位置。
  • style (string, required): 要应用的样式。枚举值:NORMAL_TEXT, TITLE, SUBTITLE, HEADING_1, HEADING_2, HEADING_3, HEADING_4, HEADING_5, HEADING_6
google_docs/set_paragraph_alignment

描述: 设置 Google 文档中段落的文本对齐方式。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要对齐的段落起始位置。
  • endIndex (integer, required): 要对齐的段落结束位置。
  • alignment (string, required): 文本对齐方式。枚举值:START(左对齐)、CENTEREND(右对齐)、JUSTIFIED
google_docs/set_line_spacing

描述: 设置 Google 文档中段落的行距。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 段落起始位置。
  • endIndex (integer, required): 段落结束位置。
  • lineSpacing (number, required): 行距百分比。100 = 单倍,115 = 1.15 倍,150 = 1.5 倍,200 = 双倍。
google_docs/create_paragraph_bullets

描述: 将段落转换为 Google 文档中的项目符号或编号列表。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 要转换为列表的段落起始位置。
  • endIndex (integer, required): 要转换为列表的段落结束位置。
  • bulletPreset (string, required): 项目符号/编号样式。枚举值:BULLET_DISC_CIRCLE_SQUARE, BULLET_DIAMONDX_ARROW3D_SQUARE, BULLET_CHECKBOX, BULLET_ARROW_DIAMOND_DISC, BULLET_STAR_CIRCLE_SQUARE, NUMBERED_DECIMAL_ALPHA_ROMAN, NUMBERED_DECIMAL_ALPHA_ROMAN_PARENS, NUMBERED_DECIMAL_NESTED, NUMBERED_UPPERALPHA_ALPHA_ROMAN, NUMBERED_UPPERROMAN_UPPERALPHA_DECIMAL
google_docs/delete_paragraph_bullets

描述: 移除 Google 文档中段落的项目符号或编号。

参数:

  • documentId (string, required): 文档 ID。
  • startIndex (integer, required): 列表段落起始位置。
  • endIndex (integer, required): 列表段落结束位置。
google_docs/insert_table_with_content

描述: 一次性将带内容的表格插入 Google 文档。

参数:

  • documentId (string, required): 文档 ID。
  • rows (integer, required): 表格行数。
  • columns (integer, required): 表格列数。
  • index (integer, optional): 插入表格的位置。如果未提供,则表格会插入到文档末尾。
  • content (array, required): 以二维数组形式提供的表格内容。每个内部数组是一行。示例:[["Year", "Revenue"], ["2023", "$43B"], ["2024", "$45B"]]
google_docs/insert_table_row

描述: 在现有表格中参考单元格的上方或下方插入新行。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。请从 get_document 获取。
  • rowIndex (integer, required): 参考单元格的行索引(从 0 开始)。
  • columnIndex (integer, optional): 参考单元格的列索引(从 0 开始)。默认值为 0
  • insertBelow (boolean, optional): 若为 true 则在下方插入,若为 false 则在上方插入。默认值为 true
google_docs/insert_table_column

描述: 在现有表格中参考单元格的左侧或右侧插入新列。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。
  • rowIndex (integer, optional): 参考单元格的行索引(从 0 开始)。默认值为 0
  • columnIndex (integer, required): 参考单元格的列索引(从 0 开始)。
  • insertRight (boolean, optional): 若为 true 则向右插入,若为 false 则向左插入。默认值为 true
google_docs/delete_table_row

描述: 删除 Google 文档中现有表格的一行。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。
  • rowIndex (integer, required): 要删除的行索引(从 0 开始)。
  • columnIndex (integer, optional): 该行中任意单元格的列索引(从 0 开始)。默认值为 0
google_docs/delete_table_column

描述: 删除 Google 文档中现有表格的一列。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。
  • rowIndex (integer, optional): 该列中任意单元格的行索引(从 0 开始)。默认值为 0
  • columnIndex (integer, required): 要删除的列索引(从 0 开始)。
google_docs/merge_table_cells

描述: 将一组表格单元格合并为一个单元格。所有单元格中的内容都会保留。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。
  • rowIndex (integer, required): 合并的起始行索引(从 0 开始)。
  • columnIndex (integer, required): 合并的起始列索引(从 0 开始)。
  • rowSpan (integer, required): 要合并的行数。
  • columnSpan (integer, required): 要合并的列数。
google_docs/unmerge_table_cells

描述: 将之前合并的表格单元格拆分回独立单元格。

参数:

  • documentId (string, required): 文档 ID。
  • tableStartIndex (integer, required): 表格的起始索引。
  • rowIndex (integer, required): 合并单元格的行索引(从 0 开始)。
  • columnIndex (integer, required): 合并单元格的列索引(从 0 开始)。
  • rowSpan (integer, required): 合并单元格所跨的行数。
  • columnSpan (integer, required): 合并单元格所跨的列数。
google_docs/insert_inline_image

描述: 将来自公共 URL 的图片插入 Google 文档。图片必须可公开访问,大小小于 50MB,且为 PNG/JPEG/GIF 格式。

参数:

  • documentId (string, required): 文档 ID。
  • uri (string, required): 图片的公共 URL。必须可在无需身份验证的情况下访问。
  • index (integer, optional): 插入图片的位置。如果未提供,则图片会插入到文档末尾。默认值为 1
google_docs/insert_section_break

描述: 插入分节符以创建具有不同格式的文档分区。

参数:

  • documentId (string, required): 文档 ID。
  • index (integer, required): 插入分节符的位置。
  • sectionType (string, required): 分节符类型。枚举值:CONTINUOUS(停留在同一页)、NEXT_PAGE(从新页开始)。
google_docs/create_header

描述: 为文档创建页眉。会返回一个 headerId,可用于配合 insert_text 添加页眉内容。

参数:

  • documentId (string, required): 文档 ID。
  • type (string, optional): 页眉类型。枚举值:DEFAULT。默认值为 DEFAULT
google_docs/create_footer

描述: 为文档创建页脚。会返回一个 footerId,可用于配合 insert_text 添加页脚内容。

参数:

  • documentId (string, required): 文档 ID。
  • type (string, optional): 页脚类型。枚举值:DEFAULT。默认值为 DEFAULT
google_docs/delete_header

描述: 从文档中删除页眉。使用 get_document 查找 headerId。

参数:

  • documentId (string, required): 文档 ID。
  • headerId (string, required): 要删除的页眉 ID。从 get_document 响应中获取。
google_docs/delete_footer

描述: 从文档中删除页脚。使用 get_document 查找 footerId。

参数:

  • documentId (string, required): 文档 ID。
  • footerId (string, required): 要删除的页脚 ID。从 get_document 响应中获取。
from crewai import Agent, Task, Crew
# Create an agent with Google Docs capabilities
docs_agent = Agent(
role="Document Creator",
goal="Create and manage Google Docs documents efficiently",
backstory="An AI assistant specialized in Google Docs document creation and editing.",
apps=['google_docs'] # All Google Docs actions will be available
)
# Task to create a new document
create_doc_task = Task(
description="Create a new Google Document titled 'Project Status Report'",
agent=docs_agent,
expected_output="New Google Document 'Project Status Report' created successfully"
)
# Run the task
crew = Crew(
agents=[docs_agent],
tasks=[create_doc_task]
)
crew.kickoff()
from crewai import Agent, Task, Crew
# Create an agent focused on text editing
text_editor = Agent(
role="Document Editor",
goal="Edit and update content in Google Docs documents",
backstory="An AI assistant skilled in precise text editing and content management.",
apps=['google_docs/insert_text', 'google_docs/replace_text', 'google_docs/delete_content_range']
)
# Task to edit document content
edit_content_task = Task(
description="In document 'your_document_id', insert the text 'Executive Summary: ' at the beginning, then replace all instances of 'TODO' with 'COMPLETED'.",
agent=text_editor,
expected_output="Document updated with new text inserted and TODO items replaced."
)
crew = Crew(
agents=[text_editor],
tasks=[edit_content_task]
)
crew.kickoff()
from crewai import Agent, Task, Crew
# Create an agent for advanced document operations
document_formatter = Agent(
role="Document Formatter",
goal="Apply advanced formatting and structure to Google Docs",
backstory="An AI assistant that handles complex document formatting and organization.",
apps=['google_docs/batch_update', 'google_docs/insert_page_break', 'google_docs/create_named_range']
)
# Task to format document
format_doc_task = Task(
description="In document 'your_document_id', insert a page break at position 100, create a named range called 'Introduction' for characters 1-50, and apply batch formatting updates.",
agent=document_formatter,
expected_output="Document formatted with page break, named range, and styling applied."
)
crew = Crew(
agents=[document_formatter],
tasks=[format_doc_task]
)
crew.kickoff()

身份验证错误

  • 确保你的 Google 账号具有访问 Google 文档所需的权限。
  • 验证 OAuth 连接是否包含 Google Docs API 所需的全部作用域(https://www.googleapis.com/auth/documents)。

文档 ID 问题

  • 仔细检查文档 ID 是否正确。
  • 确保文档存在且你的账号可访问。
  • 文档 ID 可在 Google Docs URL 中找到。

文本插入和范围操作

  • 使用 insert_textdelete_content_range 时,请确保索引位置有效。
  • 记住 Google Docs 使用零基索引。
  • 文档必须在指定索引位置有内容。

批量更新请求格式

  • 使用 batch_update 时,请确保 requests 数组根据 Google Docs API 文档正确格式化。
  • 复杂更新需要每种请求类型对应特定的 JSON 结构。

替换文本操作

  • 对于 replace_text,请确保 containsText 参数与要替换的文本完全匹配。
  • 使用 matchCase 参数控制是否区分大小写。

需要帮助?

如需 Google 文档集成设置或故障排查方面的帮助,请联系我们的支持团队。