Conditional (MsgBox response) to run a function or quit

470 views
Skip to first unread message

Robert Alder

unread,
Mar 25, 2023, 1:02:18 PM3/25/23
to Google Apps Script Community
I'm baffled.  My script named "GoodEmailPDF" works perfect when executed on it's own (It is executed from a button on a Google Sheet.  Life is good.

However,, I want to give the user an opportunity to NOT send the email.   So, a Yes_NO  MsgBox was created as follows.  Instead of running  "GoodEmailPDF" script from the button,  the following "PrelimToSending" function  is attached to that button in hopes the user can either  send the email (i.e. run  "GoodEmailPDF") or just quit.  The problem is whether they chose "YES" or "NO" the "GoodEmailPDF" function runs regardless of the users MsgBox choice.    

What the heck is wrong? Thanks for your input.  

function PrelimToSending() {
  var response = Browser.msgBox('Is this License REALLY ready to be emailed? If so, Click YES. If not, click NO to stop it from being emailed.',Browser.Buttons.YES_NO);
if (response = "YES") {
  GoodEmailPDF();
else {
  Logger.log('The user clicked "No" or the dialog\'s close button.');


}

cbmserv...@gmail.com

unread,
Mar 25, 2023, 1:31:22 PM3/25/23
to google-apps-sc...@googlegroups.com

Yes, the problem is how you have setup the conditional check in the if statement.

 

In most languages you can do this:  if (response = “YES”)

 

But in Javascript, you need to use this:  if (response === “YES”)

 

The first case, it just assigns the value “YES” to response.. Which is obviously not what you want.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b6beda22-c081-41e5-be0f-f44a8c566bd3n%40googlegroups.com.

Robert Alder

unread,
Mar 25, 2023, 2:01:43 PM3/25/23
to Google Apps Script Community
Thanks George for responding. As you might tell, I'm not a programmer so your suggestion to use three  "=" signs ("===") in the code is above my pay grade. Didn't make sense to me.  Nevertheless I edited the script to use three   = signs if (response === “YES”) .  Now  either choice (Yes or NO) results in the script simply quitting (what I wanted for else).     Selecting YES doesn't run  the  GoodEmailPDF rountine.   Whereas before with  one equal sign  (if (response = “YES”)) either choice RAN the  GoodEmailPDF.    There's program logic that is obviously  FUBAR which baffles me. . 

cbmserv...@gmail.com

unread,
Mar 25, 2023, 2:11:55 PM3/25/23
to google-apps-sc...@googlegroups.com

Hi Robert,

 

Don’t worry about it some of the language syntax baffles many but we accommodate it because we have to.

 

In your case, I just fixed your conditional check. But I should have checked your logic as well.

 

You should probably use the Google Spreadsheet UI instead of the browser one.

 

Here is how:

 

function PrelimToSending()

{

  var ui = SpreadsheetApp.getUi(); 

  var response = ui.alert(

     'Is this License REALLY ready to be emailed? If so, Click YES. If not, click NO to stop it from being emailed.',

     'Are you sure you want to continue?',

      ui.ButtonSet.YES_NO);

 

  // Process the user's response.

  if (response == ui.Button.YES) {

    // User clicked "Yes".

    GoodEmailPDF();

  } else {

    // User clicked "No" or X in the title bar.

    Logger.log('The user clicked "No" or the dialog\'s close button.');

  }

}

 

 

Robert Alder

unread,
Mar 25, 2023, 2:15:15 PM3/25/23
to Google Apps Script Community
P.S.  Here's the script as it now which responding either YES or NO results in the script  ending  (what I want for Else),  That is neither choice results in  initiating  GoodEmailPDF.  

function PrelimToSending() {
  var response = Browser.msgBox('Is this License REALLY ready to be emailed? If so, Click YES. If not, click NO to stop it from being emailed.',Browser.Buttons.YES_NO);
if (response === "YES") {

cbmserv...@gmail.com

unread,
Mar 25, 2023, 2:20:35 PM3/25/23
to google-apps-sc...@googlegroups.com

If you want to keep the browser msgbox instead, change your conditional check to:

 

  if (response === "yes") {

 

 

You need to use lowercase yes.

Robert Alder

unread,
Mar 25, 2023, 4:07:33 PM3/25/23
to Google Apps Script Community
So near and yet so far.   Thank you so very much!    Who knew (Certainly not this rookie self-taught coder) that it had to be three "=" and then lower case yes) . IT NOW Works.  

CODE:
function PrelimToSending() {
  var response = Browser.msgBox('Is this License REALLY ready to be emailed? If so, Click YES. If not, click NO to stop it from being emailed.',Browser.Buttons.YES_NO);
if (response === "yes") {

CBMServices Web

unread,
Mar 25, 2023, 9:32:33 PM3/25/23
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages