Google whenNumberLessThanOrEqualTo(string) 的表格脚本条件格式

Google Sheets script conditional formatting for whenNumberLessThanOrEqualTo(string)

在 Google sheet 中,我添加了一些与各种字母代码相关的条件格式。这里A-D是红色,E-F是黄色,G以后是绿色:

这符合我的期望。

在尝试通过应用程序脚本做同样的事情时,这似乎不受支持,因为类型检查比 UI:

  SpreadsheetApp.newConditionalFormatRule()
    .whenNumberLessThanOrEqualTo('D')
    .setBackground('#ff0000')
    .setRanges([range])
    .build());

这显示了错误消息:

Cannot find method whenNumberLessThanOrEqualTo(string)

因此我无法创建规则对象以将其应用于 sheet。

有没有办法通过应用程序脚本创建我在 UI 中制定的相同规则?或者其他方法来做到这一点?最好的方法似乎只是在 JS 中重新实现它,并在字母值的完整枚举上使用 TEXT_EQUAL_TO,或者类似于 CUSTOM_FORMULA (docs).

不幸的是,内置服务方法 whenNumberLessThanOrEqualTo() 需要数值作为参数而不是字符串。

但是,您可以利用构成 API 架构的 Advanced Sheets service instead. The advanced sheets service is basically a wrapper for the Google Sheets REST API. Its more complex and it requires that you know many of the REST resource objects (see reference documentation)。

此外,您需要先在您的 GAS 项目中启用 Sheet 的 API 高级服务 (see documentation on how to enable an advanced service)。

完成后,您可以利用该服务添加您的格式设置规则。

以下脚本是您如何执行此操作的示例:

function buildRule() {
    var conditionValue = Sheets.newConditionValue();
    var booleanCondition = Sheets.newBooleanCondition();

    var color = Sheets.newColor();
    var cellFormat = Sheets.newCellFormat();

    var booleanRule = Sheets.newBooleanRule();
    var gridRange = Sheets.newGridRange();

    var formatRule = Sheets.newConditionalFormatRule();
    var addConditionalFormatRuleRequest = Sheets.newAddConditionalFormatRuleRequest();

    var request = Sheets.newRequest();

    var batchRequest = Sheets.newBatchUpdateSpreadsheetRequest();

    conditionValue.userEnteredValue = "D";

    booleanCondition.type = "NUMBER_GREATER_THAN_EQ";
    booleanCondition.values = [conditionValue];

    // #ff0000 in RGB format
    color.red = 1; // values range from 0 to 1
    color.blue = 0;
    color.green = 0;

    cellFormat.backgroundColor = color;

    booleanRule.condition = booleanCondition;
    booleanRule.format = cellFormat;

    // selects E2 as range
    gridRange.sheetId = 0;
    gridRange.startColumnIndex = 4;
    gridRange.endColumnIndex = 5;
    gridRange.startRowIndex = 1;
    gridRange.endRowIndex = 2;

    formatRule.booleanRule = booleanRule;
    formatRule.ranges = [gridRange];

    addConditionalFormatRuleRequest.rule = formatRule;
    addConditionalFormatRuleRequest.index = 0; // index of rule; increment to add other rules

    request.addConditionalFormatRule = addConditionalFormatRuleRequest;
    batchRequest.requests = [request];

    Sheets.Spreadsheets.batchUpdate(batchRequest, SpreadsheetApp.getActive().getId());
}


上面的脚本非常冗长,所以一旦您了解 Google Sheets API 模式的资源类型,以下内容也足够了:

function buildRule() {
    var batchRequest = {
       "requests":[
          {
             "addConditionalFormatRule":{
                "rule":{
                   "booleanRule":{
                      "condition":{
                         "type":"NUMBER_GREATER_THAN_EQ",
                         "values":[
                            {
                               "userEnteredValue":"D"
                            }
                         ]
                      },
                      "format":{
                         "backgroundColor":{
                            "red":1,
                            "blue":0,
                            "green":0
                         }
                      }
                   },
                   "ranges":[
                      {
                         "sheetId":0,
                         "startColumnIndex":4,
                         "endColumnIndex":5,
                         "startRowIndex":1,
                         "endRowIndex":2
                      }
                   ]
                },
                "index":0
             }
          }
       ]
    };

    Sheets.Spreadsheets.batchUpdate(batchRequest, SpreadsheetApp.getActive().getId());
}