Reputation: 37
I'm trying to figure out how to fix this problem. I tried to use 'for loop' to set multiple ranges with one function but got error message.
'Exception: The parameters (String) don't match the method signature for SpreadsheetApp.ConditionalFormatRuleBuilder.setRanges.'
Here's my code.
function myFunction() {
let columnStart = ['d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','aa','ab','ac']
let columnEnd = ['d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','aa','ab','ac','ad','ae','af','ag','ah','ai','ai','ai','ai','ai','ai','ai','ai','ai']
let range='';
let formula ='';
let rule = '';
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetname')
for(let z=0; z<columnStart.length;z++){
formula = '=sum($'+columnStart[z]+'6:$'+columnEnd[z+6]+'6)>=40'
range += '[';
for(let i=3; i<25; i++){
if(i!=24){
range += "sheet.getRange('"+columnStart[z]+2*i+':'+columnEnd[z+6]+2*i+"'),"
}else{
range += "sheet.getRange('"+columnStart[z]+2*i+':'+columnEnd[z+6]+2*i+"')"
}
}
range += "]"
Logger.log(range)
rule = SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied(formula).setBackground('#f4cccc').setRanges(range).build();
rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
range ="";
formula='';
}
}
Firstly, i thought maybe the range got from the for loop has the problem. but when copied the range from the Logger.log() and pasted that range to the setRange's range, it worked.
Here's the code I tried.
Could you help me figure this out? Any advice would be appreciated.
Upvotes: 1
Views: 760
Reputation: 201503
I believe your goal as follows.
for (let i = 3; i < 25; i++) {,,,}
.const formula = '=sum($' + columnStart[z] + '6:$' + columnEnd[z + 6] + '6)>=40';
.range
of Logger.log(range)
is the string value. But when setRanges(range)
is used, range
is required to be the Range object. I think that this might be the reason of your issue.rules
of rules = sheet.getConditionalFormatRules()
.
rules
with sheet.setConditionalFormatRules(rules)
put outside of the loop.When above points are reflected to your script, it becomes as follows.
function myFunction() {
let columnStart = ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac'];
let columnEnd = ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'ai', 'ai', 'ai', 'ai', 'ai', 'ai', 'ai', 'ai'];
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetname');
const rules = []; // sheet.getConditionalFormatRules();
for (let z = 0; z < columnStart.length; z++) {
const formula = '=sum($' + columnStart[z] + '6:$' + columnEnd[z + 6] + '6)>=40';
const rangeList = [];
for (let i = 3; i < 25; i++) {
rangeList.push(columnStart[z] + 2 * i + ':' + columnEnd[z + 6] + 2 * i);
}
const ranges = sheet.getRangeList(rangeList).getRanges();
const rule = SpreadsheetApp.newConditionalFormatRule().whenFormulaSatisfied(formula).setBackground('#f4cccc').setRanges(ranges).build();
rules.push(rule);
}
sheet.setConditionalFormatRules(rules);
}
In your script, rules = sheet.getConditionalFormatRules();
is used. In this case, the rules are added every run. In this case, I thought that const rules = [];
might be suitable instead of rules = sheet.getConditionalFormatRules();
. In this answer, const rules = [];
is used. If you want to use rules = sheet.getConditionalFormatRules();
, please modify above script.
In this answer, I used the range and the formula from your script. If your actual situation is different from them, please modify above script. Please be careful this.
Upvotes: 3