From Working with tables | Google Docs API | Google Developers
requests = [{\'insertTable\': {\"table\": {
\"columns\": 2,
\"rows\": 2,
\"tableRows\": [
If my understanding is correct, how about this modification? The object using as a request body is the returned object from docs.documents.get method. In this answer, I would like to show you 3 samples.
This sample script is from the official document.
The following example inserts text into the first table cell of a table and adds a table row.
As an important point, before you run the script, please create new Google Document and put a table. Then, please use the script to the created Document. By this, the text of Hello
is put in "A1" of the table and one row is added to the table.
requests = [{
'insertText': {
'location': {
'index': 5
},
'text': 'Hello'
}
},
{
'insertTableRow': {
'tableCellLocation': {
'tableStartLocation': {
'index': 2
},
'rowIndex': 1,
'columnIndex': 1
},
'insertBelow': 'true'
}
}
]
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
In this sample script, a new table with 2 rows and 2 columns is created.
requests = [
{
"insertTable":
{
"rows": 2,
"columns": 2,
"location":
{
"index": 1
}
}
}
]
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
Unfortunately, although I had been looking for the method for creating a table and putting the values in each cell at the official document, I couldn't find. So I experimented about it. In this sample script, I show you the method for creating a table and putting the values in each cell.
The flow of this sample script is as follows.
A1
, B1
, A2
and B2
to the cells "A1:B2" of the table.
requests = [
{
"insertTable":
{
"rows": 2,
"columns": 2,
"location":
{
"index": 1
}
}
},
{
"insertText":
{
"text": "B2",
"location":
{
"index": 12
}
}
},
{
"insertText":
{
"text": "A2",
"location":
{
"index": 10
}
}
},
{
"insertText":
{
"text": "B1",
"location":
{
"index": 7
}
}
},
{
"insertText":
{
"text": "A1",
"location":
{
"index": 5
}
}
}
]
result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
https://www.googleapis.com/auth/documents
. Please be careful this.If I misunderstood your question and this was not the result you want, I apologize.