'Filter or delete row if cell is empty

I'm looking to have a way to check cells on a specific column to see if it's empty. If it's in fact empty, filter or delete the row.

I tried to use code from similar questions to no success, as there seem to be multiple ways of doing it.

What I understand so far is I need to define the usual values:

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ordering"); // The particular sheet I'm using
var rows = ss.getDataRange(); // I assume this counts the rows?
var columntocheck = ss.getRange(1, 7, rows); // This defines the column I want to check for empty cells
var values = rows.getValues(); // Define value of the row to be checked?

This is where I'm not sure how to approach the next step. I see most people use for loop to check each row for data length or for "" or "0".

Would appreciate any tips.



Solution 1:[1]

If sheet format (colors, fonts etc) is not important you can use this fast and efficient approach (it will clear the whole sheet and re-populate with data that has non empty cell in the column you have chosen) :

function deleteEmptyRows(){ 
  var sh = SpreadsheetApp.getActiveSheet();
  var data = sh.getDataRange().getValues();
  var colNum = 0;// choose the column to check : 0=A, 1=B, 2=C etc... 
  var targetData = new Array();
  for(n=0;n<data.length;++n){
    if(data[n][colNum]!=''){ targetData.push(data[n])};// check the cell for "not empty" (does not detect formulas !!)
  }
  sh.getDataRange().clear();
  sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge insas