0
votes

I have a google sheets file where I filter rows based on a single column criteria. if certain cell in a column is empty, I filter that entire row. But right now my problem is that I am trying to set Background color to the filtered row. But I keep getting this error: "TypeError: rows.filter(...).setBackground is not a function" when I try the following code:

var mydata3 = rows.filter(row => row[8] == '').setBackground("red");

Does anyone know how I can achieve this or is there any work around? Because I can see that setBackground is not the data type as mydata3 which is basically a filtered row.

Thanks.

   var sss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; 
   var sheetID = sss.getSheetId(); // get the id of first page
   var values = sss.getDataRange().getValues();
   var getRangeRow2 = sss.getDataRange().getRow()[5]; 
   var getRangeCol2 = sss.getDataRange().getColumn()[1]; 
   function insertRecord() {  
      var sheet_1 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
      var headerRowNumber = 3;
      var rows = sheet_1.getDataRange().offset(headerRowNumber, 0, sheet_1.getLastRow() - 
      headerRowNumber).getValues();
      var mydata3 = rows.filter(row => row[8] == '');
   }
1
Why not use formula gs-conditional-formatting?TheMaster

1 Answers

1
votes

Try it this way:

function insertRecord() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheets()[0];
  const rg=sh.getRange(3,1,sh.getLastRow()-2,sh.getLastColumn());
  let vs=rg.getValues()
  var rows=vs.map(function(r,i){
    if(r[8]=='') {
      sh.getRange(i+3,1,1,sh.getLastColumn()).setBackground('#ff0000');
      return r;
    }
  }).filter(function(e){return e;});
  console.log(rows);
}