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

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.



Solution 1:[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:

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

Solution 2:[2]

This is a follow up to the answers posted already. @ANewb hinted the the flow ends after the 5th cell. Ensure the values/entries in the payload aren't empty strings. For anyone encountering the same challenge, you can try using tenary conditions - for php $value == "" ? "N/A : $value;, for js let value = checkValue == "" ? "N?A": checkValue;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike
Solution 2