[HELP] Google Apps Script: Generate conditional formatting rules using arrays

152 views
Skip to first unread message

Adam

unread,
Sep 16, 2022, 12:19:05 PM9/16/22
to Google Apps Script Community
I want to use a for loop to take the values from 2 arrays and generate conditional formatting rules for each pair of values. 

my code is currently as follows but it is rejected because the rules are in an array.

function addRules()  {
for (var i=0; i<colorCodeVals.length;i++)
{
var conditionalFormatRule = app.newConditionalFormatRule()
 .whenTextEqualTo(colorCodeVals[i])
 .setBackground(colorCodeColors[i])
 .setRanges([templateDataRange])
var rules = templateSheet.getConditionalFormatRules();
rules.push(addRules);
templateSheet.setConditionalFormatRules(rules);
}
}

Basically I created a sheet where the user can input what conditional formatting they wish to have on the sheet by inputting an example. My code then checks the range and extracts the strings into an array and the background colours into another array. 
I want the code to create conditional format rules where if text equals array1[0] then the background sets to array2[0].
Reply all
Reply to author
Forward
0 new messages