繁体   English   中英

如何在 Google Apps Script 电子表格嵌入脚本中使用确认按钮?

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

我有这个 Google Apps 脚本,可以向我在电子表格中选择的人发送一封包含请求的电子邮件:

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);
  }

而且,在发送电子邮件之前,我想要一个确认按钮,如下所示:

 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);
  }

因此,如果用户按 OK,应用程序将执行MailApp.sendEmail(email2Send, title, message); 并发送电子邮件。

我不得不承认我的无知。 我正在阅读有关处理程序的“Google Apps Script”(Oreilly,James Ferreira 着)一书的第 4 章。 我尝试使用 Google 文档中提供的示例(已删除代码!)。 但是我遇到了一个我无法理解的错误。

使用的代码是这个示例:

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

我对这个简单的项目有一些紧迫感,所以请原谅我在进一步研究答案之前提出这个问题(我正在寻找答案,同时等待答案)。 那么,如何在此代码中使用确认/取消按钮?

您展示的代码片段用于文档嵌入式 UI,电子表格上下文的等效(好吧...几乎)类是Browser.MsgBox(prompt,buttons) ,请参阅此处的文档,它比创建 Ui + 处理程序函数要简单...即使布局和外观相当基本,它也很容易和高效。

在您的代码中,它变为:

  ...
  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)};
  ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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