I have a script that compares values in cells in two different sheets on a google spreadsheet. It can successfully match find the matches and copy them to a 3rd sheet. However, I want to expand the selection to the column left of the matches and copy over both the matches and adjacent cells.
For clarification: Spreadsheet 1 has emails and majors, spreadsheet 2 has majors under what college they belong to. I am matching the majors so I can get the emails of only the people in a specific college. Right now all it copies are the cells with the major, but I would like it to copy the cells with the email as well (which are one column to the left).
function match_CoE() {
//Set variables for active spreadsheet, input data, comparitive data, and finalized match input.
var sh = SpreadsheetApp.getActive();
var input = sh.getSheetByName('AOP 2018-2019').getRange("C1:C").getValues();
var values = sh.getSheetByName('Major-College-Conversion').getRange("A72:A75").getValues();
var output = sh.getSheetByName('College of Education Contacts');
var match = [];
//Compare Input to Pre-determined values for matches
for (i in input){
var setInput = input[i][0];
var exists = false;
for (j in values){
var setValues = values[j][0];
if (setValues == setInput){
exists = true;
break;
}
} // end for j
if (exists == true){
match.push([setInput])
}
}//end for i
//Copy matching values to new sheet.
output.getRange(1, 1, match.length, 1).setValues(match);
}
var mail = sh.getSheetByName('AOP 2018-2019').getRange("D1:D").getValues();, push its value into another array whenexist == true, and populate its value inoutputthe same way you do withmatch? - HaPhanArray#filter. Note that right now you have a nested loop - for every element ofinput, you rescan potentially all ofvalues. See my answers here, here, and here for examples that optimize similar tasks. Once you've edited your code to attempt to solve your task, I or others will be happy to help fix the code you've written (vs. just doing it all for you). - tehhowch