Google 文档 API 以编程方式添加 table 内容

Google Docs API programmatically adding a table of content

我有一个 python 脚本,它可以进行一些分析并将结果作为文本(段落)输出到 Google 文档中。我知道如何通过 batchUpdate 插入文本、更新段落和文本样式。

doc_service.documents().batchUpdate(documentId=<ID>,body={'requests': <my_request>}).execute()

例如,"my_request" 的形式类似于:

request = [
        {
            "insertText": {
                "location": {
                    "index": <index_position>,
                    "segmentId": <id>
                },
                "text": <text>
            }
        },
        {
            "updateParagraphStyle": {
                "paragraphStyle": {
                    "namedStyleType": <paragraph_type>
                },
                "range": {
                    "segmentId": <id>,
                    "startIndex": <index_position>,
                    "endIndex": <index_position>
                },
                "fields": "namedStyleType"
            }
        },
    ]

但是,一旦脚本完成更新 table,如果可以在文档顶部添加 table 内容就太好了。

但是,我对 Google Docs API 还很陌生,我不太确定该怎么做。我知道我应该使用 "TableOfContents" 作为 StructuralElement。我也知道这个选项目前不会在每次对文档进行修改后自动更新(这就是为什么我想在文档完成更新后创建它并将其放在文档顶部)。

如何用 python 做到这一点?我不清楚在我的请求中调用 "TableOfContents" 的位置。

非常感谢!

在你发表评论后,我能够更好地理解你想要做什么,但我看到了这两个 Issue Tracker 的帖子:

这些是众所周知的功能请求,遗憾的是尚未实现。您可以点击此页面左上角问题编号旁边的 ☆,因为它可以让 Google 知道更多人遇到此问题,因此更有可能更快地看到它。

因此,无法以编程方式insert/update table 内容。