简体   繁体   中英

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM