Clayton
Clayton

Reputation: 21

How to Ignore Empty Rows in getRange()getValues() with A1 notation

Teaching myself how to code in Google Apps Script and I can't figure out how to ignore blank rows in the array, based on getRange A1 notation.

This other post works if my range is only one column, but I'm trying to do it if a whole row is blank, not just the value in one column.

How to ignore empty cell values for getRange().getValues()

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("sample");
 var data = sheet.getRange("A1:E").getValues();
 var filtered = data.filter(String);
Logger.log(filtered);
}

In the code sample above, my log still shows all of the blank rows after the populated rows. If I change the A1 notation to A1:A, then it works, but I want to filter based on the whole row.

Thanks for helping a newbie.

Upvotes: 2

Views: 7511

Answers (1)

Cooper
Cooper

Reputation: 64082

Try this:

function myFunction() {
 var ss = SpreadsheetApp.getActive();
 var sheet = ss.getSheetByName("sample");
 var data = sheet.getRange(1,1,sheet.getLastRow(),5).getValues();
 var filtered = data.filter(String);
 Logger.log(filtered);
}

You could do it this way too.

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Sheet2");
 var data = sheet.getRange("A1:F").getValues();
  var filtered = data.filter(function(r){
    return r.join("").length>0;
  });
Logger.log(filtered);
}

Upvotes: 6

Related Questions