Google-sheets – Google Spreadsheet Script for Deleting Rows with Given Value in Given Range

google sheetsgoogle-apps-script

I am looking to delete rows that have a zero in column E in a particular row range. I have the following script, which will delete the rows with a zero in column E but this seems to apply to the whole spreadsheet.

{
var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();

   var rowsDeleted = 0;
   for (var i = 0; i <= numRows - 1; i++) {
   var row = values[i];
   if (row[4] == 0 || row[4] == '') {
  sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
  rowsDeleted++;
   }
  }

I am looking for help in applying this to a range, such as row 10 through row 20 rather than the entire spreadsheet.

Best Answer

Looks that the OP code was taken from the answer by Mike Grace to Delete a row in Google Spreadsheets if value of cell in said row is 0 or blank

The code doesn't check the entire spreadsheet, just the data range. Anyway to check only the rows from 10 to 20 replace

for (var i = 0; i <= numRows - 1; i++) {

by

for (var i = 9; i <= 20 - 1; i++) {