0

I am trying to use the "Replace Named Range Content Request" to update the content of table cells but I keep getting a this range cannot be replaced error, I also tried deleting the table cells content,not the table cell itself, but I got this error (

{
  "error": {
    "code": 400,
    "message": "Invalid requests[0].deleteContentRange: Invalid deletion range. Cannot delete the requested range.",
    "status": "INVALID_ARGUMENT"
  }
}

)

I have cross-checked the indexes I am using and I can't figure it out, Insert Text request works but replacing or deleting the text in a table cell brings back an error.

Here is the body of one of my table cells in JSON format

                     "startIndex": 145,
                      "endIndex": 152,
                      "paragraph": {
                        "elements": [
                          {
                            "startIndex": 145,
                            "endIndex": 152,
                            "textRun": {
                              "content": "Item 1\n",
                              "textStyle": {}
                            }

My delete content request in JSON format

{
  "requests": [
    {
      "deleteContentRange": {
        "range": {
          "startIndex": 145,
          "endIndex": 152
        }
      }
    ]
  }

I created a named range with the table cell content start and end indexes and I was expecting it to automatically replace the text in the cell when I use "Replace Named Range Content Request"

2
  • Can you show your inserting text code for context? Commented Oct 28, 2022 at 13:31
  • @Ihopethisishelpfultoyou { "requests": [ { "insertText": { "text": "Test", "location": { "index": 145, "segmentId": "" } } } ] } Commented Oct 29, 2022 at 12:53

2 Answers 2

2

I think this error normally appears when the range you specify attempts to delete an element outside of the current element's index range.

If you're trying to remove the word "Test" then endIndex should be 149 if starting at 145 (4 characters).

As the table cell itself is from range 145 to 152, then the delete range should be 146 to 151 to delete all the cell contents.

Sign up to request clarification or add additional context in comments.

1 Comment

This should be the answer.
0

Docs API is a bit of a joke. To delete the paragraph content in a tableCell, subtract 1 from the endIndex to account for the newline character.

For example, this results in the error you're seeing:

const removePhotoTokenRequest = {
      deleteContentRange: {
        range: {
          startIndex: photoElement.startIndex,
          endIndex: photoElement.endIndex,
        },
      },
    };

You'll get the error:

{
  "error": {
    "code": 400,
    "message": "Invalid requests[0].deleteContentRange: Invalid deletion range. Cannot delete the requested range.",
    "status": "INVALID_ARGUMENT"
  }
}

Instead, do this:

const removePhotoTokenRequest = {
      deleteContentRange: {
        range: {
          startIndex: photoElement.startIndex,
          endIndex: photoElement.endIndex - 1,
        },
      },
    };

Welcome to wonderful world of the Google Docs API! The four of us that have used this API to this level should hold a meetup.

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.