MartinK
MartinK

Reputation: 422

How to delete a row after matching a value in an array?

I'm using an array to handle spreadsheet data, and manipulated it. In this case, I am looking for the value of true in a column and then want to delete that row. Here's what I have:

  for ( r = 1 ; r < Array1.length; r++) { 
    if(Array1[r][33]== true) { 
      calcSheet.deleteRow(Array1[r])};
  }

I see the problem, but don't know the solution: firstArray[r] returns the whole row as an array, and not the row number. How can I get the row number? It's eluding me.

UPDATE: Here's the completed code. Thanks again to Serge.
You will notice that Serge pointed out a spreadsheet and array matching problem that would occur. The code below takes care of that as well; that's what all those r's are doing.

 var rr = 0

 for ( r = 1 ; r < firstArray.length; r++) { // iterate the first col of masterSheet
  if(firstArray[r][33]== true) { 
    var  rrr= rr + r
    calcSheet.deleteRow(rrr +1);
    rr--
  }
}     

Upvotes: 0

Views: 2344

Answers (1)

Serge insas
Serge insas

Reputation: 46802

The sheet row corresponding to this array row is r+1 if your array is really complete, ie starting on row1 (I suppose this first row is containing some headers and that's why you iterate from 1 in the array).

But there will be another side effect if you delete the row : the array will not be the exact mirror of the sheet anymore (it will be larger by 1 row each time you delete a row).

there are many ways to workaround this issue, let us know if you want some tips.

Upvotes: 2

Related Questions