1

我有这个 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

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

4

1 回答 1

7

您显示的代码片段用于文档嵌入式 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)};
  ...
于 2013-11-01T17:38:21.873 回答