0

I currently have code that searches through a single Google sheet for indexOf a number, and assigns the row number of a match to an variable. Here's an example:

for(i=0;i<values.length; i++){
  itemvalue = values[i][column1-1];
  codevalue = values[i][column2-1];

  if(itemvalue.indexOf("5019")>-1){
    row = i+1; itemcode = 5019; indexes.push(i+1)
  }
}

Unfortunately the sheet contains multiple rows of strings that contain "5019". So I'm looking for a way for indexOf to continue searching and assign a new variable for the row number of each match. So if "5019" was found at row 50,51, and 54, then row,row1,row2 will be assigned to each row number.

Appreciate any help on this, also please let me know if I missed providing any information.

Edit:Thanks to the link provided by daniel, I was able to get all the matching row numbers into an array. But I'm still trying to figure out how to assign a variable to each value in the array so I can get the value of the cells that reside at the rows and add them together.

Sean W
  • 377
  • 1
  • 5
  • 18
  • 1
    Does this work for you? http://stackoverflow.com/questions/20798477/how-to-find-index-of-all-occurrences-of-an-element-in-array – daniel Mar 09 '16 at 02:15
  • Use an array and objects. Your objects will have one property, {row: 50} - push the object into the array and then you can read your data by looping through the array and checking what the row values are. – AntonB Mar 09 '16 at 03:14
  • Would it be possible to see an example of how this would work? I've read on how to create objects and I'm able to get the row numbers I needed into an array. – Sean W Mar 12 '16 at 19:22

1 Answers1

0

I've found the solution I sought for. Here's what I did:

for(i=0;i<values.length; i++){
itemvalue = values[i][column1-1];
codevalue = values[i][column2-1];

if(itemvalue.indexOf("5019")>-1){
  row = i+1; itemcode = 5019; indexes.push(i+1)
}
for(j=k=0;j<indexes.length; j++){
rownumb.push(ss.getRange(indexes[j],qtycol).getValue()); //Get value of the cells using the row numbers from the indexes array
}
for(l=0;l<rownumb.length; l++){
rowtotal += rownumb[l]; //Adds all the values together
} 
}

So I made a for loop to get the value of cells in the rows that was in the indexes array, then made a new array called "rownumb" that holds the cell values. I then made another for loop to add all the values in array rownumb together and got exactly what I was looking for.

Sean W
  • 377
  • 1
  • 5
  • 18