title: Google Script version of VLookup (More Efficient Method?)
body: <p>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 vlookup in excel/google. </p>
  
  <p><strong>Sample Data:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/or1vA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/or1vA.png" alt="enter image description here"></a></p>
  
  <p><strong>What I've tried:</strong></p>
  
  <pre><code>function vlookup(importFromSht, importToSht, importFromCompCol, importToCompCol,importFromCol, importToCol){
    var lastImportFromRN = importFromSht.getLastRow();
    var lastImportToRN = importToSht.getLastRow();
    var importFromCompArr =  importFromSht.getRange(2, importFromCompCol, lastImportFromRN, 1).getValues();
    var importToCompArr =  importToSht.getRange(2, importToCompCol, lastImportToRN, 1).getValues();
    var importFromArr =  importFromSht.getRange(2, 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(2,importToCol,importToArr.length,1).setValues(importToArr);
  }
  </code></pre>
  
  <p><strong>Parameters Defined</strong></p>
  
  <ul>
  <li>importFromSht - Sheet we are grabbing values from.</li>
  <li>importToSht - Sheet values will be copied to.</li>
  <li>importFromCompCol - Column (number) that has values to match on.</li>
  <li>importToCompCol - Column (number) that has values to match on.</li>
  <li>importFromCol - Column (number) that contains value that needs to be copied.</li>
  <li>importToCol - Column (number) to copy value to.</li>
  </ul>
  
  <p><strong>Requirements:</strong></p>
  
  <ul>
  <li>There are over 6K rows minimum and could be many more thousand. Speed of execution is important. Is my approach the correct approach or are there more efficient methods? My script added about 30 seconds to my execution time.</li>
  <li>Would like to call it as a function as I can see myself using this in other areas of this projects and other projects.</li>
  </ul>