如何更改工作表选项卡的颜色
How can I change the color of a worksheet's tab
我有一个 python 脚本,它从外部资源中提取数据并将数据添加到 Google Sheet。在大多数情况下,我的一切正常,除了我想更改选项卡的颜色以表示脚本状态。
整个过程从复制 sheet 中的现有选项卡开始。默认情况下,模板选项卡具有黑色突出显示。然后我想将黑色更改为另一种颜色以显示数据收集正在进行中。完成后,根据数据结果将颜色更改为绿色或红色。
但是我找不到如何更改颜色的工作示例。这是我目前所拥有的:
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)
如果我尝试 运行针对 new_tab
指针 batch_update()
我得到:
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
如果我 运行 它反对整体 sheet
我得到:
{'code': 400,
'message': "Invalid requests[0].updateSheetProperties: You can't delete all the rows on the sheet.",
'status': 'INVALID_ARGUMENT'}
如何修复我的请求,使其正确更新单个选项卡?
这是 Google Sheet API and Sheet properties 的 link,我一直在查找请求的外观。
我相信你的目标如下。
- 您想使用 gspread 更改 Google Spreadsheet 的标签颜色。
- 您想使用
sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title)
插入新的 sheet 并想更改新 sheet 的标签颜色。
修改点:
- 在这种情况下,可以使用
new_tab.id
检索 sheet ID。您可以将此值用于请求正文。
- 在你的情况下,我认为
tabColor
而不是 *
可以作为 fields
的值。
- 我认为这可能是您遇到问题的原因。因为当使用
*
时,除了tabColor
之外的其他字段也会发生变化。
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
请修改body
如下,重新测试
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"
}
}
]
}
参考文献:
我有一个 python 脚本,它从外部资源中提取数据并将数据添加到 Google Sheet。在大多数情况下,我的一切正常,除了我想更改选项卡的颜色以表示脚本状态。
整个过程从复制 sheet 中的现有选项卡开始。默认情况下,模板选项卡具有黑色突出显示。然后我想将黑色更改为另一种颜色以显示数据收集正在进行中。完成后,根据数据结果将颜色更改为绿色或红色。
但是我找不到如何更改颜色的工作示例。这是我目前所拥有的:
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)
如果我尝试 运行针对 new_tab
指针 batch_update()
我得到:
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
如果我 运行 它反对整体 sheet
我得到:
{'code': 400,
'message': "Invalid requests[0].updateSheetProperties: You can't delete all the rows on the sheet.",
'status': 'INVALID_ARGUMENT'}
如何修复我的请求,使其正确更新单个选项卡?
这是 Google Sheet API and Sheet properties 的 link,我一直在查找请求的外观。
我相信你的目标如下。
- 您想使用 gspread 更改 Google Spreadsheet 的标签颜色。
- 您想使用
sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title)
插入新的 sheet 并想更改新 sheet 的标签颜色。
修改点:
- 在这种情况下,可以使用
new_tab.id
检索 sheet ID。您可以将此值用于请求正文。 - 在你的情况下,我认为
tabColor
而不是*
可以作为fields
的值。- 我认为这可能是您遇到问题的原因。因为当使用
*
时,除了tabColor
之外的其他字段也会发生变化。
- 我认为这可能是您遇到问题的原因。因为当使用
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
请修改body
如下,重新测试
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"
}
}
]
}