I found this wonderful script here:
CodePudding user response:
The argument of setRanges(ranges)
of Class ConditionalFormatRuleBuilder is Range[]
. So when you want to use "C2, C3, C4, H1, H2, H3, N5"
as the ranges, as a sample modification, how about the following modification?
Modified script:
function applyConditionalFormat() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rule = createRule(sheet, "C2, C3, C4, H1, H2, H3, N5", "=$B2>=10", "#cc0000");
sheet.setConditionalFormatRules([rule]);
}
function createRule(sheet, rangeNotation, formula, color) {
var ranges = sheet.getRangeList(rangeNotation.split(",").map(e => e.trim())).getRanges();
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied(formula)
.setBackground(color)
.setRanges(ranges)
.build();
return rule;
}
- In this modification, the value of
rangeNotation
given bycreateRule(sheet, "C2, C3, C4, H1, H2, H3, N5", "=$B2>=10", "#cc0000")
is converted to the value ofRange[]
. And, the value is used withsetRanges(ranges)
.