javascript - How to create an error function that can terminate in Google app script -


in google sheets, i'm trying create checking function google apps script ensure cells correctly filled up, , if they're correctly filled rest of script run update separate table. in code i've written, updater function calls checking function. checking function correctly throws alert when error detected, after alert closed, returns updater function wrongly run update. i'd code stop after error alert acknowledged. doing wrong?

my code (vastly simplified; check many errors):

function updater() {   var sheet = spreadsheetapp.getactivespreadsheet().getsheetbyname('update');   var lower = sheet.getrange(1, 1).getvalue;   var higher = sheet.getrange(2, 1).getvalue;   verify(lower, higher) //check values correctly input   /* run below if no errors */   sheet.getrange(5, 1).setvalue(lower);   sheet.getrange(5, 2).setvalue((lower + higher) / 2);   sheet.getrange(5, 3).setvalue(higher); }  function verify(inlow, inhigh) {   var ui = spreadsheetapp.getui();   var err = 'x';   if (inlow > inhigh) {     err = 'bottom value should lower top value. please re-enter.';   }   if(err == 'x') {     return; //if there no errors, return updater()   } else {     ui.alert(err); //if there's error, show alert. want end here   } } 

you can make verify function return boolean value can use in if condition.

modified code below :

function updater() {   var sheet = spreadsheetapp.getactivespreadsheet().getsheetbyname('update');   var lower = sheet.getrange(1, 1).getvalue;   var higher = sheet.getrange(2, 1).getvalue;   if(verify(lower, higher)){ //check values correctly input   /* run below if no errors */   sheet.getrange(5, 1).setvalue(lower);   sheet.getrange(5, 2).setvalue((lower + higher) / 2);   sheet.getrange(5, 3).setvalue(higher);   } }  function verify(inlow, inhigh) {   var ui = spreadsheetapp.getui();   var err = 'x';   if (inlow > inhigh) {     err = 'bottom value should lower top value. please re-enter.';   }   if(err == 'x') {     return true; //if there no errors, return updater()   } else {     ui.alert(err); //if there's error, show alert. want end here     return false;   } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -