Google Script version of VLookup (More Efficient Method?)

后端 未结 2 955
自闭症患者
自闭症患者 2021-01-29 06:11

I\'m trying to put together a function that will allow me to pull a column\'s info from one sheet to another based on a key column. This would work similar to an index match or

2条回答
  •  自闭症患者
    2021-01-29 06:34

    Unfortunately I am afraid there is no built-in function for this in Apps Script.

    However, I have tried your custom function with 100 values and it took <3 seconds to run. I also ran it with 1000 values and my running time was around 40 seconds. It is not ideal but it works consistently. Here is the code I have used:

        var sheet = SpreadsheetApp.getActiveSpreadsheet();
        var importFromSht = SpreadsheetApp.getActive().getSheetByName('Sheet1'); //sheet we are grabbing the values of
        var importToSht = SpreadsheetApp.getActive().getSheetByName('Sheet2');  //sheet we are pasting our values
        var importFromCompCol = 2; // Column (number) that has values to match on.
        var importToCompCol = 2; // Column (number) that has values to match on.
        var importFromCol = 1; // Column (number) that contains value that needs to be copied.
        var importToCol = 1; // Column (number) to copy value to.
        
        function customVlookup (){
          var lastImportFromRN = importFromSht.getLastRow();
          var lastImportToRN = importToSht.getLastRow();
          var importFromCompArr =  importFromSht.getRange(1, importFromCompCol, lastImportFromRN, 1).getValues();
          var importToCompArr =  importToSht.getRange(1, importToCompCol, lastImportToRN, 1).getValues();
          var importFromArr =  importFromSht.getRange(1, importFromCol, lastImportFromRN, 1).getValues();
          var importToArr = [];
        
          for (var i in importToCompArr) {
            for (var j in importFromCompArr){
              if (importToCompArr[i].toString() == importFromCompArr[j].toString()) { 
                importToArr.push(importFromArr[j]);
              }
            }
          }
          //Paste to column
          importToSht.getRange(1,importToCol,importToArr.length,1).setValues(importToArr);
        }

    A different and more efficient approach is to use IMPORTRANGE(URL of first sheet) to an intermediate sheet swapping the columns of the first sheet to then do a VLOOKUP on your second sheet. This would be way more efficient than doing it in Apps Script as it does not run in the memory and you avoid the issue of exceeding the execution time of a custom function.

    In case you want to keep it as an Apps Script custom function Here are some suggestions on the documentation on how to improve your function’s efficiency.

提交回复
热议问题