Array.push.setAnyFormatting('red')?

后端 未结 2 689
暗喜
暗喜 2021-01-25 21:48

Description:

Stack Overflow user mhawksey recently did some fantastic optimization of my code, and in doing so, introduced me to super efficient array p

2条回答
  •  滥情空心
    2021-01-25 22:14

    You can use all the various spreadsheet methods to get and set colors, font sizes, font weights, etc. to and from distinct arrays but you can not mix these "attributes" in one single item. See the doc here.

    Or even handier, in your script editor write a script that defines a range and play with the auto complete to see everything you can do with it...

    (control+space keys)


    edit following your code update.

    You should create a second array that holds all the background colors of your range and fill it according to your needs. In the code below I build the array in parrallels with your output array, not very elegantly but rather systematically to show how it works.

    Note that null value means "no background color" while #0F0 is the hexadecimal code for green but you can also use the 'green' string if you prefer...

        ...
        var output = [];
        var backGrounds=[]
        //loop through all cells in column A
        for (row = 0; row < lastRow; row++) {
          var cellValue = data[row][0];
          var dash = false;
          if (typeof cellValue === 'string') {
            dash = cellValue.substring(0, 1);
            //if a number copy to our output array
          } else {
            output.push([cellValue]);
            backGrounds.push([null]);
          }
    
          //if -dash
          if (dash === "-") {
            //build first + last name
            var name = (data[(row+1)][0]+" "+data[(row+2)][0]).trim();
            //add row for the -state (e.g. -MI)
            output.push([cellValue]);
            backGrounds.push([null]);
            output.push([name]);
            backGrounds.push([null]);
            output.push(["Order complete"]);
            backGrounds.push(['#0F0']);
            //add a blank row
            output.push([""]);
            backGrounds.push([null]);
            //jump an extra row to speed things up
            row++;
          }
        }
      s.getRange(1, 1, output.length).setBackgrounds(backGrounds);
    ...
    

提交回复
热议问题