关于 google-docs
通过 Google Docs REST API 读取、创建和编辑文档的命令行技能,使用 curl 与 jq,并说明 OAuth 权限范围及按索引编辑的注意事项。
主要功能
- 01读取文档标题和文本
- 02创建文档
- 03使用 batchUpdate 插入文本
- 04处理 OAuth 权限和 API 错误
- 05通过 jq 提取文本
适用场景
- 01读取 Google 文档内容
- 02创建会议记录
- 03向现有文档插入文本
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ acedatacloud/ skills --skill google-docs安装后在 Claude Code 中输入/google-docs确认已加载。
详细介绍
通过 curl + jq 操作 Google Docs。用户的 OAuth bearer token 位于
$GOOGLE_DOCS_TOKEN;每次调用都需要 Authorization: Bearer $GOOGLE_DOCS_TOKEN。基础 URL:https://docs.googleapis.com/v1/documents。该
令牌包含 documents.readonly(以及身份信息);写入需要 documents 权限。
失败响应为 {"error":{"code","message","status"}},请原样显示。401 =
重新安装。写入时遇到 403 PERMISSION_DENIED = 只读作用域。
文档 ID 是 URL 中的 …/document/d/<ID>/edit 片段。
D="https://docs.googleapis.com/v1/documents"; AUTH="Authorization: Bearer $GOOGLE_DOCS_TOKEN"
# Read the document. The body is a tree of structural elements; pull plain text:
curl -sS -H "$AUTH" "$D/DOC_ID" \
| jq -r '.title, ([.body.content[]?.paragraph?.elements[]?.textRun?.content] | join(""))'
创建与编辑
# Create an empty doc
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
-d '{"title":"Meeting notes 2026-06-21"}' "$D" | jq '{documentId, title}'
# Insert text at the start (index 1) via batchUpdate (confirm first)
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
-d '{"requests":[{"insertText":{"location":{"index":1},"text":"Hello\n"}}]}' \
"$D/DOC_ID:batchUpdate" | jq '.documentId'
# Replace all occurrences of a placeholder
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
-d '{"requests":[{"replaceAllText":{"containsText":{"text":"{{name}}","matchCase":true},"replaceText":"Alex"}}]}' \
"$D/DOC_ID:batchUpdate" | jq '.replies'
注意事项
- 文档模型采用基于索引的方式:文本编辑针对字符索引,每次插入都会使后续索引发生偏移 — 多次插入时,请从后往前应用,或在调用之间重新计算索引。
- 若要获取整洁的 Markdown/纯文本,请遍历
body.content[].paragraph.elements[] .textRun.content(上面的 jq 命令)。表格/列表需要更深入的遍历。 - 创建文档后,它会位于用户的 Drive 根目录;请使用
google-drive技能移动/共享它。
评论
支持 Markdown