I would like to validate an entry before it adds as a record by checking the student ID in "UserForm" against an array in "SDB" and if it finds a match to pop up with an error. At the moment I can add the same student multiple times which I would like to prevent.
function validateEntry() {
var myGooglSheet= SpreadsheetApp.getActiveSpreadsheet();
var shUserForm = myGooglSheet.getSheetByName("User Form");
var datasheet = myGooglSheet.getSheetByName("SDB");
var ui = SpreadsheetApp.getUi();
//Validating Student Id
if(shUserForm.getRange("S1") = SDB.getRange(A:A)) {
ui.alert("Already Exists.");
shUserForm.getRange("S1").activate();
shUserForm.getRange("S1").setBackground('#FF0000');
return false;
}
return true;
}
Any help would be great!
Edit:Found a workaround for it by checking if the entry exists in google spreadsheets with a formula;
=IFERROR(if(vlookup(S1,SDB!A:A,1,false)=vlookup(S1,SDB!A:A,1,false),"Modify",""),"NEW")
and then running the script to check the formula
//Validating Student Id
if(shUserForm.getRange("O1").getValue()=="Modify") {
ui.alert("Entry Already Exists.");
shUserForm.getRange("O1").activate();
shUserForm.getRange("O1").setBackground('#FF0000');
return false;
}
It does the trick but still a one step method would be ideal!