javascriptgoogle-apps-scriptgoogle-sheetscomparison-operators

How to make the cancel Button work in Google App Script Browser Input Box


function sendEmails() {
  const sheet = getSpreadsheetApp()
  const sheetData = getSpreadsheetData(sheet).getValues();
  const subjectline = "Weekly Breakdown for the week of - "
  var subjectDate = Browser.inputBox('Please enter the date range for e-mail subject.', Browser.Buttons.OK_CANCEL);
    if(Browser.Buttons.CANCEL = true) {
        Browser.msgBox('The operation has been cancelled')
        return;
      }

  const subject = subjectline + subjectDate

In the below code, even if I'm clicking OK by inputting the details to the input box,It gives me the same message box and stops running the script.


Solution

  • Browser.inputBox returns a text string. To find whether the Cancel button was pressed, use this:

        if (subjectDate === 'cancel') {
    

    Use Ui.prompt() to better detect what response was entered and which button was pressed.