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.
Sample Data:
What I've tried:
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);
}
Parameters Defined
- importFromSht - Sheet we are grabbing values from.
- importToSht - Sheet values will be copied to.
- importFromCompCol - Column (number) that has values to match on.
- importToCompCol - Column (number) that has values to match on.
- importFromCol - Column (number) that contains value that needs to be copied.
- importToCol - Column (number) to copy value to.
Requirements:
- 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.
- Would like to call it as a function as I can see myself using this in other areas of this projects and other projects.