简体   繁体   中英

How to use a confirmation button in Google Apps Script spreadsheet embedded scripts?

I have this Google Apps Script to send an email with a request to people I choose in a spreadsheet:

function sendRequestEmail() {
  var data = SpreadsheetApp.openById(SPREADSHEET);
  if(!employee_ID) {
    employee_ID = getCurrentRow();
    if (employee_ID == 1) {
      var employee_ID = Browser.inputBox("Você precisa selecionar um assistido?", "Choose a row or type its number here:", Browser.Buttons.OK_CANCEL);
    }
  }

  // Fetch variable names
  // they are column names in the spreadsheet
  var sheet = data.getSheets()[0];
  var columns = getRowAsArray(sheet, 1);
  Logger.log("Processing columns =" + columns);
  var employeeData = getRowAsArray(sheet, employee_ID); 
  Logger.log("Processing employeeData = " + employeeData);
  // Assume first column holds the name of the person
  var email2Send = "pythonist@example.com";
  var title = "Request by email";
  var name = employeeData[0];  
  var mother_name = employeeData[1];
  var message = "Hi, I have a request for you, " + name + ", this is... example";
  // HERE THE
  // CONFIRMATION BUTTON!!!                     
  MailApp.sendEmail(email2Send, title, message);
  }

And, before sending the email, I want a confirmation button, something like this:

 function showConfirmation(name, email2Send) { 
  var app = UiApp.createApplication().setHeight(150).setWidth(250);
  var msg = "Do you confirm the request to " + email2Send + " about " + name + "?";
  app.setTitle("Confirmation of request");      
  app.add(app.createVerticalPanel().add(app.createLabel(msg)));
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
  }

So, if user press OK, the app will execute the line MailApp.sendEmail(email2Send, title, message); and send an e-mail.

I have to admit my ignorance. I'm reading chapter 4 of the book "Google Apps Script" (Oreilly, by James Ferreira) on handlers. I've tried using an example provided in the documentation from Google (already deleted the code!). But I came across an error that I could not understand.

The code used were this sample:

var ui = DocumentApp.getUi();
var response = ui.prompt('Getting to know you', 'May I know your name?', ui.ButtonSet.YES_NO);
 // Process the user's response.
if (response.getSelectedButton() == ui.Button.YES) ... DO THIS

I have some urgency in this simple project, so forgive-me for asking this question before research more for the answer (I'm searching for it while wating for the answer). So, how can I use a confirmation/cancellation button in this code?

The code snippet you showed is for document embedded UI, the equivalent (well... almost) class for spreadsheet context is Browser.MsgBox(prompt,buttons) , see doc here , it will be simpler than create a Ui + a handler function... even if the layout and appearance are fairly basic it's easy and efficient.

In your code it becomes :

  ...
  var confirm = Browser.msgBox('send confirmation','Are you sure you want to send this mail ?', Browser.Buttons.OK_CANCEL);
  if(confirm=='ok'){ MailApp.sendEmail(email2Send, title, message)};
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM