I'm trying to filter CSV data by comparing it to an existing google sheet. 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. csvData is a 2d-array with all csv-values:
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:
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?
The data idList
retrieved by getValues()
is 2 dimensional array. indexOf()
cannot search the string from 2 dimensional array. 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. If I misunderstand your question, I'm sorry.
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.