Reputation: 43
I am trying to use IndexOf and it keeps returning -1 (no match) when the value is in the array. I am new to this so I am not sure why it won't work. my end goal is to return the row number of the first instance of the matching data.
var lookup_array = sales_sheet.getRange(1,1,3,1).getValues();
var index = lookup_array.indexOf(newsheet_name);
lookup_array = [[account_manager], [john doe], [john doe]]
newsheet_name = john doe
index = -1
I would expect value to = 1 since john doe is the 2nd value in the array.
Upvotes: 0
Views: 68
Reputation: 201388
Values retrieved by getValues()
is 2 dimensional array. So in order to use indexOf()
, please modify as follows. Please think of this as just one of several answers.
In this modification, 2 dimensional array is converted to 1 dimensional array by map()
, and indexOf()
is used.
var newsheet_name = "john doe";
var lookup_array = sales_sheet.getRange(1,1,3,1).getValues(); // [["account_manager"], ["john doe"], ["john doe"]]
var res = lookup_array.map(function(e) {return e[0]}).indexOf(newsheet_name);
Logger.log(res) // 1
Upvotes: 1