how to get cell range values with background colors in to single array?

I tried to write the script as shown below. It is necessary to combine the values and background colors in this cell range into a single array. But it does not come as needed.
cell range

    function newsheet(){
  var app = SpreadsheetApp
  var ss = app.getActiveSpreadsheet().getSheetByName("AddData");

 //get category if checked
  var catList = ss.getRange("I5:J31").getValues();
  var catClrs = ss.getRange("J5:J31").getBackgrounds();
  var c = catList.map((key,index) => ({[key]:catClrs[index]}));
  Logger.log(c);
}

The answer comes this way
Log result

But I want an outcome like this.

    [{Extra,true,#b6d7a8}, {Extra,false,#ffffff}, {Extra,false,#ffffff}, {Ldrain ,false,#ffffff}, {Dish Drain,true,#0c343d}, {U drain,true,#274e13}, {Hard Sholder,true,#00ffff}, {Soft Sholder,true,#cc0000}, {As. wearing,true,#ff9900}, {Tack coat Wearing,true,#fce5cd}, {As.Binder,true,#9900ff}, {Tack Coat binder,true,#46bdc6}, {Prime coat,true,#000000}, {ABC overlay,false,#ffffff}, {Subbase overlay,true,#980000}, {Edgewidning ABC,true,#999999}, {Edgewidning Subbase,false,#ffffff}, {SubGrade Ew,true,#0000ff}, {Embankment 5,true,#9fc5e8}, {Embankment 4,true,#6fa8dc}, {Embankment 3,true,#3d85c6}, {Embankment 2,true,#0b5394}, {Embankment 1,true,#1c4587}, {Subgrade Emb,true,#8e7cc3}, {Soft Ground tratment 1,true,#c27ba0}, {Soft Ground tratment 2,true,#a64d79}, {Clearing & grabbing,true,#f1c232}

39 thoughts on “how to get cell range values with background colors in to single array?”

    1. Added flat()
    2. Replaced key by row, index by i and {[key]:catClrs[index]} by {row[0],row[1],catClrs[i]}
    function newsheet(){
      var app = SpreadsheetApp
      var ss = app.getActiveSpreadsheet().getSheetByName("AddData");
    
     //get category if checked
      var catList = ss.getRange("I5:J31").getValues();
      var catClrs = ss.getRange("J5:J31").getBackgrounds().flat();
      var c = catList.map((row,i) => ({row[0],row[1],catClrs[i]}));
      Logger.log(c);
    }
    
    Reply

Leave a Comment