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

老子叫甜甜 提交于 2021-01-07 02:49:38

问题


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.


回答1:


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:

  • batch_update(body)
  • Method: spreadsheets.batchUpdate
  • UpdateSheetPropertiesRequest


来源:https://stackoverflow.com/questions/65402464/how-can-i-reset-the-color-of-a-google-sheet-through-the-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!