Search code examples
javascriptgoogle-docsgoogle-docs-api

Google Doc API (JavaScript) - batchUpdate - updateTextStyle foregroundColor returns 500?


I have an app that generates copies a document then runs batch update on the new file. When trying to use updateTextStyle I keep getting a 500 error without much detail. My entire request returns fine without foreground color, including inserting a table and multiple replace text requests.

This error also occurs when just doing it on a basic text document via the try it out feature.

For the easiest example, using the Google Docs API Try It, I run the following on a blank google doc I created in my root directory with the text 'TEST COLOR CHANGE GOOGLE DOC' and nothing else.

Code I execute:

{
  "requests": [
    {
      "updateTextStyle": {
        "fields": "foregroundColor",
        "range": {
          "startIndex": 1,
          "endIndex": 3,
        },
        "textStyle": {
          "foregroundColor": {
            "color": {
              "rgbColor": {
                "blue": 255,
                "green": 0,
                "red": 0
              }
            }
          }
        }
      }
    }
  ]
}

Error I get back:

{
  "error": {
    "code": 500,
    "message": "Internal error encountered.",
    "status": "INTERNAL"
  }
}

I've tried various ways and nothing seems to be working. Any help is appreciated!


Solution

    • You want to modify foregroundColor using the method of documents.batchUpdate in Google Docs API.

    If my understanding is correct, how about this modification?

    I think that your request body is mostly correct. But about rgbColor, please modify as follows. I think that your error message is due to this. In the official document, it says as follows.

    • red: The red component of the color, from 0.0 to 1.0.
    • green: The green component of the color, from 0.0 to 1.0.
    • blue: The blue component of the color, from 0.0 to 1.0.

    From this, please modify the property of rgbColor in your request body as follows.

    From:

    "rgbColor": {
      "blue": 255,
      "green": 0,
      "red": 0
    }
    

    To:

    "rgbColor": {
      "blue": 1,
      "green": 0,
      "red": 0
    }
    

    Reference:

    If I misunderstood your question and this was not the result you want, I apologize.