How can I change the color of a worksheet's tab

╄→гoц情女王★ 提交于 2021-02-10 05:50:52

问题


I have a python script that is pulling data from an external resource and adding data to a Google Sheet. For the most part I have everything working, except I'd like to change the color of a tab to signify the script status.

The whole process starts by duplicating an existing tab within the sheet. The template tab has a black highlight by default. I then want to change the black to another color to show the data collection is in progress. And when done, change the color to green or red depending on if the results of the data.

However I cannot find a working example of how to change the color. Here is what I have so far:

    title = 'Duplicate'
    template_id = 1000

    sheet = open_worksheet() # does all the auth/credential work
    sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title)
    new_tab = sheet.worksheet(title)

    body = {
        "requests": [
            {
                "updateSheetProperties": {
                    "properties": {
                        "sheetId": 1001,
                        "title": title,
                        "tabColor": {
                            "red": 1.0,
                            "green": 0.3,
                            "blue": 0.4
                        }
                    },
                    "fields": "*"
                }
            }
        ]
    }

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

If I try running the batch_update() against the new_tab pointer I get:

dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers

If I run it against the whole sheet I get:

{'code': 400,
 'message': "Invalid requests[0].updateSheetProperties: You can't delete all the rows on the sheet.",
 'status': 'INVALID_ARGUMENT'}

How do I fix my request so that it correctly updates the single tab?

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 change the tab color of Google Spreadsheet using gspread.
  • You want to insert new sheet using sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title) and want to change the tab color of the new sheet.

Modification points:

  • In this case, the sheet ID can be retrieved with new_tab.id. You can use this value to the request body.
  • In your situation, I think that tabColor instead of * can be used as the value of fields.
    • I thought that this might be the reason of your issue. Because when * is used, other fields except for tabColor are also changed.

When above points are reflected to your script, it becomes as follows.

Modified script:

Please modify body as follows and test it again.

body = {
    "requests": [
        {
            "updateSheetProperties": {
                "properties": {
                    "sheetId": new_tab.id,
                    # "title": title, # In this case, I think that this might not be required to be used.
                    "tabColor": {
                        "red": 1.0,
                        "green": 0.3,
                        "blue": 0.4
                    }
                },
                "fields": "tabColor"
            }
        }
    ]
}

References:

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


来源:https://stackoverflow.com/questions/65333296/how-can-i-change-the-color-of-a-worksheets-tab

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