0

I`d like to make a function clear:

 function clear() {
  var spreadsheet = SpreadsheetApp.getActive();
    for(var i=2;i<1001;i++) {
     if (spreadsheet.getActiveSheet().getRange(i,2).getValue() == "a") {
      spreadsheet.getRange(i:i).activate();
  spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
}
}
}

but i have problem with this line:

spreadsheet.getRange(i:i).activate()

How shout I write this to select row number 'i'?

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • I also try to do this like: `z='"' + i + ':' + i + '"' spreadsheet.getRange(z).activate(); ` but I have an information that Range not found – Bartosz Banasiak Feb 13 '19 at 17:47
  • ``i+':'+i``. Also note that, that line is not needed. `deleteRow(i,1)` should be enough. – TheMaster Feb 13 '19 at 18:27
  • Why are you even trying to activate a `Range`? You almost never need to do that--just programmatically manipulate it without the meaningless fluff of `activate` -> `getActiveRange` – tehhowch Feb 14 '19 at 00:58
  • Welcome. Have a look at `getDataRange`. The [Google documentation](https://developers.google.com/apps-script/reference/spreadsheet/sheet#getdatarange) for `getDataRange` has an excellent, **short**, working example of how to loop though every row **and** column. Even though you don't want to loop the columns, it is worth running the script as-is just to see how the looping works and to distinguish the row and column components; then you can just remove the column part. – Tedinoz Feb 16 '19 at 01:43

1 Answers1

0

How shout I write this to select row number 'i'?

Instead of i:i use i+':'+i

REMARKS:

  1. The code in the question is very inefficient for deleting rows. It's possible to delete several rows at a time by using deleteRows(rowPosition, howMany).

Related

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