datafarmer
datafarmer

Reputation: 43

Google Sheets Ap-script Indexof

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

Answers (1)

Tanaike
Tanaike

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.

Modified script:

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

References:

Upvotes: 1

Related Questions