gogcli 文档页脚操作指南:用gog docs footer create在 Google Docs 中创建并填充页脚
【免费下载链接】gogcliGoogle Workspace in your terminal.项目地址: https://gitcode.com/GitHub_Trending/gogcl/gogcli
导读
本文聚焦 gogcli(Google Workspace in your terminal)中gog docs footer create命令的完整用法:它用于在 Google Docs 文档中创建页脚(Footer),并可选地在同一次操作中填入初始文本。文章不仅覆盖全部命令行参数,还深入源码(internal/cmd/docs_header_footer.go)剖析其背后两次BatchUpdate调用的实现原理,并结合仓库测试用例验证行为,帮助你在终端、脚本与 Agent 工作流中可靠地管理文档页脚。
命令定位:gog docs footer家族
gog docs footer是 gogcli 文档编辑命令族的一员,对应 Google Docs API 中的页眉/页脚分段(segment)管理。命令树定义见 internal/cmd/docs_header_footer.go:
gog docs footer create(别名add、new)——创建并可选填充页脚(本命令)gog docs footer list(别名ls)——列出页脚及其分段 ID,见 gog-docs-footer-listgog docs footer delete(别名rm、remove、del)——删除页脚,见 gog-docs-footer-delete
与之对称的还有gog docs header系列;两者共享同一套底层实现(runDocsSegmentCreate、runDocsSegmentList、runDocsSegmentDelete),仅通过docsSegmentKindHeader/docsSegmentKindFooter区分目标类型。
用法与别名
gog docs (doc) footer (footers) create (add,new) <docId> [flags]要点:
docs可用别名doc,footer可用别名footers,create可用别名add、new;<docId>是位置参数,必填。gogcli 会在请求前对docId做normalizeGoogleID与去空格处理,空 ID 直接报错empty docId(见 docs_header_footer.go);- 无位置参数时可通过
--tab指定目标标签页,见下文「定位到指定标签页」。
完整参数表
以下参数完整继承自官方命令文档(gog-docs-footer-create.md),并按源码补充了默认行为说明。
命令专属参数
| Flag | 类型 | 默认 | 说明 |
|---|---|---|---|
--file | string | 从文件读取初始页脚文本(-表示从标准输入读取) | |
--text | string | 初始页脚文本 | |
--tab | string | 按标题或 ID 定位到指定标签页(参见 gog docs list-tabs) | |
--index | *int64 | 字符索引(1 表示文档开头);省略则默认放在文档末尾 | |
--at | string | 按字面文本锚定,使用匹配区间起点作为插入位置 | |
--occurrence | *int | 当--at匹配有歧义时,取第 N 次匹配(从 1 开始计数) | |
--match-case | bool | 对--at匹配启用大小写敏感 | |
--at-end | bool | 定位到文档/标签页末尾(与--index、--at互斥) |
全局/通用参数
| Flag | 类型 | 默认 | 说明 |
|---|---|---|---|
--access-token | string | 直接使用提供的访问令牌(绕过已存刷新令牌;令牌约 1 小时过期) | |
-a--account--acct | string | 账户邮箱、别名或auto,用于已认证的 Google API 命令 | |
--client | string | OAuth 客户端名称(选择对应存储凭据与令牌桶) | |
--color | string | auto | 输出颜色:auto|always|never |
--disable-commands | string | 逗号分隔的禁用命令列表;支持点路径 | |
-n--dry-run--dryrun--noop--preview | bool | 不真正改动,仅打印预期动作并成功退出 | |
--enable-commands | string | 逗号分隔的启用命令前缀列表;支持点路径(限制 CLI 范围) | |
--enable-commands-exact | string | 逗号分隔的精确启用命令列表;父命令不自动启用子命令 | |
-y--force--assume-yes--yes | bool | 对破坏性命令跳过确认 | |
--gmail-no-send | bool | false | 阻止 Gmail 发送操作(Agent 安全) |
-h--help | kong.helpFlag | 显示上下文相关帮助 | |
--home | string | 覆盖 gogcli 配置/数据/状态/缓存根目录(等价于GOG_HOME) | |
-j--json--machine | bool | false | 向标准输出输出 JSON(最适合脚本化) |
--no-input--non-interactive--noninteractive | bool | 绝不提示,失败即退出(适合 CI) | |
-p--plain--tsv | bool | false | 输出稳定可解析的文本(TSV,无颜色) |
--quota-project | string | 为 API 用量计费的 Google Cloud 项目(发送为X-Goog-User-Project;部分 API 在使用--access-token或 ADC 时要求) | |
--readonly | bool | false | 运行时阻止变更类 API 请求;auth add也改为申请只读 OAuth 范围 |
--results-only | bool | JSON 模式下仅输出主结果(丢弃nextPageToken等信封字段) | |
--select--pick--project | string | JSON 模式下按逗号分隔选择字段(尽力而为;支持点路径;多数命令建议用--fields) | |
-v--verbose | bool | 启用详细日志 | |
--version | kong.VersionFlag | 打印版本并退出 | |
--wrap-untrusted | bool | false | JSON/raw 输出中将抓取的文本字段包上外部不可信内容标记 |
文本来源:--text与--file的解析规则
页脚初始文本有两个来源,二者互斥。解析逻辑位于 internal/cmd/docs_helpers.go 的resolveTextInput:
- 只传
--text:直接使用该字符串; - 只传
--file:读取文件内容,-表示从标准输入读取;路径支持环境变量展开(config.ExpandPath,见 docs_helpers.go); - 两者都传:报错
use only one of --text or --file; - 两者都没传:
provided=false,命令仍会创建空页脚,但不追加文本(此时只发出 1 个 API 请求)。
注意与一般文档编辑命令不同:create允许不提供文本(创建空页脚),而插入脚注等命令要求非空文本。该差异在源码runDocsSegmentCreate中体现:if provided && text != ""时才执行填充步骤(见 docs_header_footer.go)。
定位语义:页脚「创建在哪一节」
页脚属于文档的分段(section)资源,创建时通过SectionBreakLocation指定挂靠的节。gogcli 的定位选项(--index、--at、--at-end、--occurrence、--match-case、--tab)统一定义在DocsBodyPlacementFlags(见 internal/cmd/docs_structural.go),与gog docs insert、gog docs insert-footnote、gog docs insert-section-break等命令共用同一套占位解析器。
行为细节(见 docs_header_footer.go 与 docs_header_footer.go):
- 不指定任何定位参数:目标为所选标签页的默认节(
section: document-default),即页脚挂到该标签页首个分节; - 指定
--index/--at/--at-end:先解析出具体位置,再定位到包含该位置最近的SectionBreak(docsSectionBreakLocation从文档正文中向前找最近的节断点,见 docs_header_footer.go); - 目标在表格内部:直接报错
header/footer sections cannot be selected from inside a table; --tab:指定标签页标题或 ID,配合--index定位到该标签页内的节。
测试用例 internal/cmd/docs_header_footer_test.go 验证了「选中标签页首节」的行为:当指定--tab Second时,实际发出的请求体包含"sectionBreakLocation":{"index":0,"tabId":"tab-2"}。
底层实现:两次BatchUpdate调用
从源码看,gog docs footer create的请求序列是先创建、后填充两步(见 docs_header_footer.go):
- 第一次
BatchUpdate:发送CreateFooterRequest{Type: "DEFAULT", SectionBreakLocation: ...},让 Docs API 创建页脚分段并返回FooterId; - 从响应提取分段 ID:遍历
response.Replies找到CreateFooter.FooterId(docsCreatedSegmentID,见 docs_header_footer.go);若缺失则报错create footer response missing segment ID; - 第二次
BatchUpdate(仅当提供了非空文本):发送InsertTextRequest,通过EndOfSegmentLocation{SegmentId: segmentID, TabId: tabID}把文本追加到新建页脚内部,实现「创建即填充」; - 输出结果时
requests字段反映实际发出的请求次数(创建 1 次,填充则共 2 次)。
测试TestDocsHeaderCreatePopulatesReturnedSegment(docs_header_footer_test.go)精确断言了这两个请求的先后顺序与内容:第一次请求必须是CreateHeader.Type == "DEFAULT",第二次必须是InsertText且EndOfSegmentLocation.SegmentId == "header-1"、文本与参数一致。页脚与页眉共用完全相同的代码路径(仅 kind 不同),因此该测试同时佐证了页脚创建的两步行为。
输出格式
创建成功后的默认输出为 TSV 行(writeDocsSegmentMutationResult,见 docs_header_footer.go):
documentId <docId> segmentId <footerId> segmentType footer requests 1|2 tabId <tabId> # 仅当指定了标签页时出现使用--json时输出等价 JSON 对象,适合脚本解析:
gog docs footer create "DOC_ID" --text "Page Footer" --json{"documentId":"DOC_ID","segmentId":"kix.footer.xxx","segmentType":"footer","requests":2}实战示例
1. 创建空页脚
gog docs footer create "DOC_ID"不提供文本,仅创建DEFAULT类型页脚,后续可用 gog docs insert / gog docs update 配合--segment <footerId>追加内容(测试TestDocsSegmentTextCommandsPropagateSegmentID验证了分段 ID 在编辑命令间的传递)。
2. 创建并填充简单文本
gog docs footer create "DOC_ID" --text "Confidential — Internal use only"3. 从文件读取页脚文本
gog docs footer create "DOC_ID" --file footer.txt gog docs footer create "DOC_ID" --file - < footer.txt # 标准输入4. 将页脚挂到指定标签页
gog docs footer create "DOC_ID" --text "Appendix footer" --tab "Appendix"5. 先预览后执行(dry-run)
gog docs footer create "DOC_ID" --text "Draft" --dry-run--dry-run会打印预期动作(含documentId、segmentType、textBytes、定位信息)而不真正改动文档(见 docs_header_footer.go)。
6. 定位到文档末尾或指定文本之后
gog docs footer create "DOC_ID" --text "Footer" --at-end gog docs footer create "DOC_ID" --text "Footer" --at "REFERENCES" --occurrence 1 --match-case安全与脚本化注意事项
--readonly:开启后运行时拦截变更类 API 请求,docs footer create将无法执行,适合只读巡检场景;-y/--force:delete 类命令的破坏性确认可跳过;create本身不触发破坏性确认;--no-input:CI 环境中不交互,失败即退出,避免挂起;--disable-commands/--enable-commands:可限制 CLI 暴露范围,例如只允许docs前缀,是 Agent 沙箱隔离的常用手段;--gmail-no-send:与页脚无关但常随全局 flag 出现,用于阻断 Gmail 发送,属 Agent 安全设计。
使用前提
- 需先完成 gogcli 的认证(参见 安装与认证),使目标账户具备该 Google Docs 文档的编辑权限;
- 命令基于 Google Docs API,需要网络访问与对应 OAuth 范围;
--readonly认证时无法执行创建操作; DOC_ID支持文档 URL 中的 ID 或纯 ID,gogcli 会在请求前做规范化处理。
相关命令索引
- 父命令:gog docs footer、gog docs
- 兄弟命令:gog docs footer list、gog docs footer delete
- 对称功能:gog docs header create
- 编辑已建页脚:gog docs insert、gog docs update
- 命令总索引:Command index
说明:本文对应的命令帮助文档由
gog schema --json自动生成(见 gog-docs-footer-create.md),以仓库当前版本为准。
【免费下载链接】gogcliGoogle Workspace in your terminal.项目地址: https://gitcode.com/GitHub_Trending/gogcl/gogcli
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考