[英]How can I change the color of a worksheet's tab
我有一個 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 屬性的鏈接,我一直在其中查找請求的外觀。
我相信你的目標如下。
sheet.duplicate_sheet(template_id, insert_sheet_index=0, new_sheet_name=title)
插入新工作表,並希望更改新工作表的標簽顏色。new_tab.id
檢索工作表 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"
}
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.