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

Time:11-25

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.

CodePudding user response:

This:

if(Browser.Buttons.CANCEL = true) {
    Browser.msgBox('The operation has been cancelled')
    return;
  }

should be:

if(Browser.Buttons.CANCEL == true) {
    Browser.msgBox('The operation has been cancelled')
    return;
  }

= is an assignment

== is a comparison

and try this:

function sendEmails() {
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName("Your Sheet Name")
  const sheetData = sh.getDataRange().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
}

CodePudding user response:

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.

  • Related