Matching values in two diffent sheets to fetch a third [google-apps-script] -
i trying match names 1 sheet names a reference data sheet pull corresponding emails new sheet. have tried following code no success. manage ski club members have same last names, , vlookup stops @ first match. need confirm both first , last name in order fetch appropriate email.
the code have tried follows;
function getemail() { var ss = spreadsheetapp.getactivespreadsheet() var sheetroster = ss.getsheets()[0]; var sheettrip = ss.getsheets()[1]; var startrow = 2; var tripdata = sheettrip.getrange(startrow, 2, sheettrip.getlastrow()); var rosterdata = sheetroster.getrange(startrow,2,sheetroster.getlastrow()); (var = 0; < rosterdata.length; i++) { (var k = 0; k < tripdata.length; k++) { if((tripdata[k][1] == rosterdata[i][1])&&(tripdata[k][0] == rosterdata[i][0])){ tripdata[k][2] = (rosterdata[i][2]); } } } }
the lines of code data, missing getvalues()
method. change:
var tripdata = sheettrip.getrange(startrow, 2, sheettrip.getlastrow()); var rosterdata = sheetroster.getrange(startrow,2,sheetroster.getlastrow());
to:
var tripdata = sheettrip.getrange(startrow, 1, sheettrip.getlastrow()-1,2).getvalues(); var rosterdata = sheetroster.getrange(startrow,1,sheetroster.getlastrow()-1,2).getvalues();
also note, need 4 parameters. need both columns of data, need fourth parameter number 2, 2 columns of data. , start in column 1. second parameter needs changed 1.
to see results, use logger.log('tripdata: ' + tripdata);
statement, run code, , in view menu, choose logs.
also, subtract 1 row length avoid getting blank value in array @ end, since starting in row 2.
actually, need 3 columns roster, because need retrieve email. , loop through trip data in outer loop. here code looking email , adding it. note it's case sensitive, didn't match first names starting lower case letters.
function getemail() { var ss = spreadsheetapp.getactivespreadsheet() var sheetroster = ss.getsheets()[0]; var sheettrip = ss.getsheets()[1]; var startrow = 2; var tripdata = sheettrip.getrange(startrow, 1, sheettrip.getlastrow()-1,2).getvalues(); var rosterdata = sheetroster.getrange(startrow,1,sheetroster.getlastrow()-1,3).getvalues();//get 2 columns of data first , last name var firstname, lastname; logger.log('rosterdata: ' + rosterdata); (var = 0; < tripdata.length; i++) { firstname = tripdata[i][0]; lastname = tripdata[i][1]; logger.log(firstname + " " + lastname); (var k = 0; k < rosterdata.length; k++) { if ((firstname == rosterdata[k][0]) && (lastname == rosterdata[k][1])) { sheettrip.getrange(k+2, 3).setvalue(rosterdata[i][2]) }; }; }; };
Comments
Post a Comment