context-infra 检查与复盘infra.guiming.net · 全内容自包含呈现 · 生成于 2026-07-21 16:28 UTC

Google Docs 操作

Z3 全文↑ Z2 条目

术-操作 · 术层 skill 全文

← 返回术层 skill 索引 · 返回方法论区

本页是 <code>rules/skills/google_docs.md</code> 的逐字投影(仅隐私清洗,零改写)。

时点提示:本页是仓内文件 rules/skills/google_docs.md 的逐字投影(仅做隐私清洗:仓库根绝对路径→相对路径、家目录→~/;除此零改写)。若源文件后续有修订,以仓内真源为准。

Skill: Google Docs 操作

通过 CLI 命令操控 Google Docs:发布 Markdown 文件、创建文档、搜索、修改、分享、Tab 管理。

When to Use

用户说出以下意图时触发:

Prerequisites

调用方式

所有命令在项目目录下通过 python -m gdocs 调用,输出均为 JSON。

cd /path/to/knowledge_working/adhoc_jobs/gdocs_skill && source .venv/bin/activate

常见场景

场景 1:把一个 Markdown 文件发布到 Google Docs

这是最常见的需求。

python -m gdocs publish path/to/report.md --title "AI 前线 2026-03-08"

发布后立刻分享给某人:

python -m gdocs publish path/to/report.md --title "报告" --share someone@example.com --role writer

场景 2:Tab 管理

列出文档所有 Tab:

python -m gdocs tab list DOC_ID

给文档添加新 Tab:

python -m gdocs tab add DOC_ID "Tab标题"
python -m gdocs tab add DOC_ID "Tab标题" path/to/content.md --format markdown

更新已有 Tab 的内容(清空后重写):

python -m gdocs tab replace DOC_ID TAB_ID path/to/updated.md

默认使用 markdown 格式。如需纯文本:

python -m gdocs tab replace DOC_ID TAB_ID file.txt --format plain

重命名 Tab:

python -m gdocs tab rename DOC_ID TAB_ID "新标题"

场景 3:创建空文档

python -m gdocs create --title "新文档"

场景 4:搜索文档

python -m gdocs search "关键词"
python -m gdocs search "关键词" --max-results 20

场景 5:分享文档

python -m gdocs share DOC_ID --email user@example.com --role writer
python -m gdocs share DOC_ID --email user@example.com --role reader --message "请查看"

场景 5.5:删除文档(清理 test / 废 docs)

python -m gdocs delete DOC_ID                # 移到回收站(默认,30 天可恢复)
python -m gdocs delete DOC_ID --permanent    # 立即永久删除(不可恢复)

何时用

纪律:每次 create / publish 之前,先想清楚是否会留下不需要的 doc。如果是 debug 用 doc,用完立即 delete,不要留在用户的 Drive 里污染。Default trash 模式保留 30 天可恢复窗口。

场景 6:修改标题 / 获取链接

python -m gdocs title DOC_ID "新标题"
python -m gdocs link DOC_ID
python -m gdocs link DOC_ID --public

场景 7:在文档中插入本地图片

python -m gdocs image DOC_ID path/to/image.png --width 468

默认插入到文档末尾。指定位置插入需要知道字符 index:

python -m gdocs image DOC_ID image.png --index 2050 --width 468

--width 468 是全宽。图片会先上传到 Google Drive 再插入文档。

带图片的 Markdown 发布流程:Markdown 中的 !alt(local_path.png) 不会自动变成图片,publish 后 alt text 会保留为纯文本。正确做法:

  1. 先 publish markdown(图片占位符变成 alt text)
  2. 用 Google Docs API 扫描文档找到 alt text 的 index
  3. 从文档底部往顶部逐个 gdocs image --index 插入,避免 index 偏移

支持的 Markdown 格式

语法效果
# 标题Heading 1
## 标题Heading 2
### 标题Heading 3
加粗加粗
斜体斜体
加粗斜体加粗+斜体
` 代码 `等宽字体 (Courier New)
文本(url)超链接
- 项目无序列表
1. 项目有序列表
---分割线(灰色居中线)
> 引用文本引用块(左缩进 + 左边框)
`\col \col \`原生表格(表头自动加粗)

注意事项

故障排查

CLI publish 失败时怎么办

正常错误信息应包含 status code 和 response body,例如 {"error": "Failed to create document '...': HTTP 503 — <body>"}。先看 status code 和 body,再决定重试还是修参数。

Status code 速查:

反 pattern:fallback 到 workaround chain

如果 CLI publish 失败,不要先创建 debug doc、再绕过 CLI 直接调 client、再 tab replace、再 share。这样容易留下 test doc 污染 Drive。正确做法是先读错误细节;如果不得不创建 debug doc 验证 auth,用完立即 gdocs delete DOC_ID

CLI publish 排版不对

症状:doc 创建成功但所有内容是纯文本,没有 heading / table 样式。

原因:直接调用 client.create_document() 没走 content_format="markdown" 路径。

修复:用 gdocs tab replace DOC_ID t.0 file.md --format markdown 重写同一份 doc。Tab id 可以用 gdocs tab list DOC_ID 查到。不要重新 create 新 doc。

OAuth 重新授权

如果遇到 OAuth 相关错误:

  1. 删除 secrets/token.json
  2. 重新运行命令,浏览器会弹出授权页面
  3. 确认当前 Google 账号在 OAuth consent screen 的 Test users 列表中

← 返回术层 skill 索引 · 返回方法论区