0

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?

clawjelly
  • 35
  • 7

1 Answers1

0

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?

From :

if (idList.indexOf(csvData[i][6].toString())!=-1) {

To :

if (Array.prototype.concat.apply([], idList).indexOf(csvData[i][6].toString())!=-1) {

Note :

  • Using Array.prototype.concat.apply([], idList), 2 dimensional array is converted to 1 dimensional array.

References :

If I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165