2

I would like to make a newly created rule for conditional formatting the top rule so it has precedent over all others but be able to do so without having to drag it to the top as this takes for ever if the column has many rules like 30 to 40 already in place for different cells within the column.

1 Answer 1

1

You want to create a new Conditional Formatting rule and make that rule the first rule without reordering by dragging the rule up in the list.

This answer is based on a short script that:


function addrule() {

  // add a new conditional formatting rule
  // AND insert the rule as the first Conditional Formatting rule 

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "MySheet";
  var sheet = ss.getSheetByName(sheetname);

  // define the range
  var range = sheet.getRange("B2");
  
  // define a rule
  var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("Definitely")
    .setBackground("#0066ff")
    .setFontColor("#ffffff")
    .setRanges([range])
    .build();

  // get all the rules
  var rules = sheet.getConditionalFormatRules();

  // insert the new rule at the beginning of the array of rules
  rules.unshift(rule);

  // update the sheet for the conditional Formatting rules
  sheet.setConditionalFormatRules(rules);
}

BEFORE

Before


AFTER

AFTER

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.