阅读Google Spreadsheets条件格式信息

时间:2019-04-05 13:02:55

标签: google-sheets-api

我希望使用脚本阅读Google电子表格的条件格式信息。我这样做如下:

function readConditionalFormatInfo() {
  var url = "https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId?fields=sheets(properties(title,sheetId),conditionalFormats)"
  var response = UrlFetchApp.fetch(url)
  Logger.log(response)
}

但是发生错误:

Request failed for https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId?fields=sheets(properties(title,sheetId),conditionalFormats) returned code 403. Truncated server response: { "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } } (use muteHttpExceptions option to examine full response) (line 214, file "Code")

也许我不应该在GAS中运行REST API,但是我不知道如何在脚本中执行该操作!

感谢任何人都可以提供帮助!

1 个答案:

答案 0 :(得分:1)

如下所示:

function readConditionalFormat() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  var rules = sheet.getConditionalFormatRules();
  if (rules != null) {
    for (var i=0;i<rules.length;i++) {
      var rule = rules[i].getBooleanCondition()
      var criteria = rule.getCriteriaType()
      var value = rule.getCriteriaValues()
      var bakcolor = rule.getBackground()
      Logger.log(criteria+ " -> " + value + ":" + bakcolor);
    }
  } else {
    Logger.log('Conditional Format rule null')
  }
}