我有一个 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)
如果我尝试运行batch_update()
反对这new_tab
我得到的指针:
dict(vr, range=absolute_range_name(self.title, vr['range']))
TypeError: string indices must be integers
如果我对整体运行它sheet
I get:
{'code': 400,
'message': "Invalid requests[0].updateSheetProperties: You can't delete all the rows on the sheet.",
'status': 'INVALID_ARGUMENT'}
如何修复我的请求,以便它正确更新单个选项卡?
这是一个链接谷歌表格API https://developers.google.com/sheets/api/guides/batchupdate and 板材特性 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#SheetProperties我一直在查找请求的外观。