I’m trying to amend cells in a Google doc using their api. But I can’t seem to delete text from the tableCell.
I always get the invalid deletion range error. There is this duplicate question.
But the solution is what I’m already doing:
let requests = [];
// I hold an instance of the table in the document.
// The getCellAt(…) is a function to return the tableCell object.
// It appears to work correctly: a breakpoint at this line and inspection
// of the cell variable confirms this.
let cell = this._tables[0].getCellAt(row, column);
cell.content.forEach((cntnt) => {
cntnt.paragraph.elements.forEach((element) => {
let deletion = getNewDeleteRequest();
deletion.deleteContentRange.range.startIndex = element.startIndex;
deletion.deleteContentRange.range.endIndex = element.endIndex - 1; //subtract 1 for the end newline
deletion.deleteContentRange.range.tabId = this._documentTabID;
// Ensure minimum of length 2
let deletionRange = deletion.deleteContentRange.range.endIndex - deletion.deleteContentRange.range.startIndex;
if (deletionRange > 1) { // avoids deleting paragraphs just with single new lines
requests.push(deletion);
}
})
});
return requests.reverse();
The helper function is:
/**
* This is the structure for deleting text
*/
const deleteRequestTemplate = {
deleteContentRange: {
range: {
startIndex: 10,
endIndex: 50,
tabId: "teb id"
}
}
}
// Return a perfect clone of the delete text template
function getNewDeleteRequest() {
return structuredClone(deleteRequestTemplate);
}
Inserting text at the startIndex (using very similar helper function to the one above) works and subtracting 2 or 0 from the endIndex makes no difference.
As noted in one of the answers to the other SO question, there’s now only 5 of us using the api…..