1

I have this script to delete row that are checked with a checkbox.

function deleterows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.showRows(1, sheet.getMaxRows());

  var values = sheet.getRange('K1:K50').getValues();

  values.forEach( function (r, i) {
    Logger.log(r);
    if (r[0]) 
      sheet.deleteRows(i+1);
    }); 
}

But it does not work properly - when I select multiple rows with the checkboxes - it deletes part of selected and part of the rows that follow selection screenshot

I am new the JavaScripts so can someone help me please

1 Answers1

1

By one hand, according to https://developers.google.com/apps-script/reference/spreadsheet/sheet#deleterowsrowposition-howmany, deleteRows require two parameters but your script is passing only one.

By the other hand, every time that a row is deleted the row number of the below rows changes, so it's better to iterate backwards. For more details see this other Q/A Deleting rows in google sheets using Google Apps Script

Rubén
  • 34,714
  • 9
  • 70
  • 166