MivaScott
MivaScott

Reputation: 1806

How can I reset the color of a Google Sheet through the API

Building off of this question about setting a color, I wonder how to reset/clear the color of a Google Sheet's tab.

For reference, here is how to set a color

    sheet = open_worksheet() # does all the auth/credential work
    new_tab = sheet.worksheet('DemoTab')

    body = {
        "requests": [
            {
                "updateSheetProperties": {
                    "properties": {
                        "sheetId": new_tab.id,
                        "tabColor": {
                            "red": 1.0,
                            "green": 0.3,
                            "blue": 0.4
                        }
                    },
                    "fields": "tabColor"
                }
            }
        ]
    }

    try:
        res = sheet.batch_update(body)
        pprint(res)
    except gspread.exceptions.APIError as gea:
        pprint(gea.args[0], width=100)

All the documentation states that "tabColor" should be a Color object (as shown above with a dict of red, green, and blue). There is also an optional alpha.

There is also a "tabColorStyle" parameter, but it too is looking for a color.

I have tried setting "tabColor" to an empty dict, {}, RGB to 0 each, and RGB to -1 each. All end up just turning the color black.

There is no mention of a .clear option.

So how do I remove the color once it has been set?

Here is a link to the Google Sheet API and Sheet properties where I've been looking up how the request should look.

Upvotes: 0

Views: 608

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal as follows.

  • You want to reset the tab colors of sheets in a Google Spreadsheet.
  • You want to achieve this using gspread.

Modification point:

  • In this case, I think that to use the value of fields is an important point. When "fields": "tabColor" is used for the request body of the method of batchUpdate, the property of tabColor is modified. In that case, in order to reset the tab color, tabColor is not included in properties. By this, the tab color is reset.

When above point is reflected to the script, it becomes as follows.

Sample script:

spreadsheetId = "###" # Please set the Spreadsheet ID.

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheets = spreadsheet.worksheets()
body = {"requests": [{
    "updateSheetProperties": {
        "properties": {
            "sheetId": e.id,
        },
        "fields": "tabColor"
    }
} for e in sheets]}
spreadsheet.batch_update(body)
  • In this sample script, the tab colors of all sheets in the Google Spreadsheet are reset.

Note:

  • When you want to reset the tab color of one of sheets in the Google Spreadsheet, please use the following request body.

      body = {"requests": [{
          "updateSheetProperties": {
              "properties": {
                  "sheetId": sheetId, # Please set the sheet ID.
              },
              "fields": "tabColor"
          }
      }]}
    

References:

Upvotes: 2

Related Questions