javascript - Google Sheets Delete Row if value in a column on one tab to not match a column on other -
i trying make script in google sheets check values in column on 1 tab against values in column in tab.
for example; have list allowed e-mail addresses in 1 tab ("users") , then, form have entering addresses in other tab ("form1").
if addresses form not in allow list, automatically delete row.
i think main problem code here.
if( entryvalues[i] === emailsallowed[t] )
even tho values in arrays match never become true.
function onopen() { var sheet = spreadsheetapp.getactivespreadsheet(); var s = sheet.getsheetbyname('form1'); //get sheet entered records in it. var s2 = sheet.getsheetbyname('users');//get sheet allow list in it. var entryvalues = s.getrange('c2:c9').getvalues(); //(get list of e-mail addresses entered via form (using cell reference)) var emailsallowed = s2.getrange('a2:a6').getvalues(); //(get list of allowed e-mail addresses (using cell reference)) var isallowed = 0; for(var = entryvalues.length-1; > 1 ; i--) // work bottom when delete row not miss other rows or delete row should not. { var valuee = entryvalues[i] // test variable see value (can delete line) for(var t = 0; t < emailsallowed.length; t++) //loop through records in allow list { var valuea = emailsallowed[t] // test variable see value (can delete line) if( entryvalues[i] === emailsallowed[t] ) // check see if there match between entered , in allow list. //!!! reason line never true !!!// { isallowed = 1;} //if there set variable isallowed 1 (to indicate there match) } if( isallowed === 1 ) {isallowed = 0; }// if there match set isallowed 0, ready next run else { s.deleterows(i) //if there no match (and isallowed not set) delete invalid row. } } }
thanks
update: here working code: (sandy good)
function onopen() { var sheet = spreadsheetapp.getactivespreadsheet(); var s = sheet.getsheetbyname('form1'); //get sheet entered records in it. var s2 = sheet.getsheetbyname('users');//get sheet allow list in it. var entryvalues = s.getrange('c2:c9').getvalues().join().split(","); //(get list of e-mail addresses entered via form (using cell reference)) var emailsallowed = s2.getrange('a2:a6').getvalues().join().split(","); //(get list of allowed e-mail addresses (using cell reference)) var isallowed = 0; // create variable isallowed , set 0 (this used leave row if matches) for(var = entryvalues.length-1; > 1 ; i--) // work bottom when delete row not miss other rows or delete row should not. { var valuee = entryvalues[i] // test variable see value (can delete line) for(var t = 0; t < emailsallowed.length; t++) //loop through records in allow list { var valuea = emailsallowed[t] // test variable see value (can delete line) if( entryvalues[i] === emailsallowed[t] ) // check see if there match between entered , in allow list. //!!! reason line never true !!!// { isallowed = 1;} //if there set variable isallowed 1 (to indicate there match) } if( isallowed === 1 ) {isallowed = 0; }// if there match set isallowed 0, ready next run else { s.deleterow(i+2) //if there no match (and isallowed not set) delete invalid row. (+ 2 because rows start 1 , have heading (the array starts 0)) } } }
the getvalues()
method returns 2 dimensional array. not getting inner array value. code not comparing value value, it's comparing array array. inner array has 1 value in it, it's still array.
you need values out of 2 dimensional array. code looks this:
var entryvalues = s.getrange('c2:c9').getvalues(); var emailsallowed = s2.getrange('a2:a6').getvalues();
to convert 2d array regular array, this:
var entryvalues = s.getrange('c2:c9').getvalues().join().split(","); var emailsallowed = s2.getrange('a2:a6').getvalues().join().split(",");
this method can used because there 1 value in each inner array, , because getting 1 column. if getting multiple columns, technique not work.
twodarray = [ [innervalue], [innervaluetwo], [inner3] ]
Comments
Post a Comment