Home > Mobile >  How to delete cells in a range of a column in google app script using getLastRow()
How to delete cells in a range of a column in google app script using getLastRow()

Time:10-06

I've been looking through stackoverflow for some help on how to script in google sheets a function that would allow me to remove empty cells in a range and shift them over (to the left) when completed. I've only seen questions regarding removing empty rows or columns instead of cells in a range.

This is what the data looks like I'm trying to work with:

enter image description here

After:

enter image description here

Update

It turned out that some cells in column 'A' could contain spaces. Here is modified function that removes spaces from the range:

function blankRemoval(){
  var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');
  var col_A = sheet.getRange(1,1,sheet.getLastRow()-3,1);

  var contents = col_A.getValues().flat().join('').replace(/\s /g,'');

  if (contents == '') col_A.deleteCells(SpreadsheetApp.Dimension.COLUMNS);
}
  • Related