google-docs-api - 如何使用 Google Docs API 缩进项目符号列表

标签 google-docs-api

从看起来像的 Google Doc 开始:

* Item
我希望进行一系列 API 调用以将文档转换为:
* Item
   - Subitem
但是,我不知道如何使用 API 做到这一点。 CreateParagraphBulletRequest 没有我可以指定的缩进级别。文档建议:

The nesting level of each paragraph will be determined by counting leading tabs in front of each paragraph. To avoid excess space between the bullet and the corresponding paragraph, these leading tabs are removed by this request. This may change the indices of parts of the text.


但是,将制表符添加到 InsertTextRequest 的开头将在制表符之前添加,而不是更改缩进:
* Item
*        Subitem
有没有人对我可能做错的事情有任何想法?

最佳答案

我相信你的目标如下。

  • 您想使用 Google Docs API 创建嵌套列表。
  • 首先,Google 文档中存在一个列表,该列表将一个项目作为第一级。如下。
      - item1
    
  • 在这种情况下,您希望将嵌套项作为第二级插入到现有列表中。如下。
      - item1
         - item2
    

  • 实现目标的要点:
    在这种情况下,为了将项目插入到现有列表中作为第 2 级,根据我的经验,我无法直接插入它。就我而言,作为一种解决方法,我使用以下流程。
  • 插入文字 \n\titem2\n第二级使用 insertText要求。
  • 在这种情况下,第 1 级也被插入。好像要插入深层次的项目,需要从第1层开始设置,转换成带项目符号的列表。

  • 使用 createParagraphBullets ,它将项目符号提供给列表。至此,\t转换为嵌套项。
  • 删除第 1 级的项目符号。
  • 删除换行符。

  • 示例请求正文:
    当上面的流程反射(reflect)到Docs API中batchUpdate方法的请求体时,就变成了这样。
    {
      "requests": [
        {
          "insertText": {
            "text": "\n\titem2\n",
            "location": {
              "index": 7
            }
          }
        },
        {
          "createParagraphBullets": {
            "range": {
              "startIndex": 1,
              "endIndex": 15
            },
            "bulletPreset": "BULLET_DISC_CIRCLE_SQUARE"
          }
        },
        {
          "deleteParagraphBullets": {
            "range": {
              "startIndex": 7,
              "endIndex": 8
            }
          }
        },
        {
          "deleteContentRange": {
            "range": {
              "startIndex": 7,
              "endIndex": 8
            }
          }
        }
      ]
    }
    
    结果:
    当使用上述请求体时,得到如下结果。
    前:
    enter image description here
    后:
    enter image description here
    笔记:
  • 虽然我已经在不更改现有列表的情况下寻找其他使用 Docs API 的方法,但不幸的是,我仍然找不到它们。我认为为了将深层嵌套的项目插入现有列表,在当前阶段,可能需要使用 \t 从第一级提供项目。 .不幸的是,我不确定这是否是规范。那么,例如,如何为问题跟踪器请求这个作为 future 的请求? Ref

  • 引用:
  • Working with lists
  • documents.batchUpdate
  • 关于google-docs-api - 如何使用 Google Docs API 缩进项目符号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65330602/

    相关文章:

    google-docs - 如何使用 Google Docs API 编辑 Google Docs 标题?

    google-apps-script - 从谷歌驱动器永久删除文件

    python - 如何使用 watch 订阅 Google 云端硬盘上的更改

    iphone - 适用于 iOS 的 Google Drive 与 Google Doc?

    Java Google Docs API - 在独立 java 程序中使用的文档转换工具

    url - 通过 id 打开谷歌文档

    java - 使用 API 在 Google 文档中导入 XML 数据源

    android - "411 Length required"- 来自使用 Android API 10 及更低版本的 Google Docs Api 的响应

    google-drive-api - 谷歌电子表格批量更新很慢

    c# - 如何在 Google 文档中获取文件夹中的所有文档