I'm encountering an implementation problem I need some help with:
I am building a Google Docs integration that involves programmatically creating a table and then mapping through an array of records to add the data of those records to the table cells. I'm able to create a blank table and locate the start index of each cell to insert the data into, but when I try to use the insertText request from the docs and place the text in a specific cell I get the following error:
"Invalid request[1].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraph by inserting new lines."
I've tried the following
- Simply inserting my text and hoping that each insertText request places text in a new cell
- Adding
'\n'at the start index of the cells - Creating the table with tableRows already defined (this error out as an invalid request format)
- Creating cells one-by-one and inserting text after each appended 1x1 column or row (eventually you create a row that duplicated the row above it with multiple columns and all text would just be in the first cell)
- Inserting Column breaks after the first cells text in hopes it would shift the paragraph over to the new cell
Here's the last implementation I tried:
(For context I aim to filter through an items array, create a table for each item, and then for every subitem in that item object to have a table row where relevant data will go under Header 1, 2 and 3, but for now I'd be happy just to get the tables and headers in the right places)
var requests = []
await Promise.all(
items.map(async (item) => {
let subitems = []
subitems = item.subitems.filter((subitem) => subitem.selected)
//Create table api call
await docs.documents
.batchUpdate({
documentId: newDoc.data.documentId,
resource: {
requests: [
{
insertTable: {
columns: 3,
rows: item.subitems.length + 1,
endOfSegmentLocation: { segmentId: '' },
},
},
],
},
})
.then((res) => res.data)
//Api call for cell data
var docData = await docs.documents
.get({
documentId: newDoc.data.documentId,
})
.then((res) => res.data)
requests.push(
{
insertText: {
text: 'Header 1',
location: {
index:
docData.body.content[2].table.tableRows[0].tableCells[0]
.startIndex,
},
},
},
{
insertText: {
text: 'Header 2',
location: {
index:
docData.body.content[2].table.tableRows[0].tableCells[1]
.startIndex,
},
},
},
{
insertText: {
text: 'Header 3',
location: {
index:
docData.body.content[2].table.tableRows[0].tableCells[2]
.startIndex,
},
},
}
)
})
)
//Insert Text API call
var myDoc = await docs.documents
.batchUpdate({
documentId: newDoc.data.documentId,
resource: {
requests,
},
})
.then((res) => res.data)
return myDoc
}
If you need any more info let me know. Thanks in advance.