google-docs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Drive Google Docs via
curl + jq
. The user's OAuth bearer token is in
$GOOGLE_DOCS_TOKEN
; every call needs
Authorization: Bearer $GOOGLE_DOCS_TOKEN
. Base URL:
https://docs.googleapis.com/v1/documents
. The token carries
documents.readonly
(+ identity); writes need
documents
.
Failures are
{"error":{"code","message","status"}}
— show verbatim.
401
= re-install.
403 PERMISSION_DENIED
on a write = read-only scope.
The doc id is the
…/document/d/<ID>/edit
segment of the URL.
bash
D="https://docs.googleapis.com/v1/documents"; AUTH="Authorization: Bearer $GOOGLE_DOCS_TOKEN"
通过
curl + jq
操作Google Docs。用户的OAuth承载令牌存储在
$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
部分的<ID>
bash
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:

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(""))'
undefined
curl -sS -H "$AUTH" "$D/DOC_ID"
| jq -r '.title, ([.body.content[]?.paragraph?.elements[]?.textRun?.content] | join(""))'
undefined

Create & edit

创建与编辑

bash
undefined
bash
undefined

Create an empty doc

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}'
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)

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'
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

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'
undefined
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'
undefined

Gotchas

注意事项

  • The document model is index-based: text edits target character indices, and every insert shifts later indices — for multiple inserts, apply them back-to-front or recompute indices between calls.
  • To get clean Markdown/plain text, walk
    body.content[].paragraph.elements[] .textRun.content
    (the jq above). Tables / lists need deeper traversal.
  • Creating a doc puts it in the user's Drive root; use the
    google-drive
    skill to move/share it.
  • 文档模型是基于索引的:文本编辑以字符索引为目标,每次插入操作都会改变后续索引——如果要执行多次插入,需从后往前操作,或者在每次调用之间重新计算索引。
  • 若要获取整洁的Markdown/纯文本,需遍历
    body.content[].paragraph.elements[] .textRun.content
    (如上述jq命令所示)。表格/列表需要更深入的遍历。
  • 创建的文档会存放在用户的Drive根目录中;可使用
    google-drive
    技能来移动/共享文档。