showing results for - "set range background color google script multiple colors"
Byron
05 Mar 2019
1const myColorFunction = ({
2  sheetName = "Form Responses 1",
3  targetValue = "Open"
4} = {}) => {
5
6  const ss = SpreadsheetApp.getActiveSpreadsheet();
7
8  const sheet = ss.getSheetByName(sheetName);
9
10  const rng = sheet.getDataRange();
11
12  const numHeaders = 1;
13
14  const backgrounds = rng.getBackgrounds();
15  const fontColors = rng.getFontColors();
16
17  const newBckgs = backgrounds.map((row) => {
18    const [firstCell] = row;
19
20    if (firstCell === targetValue) {
21      row[5] = "red";
22    }
23
24    return row;
25  });
26
27  const newColors = fontColors.map((row) => {
28    const [firstCell] = row;
29
30    if (firstCell === targetValue) {
31      row[5] = "white";
32    }
33
34    return row;
35  });
36
37  rng.setBackgrounds(newBckgs);
38  rng.setFontColors(newColors);
39}
40