Search code examples
google-docs-api

How do I indent a bulleted list with the Google Docs API


Starting with a Google Doc that looks like:

* Item

I'm hoping to make a series of API calls to turn the doc into:

* Item
   - Subitem

But, I can't figure out how to do this with the API. A CreateParagraphBulletRequest doesn't have an indent level I can specify. The documentation suggests:

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.

However, prepending tabs to the beginning of an InsertTextRequest will prepend the tab character, rather than changing the indent:

* Item
*        Subitem

Does anyone have any ideas for what I may be doing wrong?


Solution

  • I believe your goal as follows.

    • You want to create a nested list using Google Docs API.

    • At first, a list, which has one item as 1st level, is existing in the Google Document. It's as follows.

        - item1
      
    • Under this situation, you want to insert a nested item to the existing list as 2nd level. It's as follows.

        - item1
           - item2
      

    Points for achieving your goal:

    In this case, in order to insert the item to the existing list as the 2nd level, in my experience, I couldn't directly insert it. In my case, as a workaround, I'm using the following flow.

    1. Insert a text \n\titem2\n for 2nd level using insertText request.
      • In this case, the 1st level is also inserted. It seems that in order to insert the deep level items, it is required to set from the 1st level and convert to the list with the bullets.
    2. Using createParagraphBullets, it gives the bullets to the list. By this, \t is converted to the nested items.
    3. Remove the bullet of the 1st level.
    4. Remove the line break.

    Sample request body:

    When above flow is reflected to the request body of the method of batchUpdate in Docs API, it becomes as follows.

    {
      "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
            }
          }
        }
      ]
    }
    

    Result:

    When above request body is used, the following result is obtained.

    Before:

    enter image description here

    After:

    enter image description here

    Note:

    • Although I had looked for other methods for using Docs API without changing the existing list, unfortunately, I cannot still find them. I thought that in order to insert the deep nested items to the existing list, in the current stage, the items might be required to be given from 1st level using \t. Unfortunately, I'm not sure whether this is the specification. So, for example, how about requesting this for the issue tracker as the future request? Ref

    References: