简体   繁体   中英

Apps script conditional formatting for value not in array

在此处输入图像描述

I have a column containing US state abbreviations. I'd like to have an array of all 50 states:

const states = ['DE','AK','TX','CA' .........]

that the value of each cell in the column is compared against. If its not one of the states I'd like to format it red.

I've looked at conditional formatting ( https://developers.google.com/apps-script/reference/spreadsheet/conditional-format-rule-builder#whenTextDoesNotContain(String) ) where they have sample code like:

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextDoesNotContain("hello")
.setBackground("#FF0000")
.setRanges([range])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

Is there a conditional rule that would allow (in pseudocode):

.whenTextDoesNotContain(states)

Here is an example of using a custom formula in conditional formatting.

I create a data set in A1:A5.

Then I put conditional formatting on A7:A10. The custom formula is =ISNA(MATCH(A7,A$1:A$5,0))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM