如何以编程方式删除 Google Sheet 上的所有条件格式?

Xav*_*hay 5 google-api google-sheets google-sheets-api google-sheets-conditionalformatting

我尝试结合https://developers.google.com/sheets/api/samples/conditional-formatting 中的两个示例

  1. 阅读所有条件格式。
  2. 删除它们。

删除需要删除索引,但这不会在读取 API 响应中返回。我尝试假设数组中返回格式的索引是合适的,但是在操作中间遇到错误“索引处没有条件格式”,然后才将它们全部删除。

这是我要清除的工作表的副本:https : //docs.google.com/spreadsheets/d/1Y0tsEcka-1gziimesE74IhPFqGkUO985eZNoVQ9y0BU/edit#gid=0

Tan*_*ike 3

这个解决方案怎么样?在此解决方案中,您的问题可以通过 2 次 API 请求来解决。

1. 从电子表格上的工作表中检索所有条件格式。

sheets.spreadsheets.get用于这种情况。

要求 :

GET https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###?ranges=### sheet name ###&fields=sheets%2FconditionalFormats%2Franges%2FsheetId
Run Code Online (Sandbox Code Playgroud)

请输入### spreadsheet ID ###### sheet name ###

回复 :

此响应检索条件格式的数量。这用于删除条件格式。

{"sheets": [{"conditionalFormats": [
  {"ranges": [{"sheetId": #####}]},
  {"ranges": [{"sheetId": #####}]}
]}]}
Run Code Online (Sandbox Code Playgroud)

2.删除所有条件格式。

sheets.spreadsheets.batchUpdate用于这种情况。

要求 :

POST https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###:batchUpdate
Run Code Online (Sandbox Code Playgroud)

请求正文:

这里,index表示通过上述方法检索到的条件格式的数量GET。例如,当工作表中有2个条件格式时, 的长度requests为2。以下requests[0]含义sheets.conditionalFormats[0]如上所示。

{"requests": [
  {"deleteConditionalFormatRule": {"sheetId": #####, "index": 0}},
  {"deleteConditionalFormatRule": {"sheetId": #####, "index": 1}}
]}
Run Code Online (Sandbox Code Playgroud)

请输入### spreadsheet ID ###sheetId

笔记 :

  • 为了使用上述 API,您可以检索访问令牌。
  • 由于目的是删除工作表上的所有条件格式,因此从电子表格中检索的信息是最低限度的必要。

参考 :

如果我误解了你的问题,我很抱歉。