如何通过 API 重置 Google Sheet 的颜色
How can I reset the color of a Google Sheet through the API
根据 设置颜色,我想知道如何 reset/clear Google Sheet 选项卡的颜色。
供参考,这里是如何设置颜色
sheet = open_worksheet() # does all the auth/credential work
new_tab = sheet.worksheet('DemoTab')
body = {
"requests": [
{
"updateSheetProperties": {
"properties": {
"sheetId": new_tab.id,
"tabColor": {
"red": 1.0,
"green": 0.3,
"blue": 0.4
}
},
"fields": "tabColor"
}
}
]
}
try:
res = sheet.batch_update(body)
pprint(res)
except gspread.exceptions.APIError as gea:
pprint(gea.args[0], width=100)
所有文档都指出“tabColor”应该是一个 Color 对象(如上所示,带有红色、绿色和蓝色的字典)。还有一个可选的 alpha。
还有一个“tabColorStyle”参数,但它也在寻找一种颜色。
我试过将“tabColor”设置为空字典,{}
,RGB 分别设置为 0,RGB 分别设置为 -1。最终都变成了黑色。
没有提到 .clear
选项。
那么如何在设置颜色后将其删除?
这是 Google Sheet API and Sheet properties 的 link,我一直在查找请求的外观。
我相信你的目标如下。
- 您想重置 Google 电子表格中工作表的标签颜色。
- 您想使用 gspread 实现此目的。
修改点:
- 在这种情况下,我认为使用
fields
的值是很重要的一点。 batchUpdate方法的request body使用"fields": "tabColor"
时,修改了tabColor
的属性。在那种情况下,为了重置选项卡颜色,tabColor
不包含在 properties
中。由此,标签颜色被重置。
将以上几点反映到脚本中,就变成了下面的样子。
示例脚本:
spreadsheetId = "###" # Please set the Spreadsheet ID.
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheets = spreadsheet.worksheets()
body = {"requests": [{
"updateSheetProperties": {
"properties": {
"sheetId": e.id,
},
"fields": "tabColor"
}
} for e in sheets]}
spreadsheet.batch_update(body)
- 在此示例脚本中,重置了 Google 电子表格中所有工作表的标签颜色。
注:
如果您想重置 Google 电子表格中其中一张的标签颜色,请使用以下请求正文。
body = {"requests": [{
"updateSheetProperties": {
"properties": {
"sheetId": sheetId, # Please set the sheet ID.
},
"fields": "tabColor"
}
}]}
参考文献:
根据
供参考,这里是如何设置颜色
sheet = open_worksheet() # does all the auth/credential work
new_tab = sheet.worksheet('DemoTab')
body = {
"requests": [
{
"updateSheetProperties": {
"properties": {
"sheetId": new_tab.id,
"tabColor": {
"red": 1.0,
"green": 0.3,
"blue": 0.4
}
},
"fields": "tabColor"
}
}
]
}
try:
res = sheet.batch_update(body)
pprint(res)
except gspread.exceptions.APIError as gea:
pprint(gea.args[0], width=100)
所有文档都指出“tabColor”应该是一个 Color 对象(如上所示,带有红色、绿色和蓝色的字典)。还有一个可选的 alpha。
还有一个“tabColorStyle”参数,但它也在寻找一种颜色。
我试过将“tabColor”设置为空字典,{}
,RGB 分别设置为 0,RGB 分别设置为 -1。最终都变成了黑色。
没有提到 .clear
选项。
那么如何在设置颜色后将其删除?
这是 Google Sheet API and Sheet properties 的 link,我一直在查找请求的外观。
我相信你的目标如下。
- 您想重置 Google 电子表格中工作表的标签颜色。
- 您想使用 gspread 实现此目的。
修改点:
- 在这种情况下,我认为使用
fields
的值是很重要的一点。 batchUpdate方法的request body使用"fields": "tabColor"
时,修改了tabColor
的属性。在那种情况下,为了重置选项卡颜色,tabColor
不包含在properties
中。由此,标签颜色被重置。
将以上几点反映到脚本中,就变成了下面的样子。
示例脚本:
spreadsheetId = "###" # Please set the Spreadsheet ID.
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheets = spreadsheet.worksheets()
body = {"requests": [{
"updateSheetProperties": {
"properties": {
"sheetId": e.id,
},
"fields": "tabColor"
}
} for e in sheets]}
spreadsheet.batch_update(body)
- 在此示例脚本中,重置了 Google 电子表格中所有工作表的标签颜色。
注:
如果您想重置 Google 电子表格中其中一张的标签颜色,请使用以下请求正文。
body = {"requests": [{ "updateSheetProperties": { "properties": { "sheetId": sheetId, # Please set the sheet ID. }, "fields": "tabColor" } }]}