Reputation: 1806
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.
Upvotes: 2
Views: 1161
Reputation: 201378
I believe your goal as follows.
sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title)
and want to change the tab color of the new sheet.new_tab.id
. You can use this value to the request body.tabColor
instead of *
can be used as the value of fields
.
*
is used, other fields except for tabColor
are also changed.When above points are reflected to your script, it becomes as follows.
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"
}
}
]
}
Upvotes: 1