How to add content to table cell via google-docs-api request?

我怕爱的太早我们不能终老 提交于 2019-12-11 17:25:10

问题


I want to add a content to a table cell in google doc, but the way described in the documentation doesn't work.

What is wrong with my request? When I provide 1 for index parameter of insertText request it just pastes text before table. When I provide 2 as value of index parameter I get an error: "Invalid requests[1].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines."

{
  "requests": [
    {
      "insertTable": {
        "endOfSegmentLocation": {
          "segmentId": ""
        },
        "columns": 1,
        "rows": 1
      }
    },
    {
      "insertText": {
        "location": {
          "index": 1
        },
        "text": "Cell content"
      }
    }
  ]
}

I expect that text must be inserted into the only cell of the table.


回答1:


  • You want to append a table (1 x 1) to the last body.
  • You want to insert a text to the 1st cell.

From your request body, I could understand like above. If my understanding is correct, how about this flow? I think that there might be several solutions. So please think of this as just one of them.

In the case that new table is appended to the last body ("segmentId": "" means that the table is appended to the last body.), at first, the start index of the table is required to be known. So how about the following flows?

Flow 1:

In this flow, it supposes that the index of last body is not known.

  1. Append a table using the following request body.

    {
      "requests": [
        {
          "insertTable": {
            "endOfSegmentLocation": {
              "segmentId": ""
            },
            "columns": 1,
            "rows": 1
          }
        }
      ]
    }
    
  2. Retrieve the start index of table using the following endpoint. At that time, you can also retrieve the start index of the cell.

    GET https://docs.googleapis.com/v1/documents/{fileId}?fields=body(content(startIndex%2Ctable))
    
  3. Insert the text to the cell. In this case, it supposes that the retrieved start index of the appended table is 10. The start index of the 1st cell is 14 (I think that the start index of the 1st cell can be retrieved by start index of table + 4.). In this case, the request body for inserting the text to the cell is as follows.

    {
      "requests": [
        {
          "insertText":
          {
            "location":
            {
              "index": 14
            },
            "text": "Cell content"
          }
        }
      ]
    }
    

Flow 2:

In this flow, it supposes that the index of last body is known. For example, when the table is appended to the new Document, you can create the table with the text using the following request body. In this case, the start index of the table and the cell are 1 and 5, respectively.

    {
      "requests": [
        {
          "insertTable":
          {
            "endOfSegmentLocation":
            {
              "segmentId": ""
            },
            "columns": 1,
            "rows": 1
          }
        },
        {
          "insertText":
          {
            "location":
            {
              "index": 5
            },
            "text": "Cell content"
          }
        }
      ]
    }

References:

  • Inserting or deleting table rows
  • Thread: Insert table in Google Docs API Python

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/56735529/how-to-add-content-to-table-cell-via-google-docs-api-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!