首页 文章

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

提问于
浏览
2

我尝试结合两个例子来自https://developers.google.com/sheets/api/samples/conditional-formatting

  • 阅读所有条件格式 .

  • 删除它们 .

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

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

1 回答

  • 1

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

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

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

    要求:

    GET https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###?ranges=### sheet name ###&fields=sheets%2FconditionalFormats%2Franges%2FsheetId
    

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

    回复:

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

    {"sheets": [{"conditionalFormats": [
      {"ranges": [{"sheetId": #####}]},
      {"ranges": [{"sheetId": #####}]}
    ]}]}
    

    2.删除所有条件格式 .

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

    要求:

    POST https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###:batchUpdate
    

    申请机构:

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

    {"requests": [
      {"deleteConditionalFormatRule": {"sheetId": #####, "index": 0}},
      {"deleteConditionalFormatRule": {"sheetId": #####, "index": 1}}
    ]}
    

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

    注意:

    • 为了使用上述API,您可以检索访问令牌 .

    • 因为要删除工作表上的所有条件格式是目标,从电子表格中检索的信息是必需的最小值 .

    参考文献:

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

相关问题