I am making custom conditional formatting rules using this method: ConditionalFormatRuleBuilder.
However, If I ever update it, say the color in .setBackground("#FF0000"), and run it again, it creates a new conditional formatting rule that conflicts with the original one.
What I want to know, is in that example is there a way to remove the rule defined as rule? For example is there an opposite of rules.push(rule);? Something like rules.remove(rule); ?
Here's an example from my code. I currently have this:
var rule1 = SpreadsheetApp.newConditionalFormatRule()
.whenTextEqualTo(s1)
.setBackground(s1c)
.setRanges([r])
.build();
var rules = curSheet.getConditionalFormatRules();
rules.push(rule1);
curSheet.setConditionalFormatRules(rules);
Would it be possible to remove the rule defined as rule1?
If you highlight your entire sheet and select Conditional Formatting from the menu, you will see a list of all of the active format rules on the right of the screen.
The order that the rules in that pane is the same order of the rules in the .getConditionalFormatRules() array.
So you can use splice() to remove which ever rule(s) you want
function deleteFormat(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet1");
var rules = sheet.getConditionalFormatRules();
// if you want to remove rules starting at the 4th rule and delete only 1 rule.
rules.splice(3,1);
sheet.setConditionalFormatRules(rules);
}
I tested it on a sample sheet, and it worked. If you have any issues, add a comment and I will look into it.
Also, if you want to edit a rule programmatically, you can use slice with the builder.
This will remove the old rule at that position in the array, and replace it with the new one.