0

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…..

1
  • That's why the array is reversed on the reurn line. Although it doesn't seem to matter if it's one or many requests. Commented 2 days ago

1 Answer 1

0

It turns out that for me, the best way (i.e. actually works) is to add one to the cell's start index. Perhaps the document has unprintable characters..... I don't think it does. But for anyone else, they might like to try adding one to the start and subtracting one from the end indicies of the Google Doc tableCell object:

deletion.deleteContentRange.range.startIndex = cell.startIndex + 1; // << Fixed it!!
deletion.deleteContentRange.range.endIndex = cell.endIndex - 1; // Presumably for the end new line that needs to be left in place.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.