Home > Software design >  Deleting empty rows on if the row in Column D is empy
Deleting empty rows on if the row in Column D is empy

Time:05-31

Hi I im trying to work on scripts that deletes the entire row if the Columnd D has no information in that row (specailly at the buttom part). here is my script that im trying to work on but im getting this error.

function removeEmptyRows(){
  var sh = SpreadsheetApp.getActiveSheet();

  var maxRange = sh.getRange('D3:D27');
  var maxRows = sh.getMaxRows();
  var lastRow = maxRange.getLastRow();

  sh.deleteRows(lastRow 1, maxRows-lastRow);
}

THE ERROR: Exception: Those rows are out of bounds. removeEmptyRows @ Code.gs:47

i did find a working solution in the internet but it was using a for loop, which takes a lot of time to delete the rows if the rows is like 100 . Here is a sample screenot if how shout it work PS. sorry for bad english

enter image description here

CodePudding user response:

Try it this way

function removeEmptyRows() {
  var sh = SpreadsheetApp.getActiveSheet();
  var maxRange = sh.getRange('D3:D27');
  var maxRows = sh.getMaxRows();
  var lastRow = maxRange.getLastRow();
  if (maxRows > lastRow) {
    sh.deleteRows(lastRow   1, maxRows - lastRow);
  }
}
  • Related