Google Apps 脚本:将格式规则应用于特定 sheet

Google Apps Script: Apply formatting rule to a spesific sheet

根据文件:https://developers.google.com/apps-script/reference/spreadsheet/conditional-format-rule-builder

var sheet = SpreadsheetApp.getActiveSheet();

但我只希望它位于一个名为 Resultat 的特定 sheet 中。

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");

但是有了这个 sheetByName,我得到:

TypeError: ThisSheet.newConditionalFormatRule is not a function

调用代码:

 var ThisSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");
var range = ThisSheet.getRange("C:D") // only want it to select C element.
 ThisSheet.clearConditionalFormatRules();
  var rule1 = ThisSheet.newConditionalFormatRule()
      .whenTextContains("NO")
      .setBackground("#b3cfb0")
      .setRanges([range])
      .build();    
  var rules = ThisSheet.getConditionalFormatRules();
  rules.push(rule1);
  ThisSheet.setConditionalFormatRules(rules);
  

修改点:

  • 根据你的脚本,我无法理解 ThisSheet。如果ThisSheetvar sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");sheetnewConditionalFormatRule()就是ClassSpreadsheetApp的方法。由此,错误发生。

ThisSheet为[=14=的sheet时,下面的修改如何?

修改后的脚本:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("resultat");
var range = sheet.getRange("C:D") // only want it to select C element.
sheet.clearConditionalFormatRules();
var rule1 = SpreadsheetApp.newConditionalFormatRule()
  .whenTextContains("NO")
  .setBackground("#b3cfb0")
  .setRanges([range])
  .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule1);
sheet.setConditionalFormatRules(rules);
  • 通过上面修改的脚本,条件格式规则设置为“C:D”的范围。
  • 如果要将条件格式规则设置为“C”列,请将var range = sheet.getRange("C:D")修改为var range = sheet.getRange("C:C")

参考: