[英]Google Sheets: indexOf() doesn't accept string object
I'm trying to filter CSV data by comparing it to an existing google sheet. 我正在尝试通过将CSV数据与现有的Google表格进行比较来过滤CSV数据。 This is the code:
这是代码:
var ss = SpreadsheetApp.getActive();
// get booking ID
var idList=ss.getSheetByName("Umsatzliste").getRange("G:G").getValues();
var filteredCSV=[];
for ( var i=0, lenCsv=csvData.length; i<lenCsv; i++ ){
if (idList.indexOf(csvData[i][6].toString())!=-1) {
filteredCSV.push(csvData[i]);
}
}
csvData=filteredCSV;
The indexOf()-function never seems to work out. indexOf()函数似乎从未奏效。 csvData is a 2d-array with all csv-values:
csvData是具有所有csv值的二维数组:
ss.toast("ID#1: "+idList[0]+" ID#2: "+csvData[2349][6]+" - "+(idList[0]==csvData[2349][6]).toString());
returns 退货
ID#1: MC/000002674 ID#2: MC/000002674 - false
Alright, the typeof reveals they are both "objects", so i try convert the csvData to a string value: 好的,typeof揭示了它们都是“对象”,因此我尝试将csvData转换为字符串值:
ss.toast("ID#1: "+idList[0]+" ID#2: "+csvData[2349][6]+" - "+(idList[0]==csvData[2349][6].toString()).toString());
which returns: 返回:
ID#1: MC/000002674 ID#2: MC/000002674 - true
So a comparison works. 这样比较就可以了。 Any idea why indexOf() doesn't work?
知道为什么indexOf()不起作用吗?
The data idList
retrieved by getValues()
is 2 dimensional array. 由
getValues()
检索的数据idList
是二维数组。 indexOf()
cannot search the string from 2 dimensional array. indexOf()
无法从二维数组中搜索字符串。 So how about the following modification? 那么下面的修改呢?
if (idList.indexOf(csvData[i][6].toString())!=-1) {
if (Array.prototype.concat.apply([], idList).indexOf(csvData[i][6].toString())!=-1) {
Array.prototype.concat.apply([], idList)
, 2 dimensional array is converted to 1 dimensional array. Array.prototype.concat.apply([], idList)
,将二维数组转换为一维数组。 If I misunderstand your question, I'm sorry. 如果我误解了您的问题,对不起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.