简体   繁体   English

Google Sheets脚本索引

[英]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. 我正在尝试使用IndexOf,并且当值在数组中时,它始终返回-1(不匹配)。 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]] lookup_array = [[account_manager],[john doe],[john doe]]

newsheet_name = john doe newsheet_name =约翰·杜

index = -1 指数= -1

I would expect value to = 1 since john doe is the 2nd value in the array. 我希望value = 1,因为john doe是数组中的第二个值。

Values retrieved by getValues() is 2 dimensional array. getValues()检索的值是二维数组。 So in order to use indexOf() , please modify as follows. 因此,为了使用indexOf() ,请进行以下修改。 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. 在此修改中,二维数组通过map()转换为一维数组,并使用indexOf()

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: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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