【发布时间】:2020-12-17 01:02:09
【问题描述】:
我有一个 python 脚本,它从外部资源中提取数据并将数据添加到 Google 表格。在大多数情况下,我一切正常,除了我想更改选项卡的颜色以表示脚本状态。
整个过程从复制工作表中的现有选项卡开始。默认情况下,模板选项卡具有黑色突出显示。然后我想将黑色更改为另一种颜色以显示数据收集正在进行中。完成后,根据数据结果将颜色更改为绿色或红色。
但是我找不到如何更改颜色的工作示例。这是我目前所拥有的:
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 和Sheet properties 的链接,我一直在其中查找请求的外观。
【问题讨论】:
标签: python google-sheets google-sheets-api gspread