Comparison of arrays in Google spreadset

I am trying to write a script that copies data from a "Longlist" sheet to a "Final" sheet (onEdit) if the data in the "Longlist" sheet does not exist in the "Final" sheet.

The code below copies data from sheet to sheet, however, when the function starts again, it copies the data even in the final sheet and creates duplicate rows.

I would be glad if anyone could help.

Note: Google sheet does not support - data [i] .equals (data2 [z]) or vlookup inside script

function onEdit() {
    var ss= SpreadsheetApp.getActive();  
    var sheet = ss.getSheetByName("Longlist New");
    var sheet2 = ss.getSheetByName("Final List");
    var data = sheet.getRange('a1:a100').getValues();  // Long List - Names
    var data2 = sheet2.getRange('c2:c50').getValues(); // Final List - Names
    for (var i=1; i<=data.length; i++)                         {
        for (var z=0; z<=data2.length; z++) {   
            if (data [i] == data2 [z]) {
                x=1;
            } 
        } 
        if (x != 1)    {   
            a= sheet2.getLastRow();
            b= sheet2.getLastColumn(); 
            sheet2.getRange(a+1,12).setValue(sheet.getRange(i+1,9).getValue());   
            ...
            ..
        }
    }
}
+4
source share
1 answer
var dataToCopy = data.reduce (function(a,b)   { return a.concat(b) })
                     .filter (function(e,i,a) { return this.indexOf(e) == -1 },
                        data2.reduce( function(a,b) { return a.concat(b) }))

                     .filter (function(e,i,a) { return a.indexOf(e) === i })
                     .map    (function(e,i,a) { return [e] });

TARGET

, data2.

  • ..reduce .concat "1D" 1D.

  • .filter , 2. data2 thisArg 'this'. "," . data2 ( , 1), ​​ .

    .indexOf data2 -1, . true/false data1, , false. dataToCopy , , data2.

  • dataToCopy. indexOf , . , . dataToCopy. , .

  • ToCopy . .

+1

Source: https://habr.com/ru/post/1610012/


All Articles