0

I have looked at this discussion but didn't want to hijack it. link

I'm a newbie to google scripts but i have found this one which work in some way

function deleteRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('ark1'); // 0AmWS-4fnAa_OdG9aV210NnZlZHlDYVRZSUxrOFdwX1E&usp
  var values = s.getDataRange().getValues();

  var deleted = 0;  // Counter (don't need if we loop backwards)
  for (var row = 0; row < values.length; row++) {
    if (values[row][2] == '' || values[row][2] == 0) {
      s.deleteRow(row + 1 - deleted);
      deleted++;
    }
  }
  SpreadsheetApp.flush();
};

It works in my test spreadsheet. But only in the column C. When i enter the number 0 the row is deleted. I would like it to react to a different column. ie N instead. How do I do that? I bet the answer is pretty obvious, but I just can't figure it out. Any help is much appreciated.

Thanks!

Jesper Homann

Community
  • 1
  • 1

1 Answers1

0

Columns begin with A = 1, B = 2, C = 3 etc... so column N is 14 .

BUT

Arrays count from 0 so in your code example you will have to use

if (values[row][13] == '' || values[row][13] == 0) {

I hope I make it clear enough.

Note that in the code above you used s.deleteRow(row + 1 - deleted); and the +1 comes for the very same reason : rows count from 1 and arrays count from 0 .

IMO, the variable name row was definitely not a good choice since it's ambiguous wether it's a sheet row or an array row... one should call it n or i, anything neutral. In this example it's an array row thus starting from 0 then +1 to get the sheet row (the one we delete).

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks very much. It certainly helped me out here. clear and fast reply. Like it very much!! – user3012922 Nov 21 '13 at 12:09
  • Hi again. Unfortuneately the scripts also deletes the rows i have above where i have the value 0. Is there any way to work around this? I would like it to only delete the rows with the value 0. Thanks again! – user3012922 Nov 21 '13 at 12:38
  • Is the script in your question an exact copy of the one you use ? – Serge insas Nov 21 '13 at 12:43
  • then it should not delete anything other than rows that satify the condition. Try placing a Logger.log(row + 1 - deleted) in the loop and view the log to see what actually happens. – Serge insas Nov 21 '13 at 21:02