javascript - Changing script to send non-duplicate values to a new sheet instead of duplicate values -
i have script iterates through 2 spreadsheets, finds duplicate values in column a, appends row of these duplicate values sheet.
i want script similar thing, append rows not duplicates instead of ones are. how can alter sends non duplicates "new students" sheet"? tried changing == !==. sends whole list. have been searching around while , know it's easy fix.
much! brandon
function compareandupdate() { var s1 = spreadsheetapp.openbyid("xxxxxx-vvxyewdki0j9tgqjl9_f-wze0zoboqrisclaa").getsheetbyname('updated student list'); var s2 = spreadsheetapp.openbyid("xxxxxx-yb9xm1j5rkws7nf23vd-ntueigspbctj3lew4").getsheetbyname('master student list'); var s3 = spreadsheetapp.openbyid("xxxxxx-yb9xm1j5rkws7nf23vd-ntueigspbctj3lew4").getsheetbyname('new students'); var values1 = s1.getdatarange().getvalues(); var values2 = s2.getdatarange().getvalues(); var resultarray = []; for(var n=0; n < values1.length ; n++){ var keep = false; for(var p=0; p < values2.length ; p++){ logger.log(values1[n][0]+' =? '+values2[p][0]); if( values1[n][0] == values2[p][0]){ resultarray.push(values1[n]); logger.log('true'); break ;// remove if values not unique , want keep occurrences... } } } s3.getrange(+1,1,resultarray.length,resultarray[0].length).setvalues(resultarray); }
if prefer keep existing structure (which think wrote time ago ;-) , btw has unnecessary variable (keep)!) can use modified version of code :
function compareandupdate() { var s1 = spreadsheetapp.openbyid("xxxxxx-vvxyewdki0j9tgqjl9_f-wze0zoboqrisclaa").getsheetbyname('updated student list'); var s2 = spreadsheetapp.openbyid("xxxxxx-yb9xm1j5rkws7nf23vd-ntueigspbctj3lew4").getsheetbyname('master student list'); var s3 = spreadsheetapp.openbyid("xxxxxx-yb9xm1j5rkws7nf23vd-ntueigspbctj3lew4").getsheetbyname('new students'); // var definition easier testing purpose // var s1 = spreadsheetapp.getactivespreadsheet().getsheets()[0]; // var s2 = spreadsheetapp.getactivespreadsheet().getsheets()[1]; // var s3 = spreadsheetapp.getactivespreadsheet().getsheets()[2]; var values1 = s1.getdatarange().getvalues(); var values2 = s2.getdatarange().getvalues(); var resultarray = []; for(var n=0; n < values1.length ; n++){ var keep = true; for(var p=0; p < values2.length ; p++){ logger.log(values1[n][0]+' =? '+values2[p][0]); if( values1[n][0] == values2[p][0]){ keep = false;// if same value in first column in s2 don't keep (you can modify condition needs, extend whole row or other columns example ) break; } } if(keep){ resultarray.push(values1[n]);// keep values s1 not in s2 } } s3.getrange(+1,1,resultarray.length,resultarray[0].length).setvalues(resultarray); }
Comments
Post a Comment