[英]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:我有这个 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);
}
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);
因此,如果用户按 OK,应用程序将执行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.我正在阅读有关处理程序的“Google Apps Script”(Oreilly,James Ferreira 着)一书的第 4 章。 I've tried using an example provided in the documentation from Google (already deleted the code!).我尝试使用 Google 文档中提供的示例(已删除代码!)。 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.您展示的代码片段用于文档嵌入式 UI,电子表格上下文的等效(好吧...几乎)类是Browser.MsgBox(prompt,buttons)
,请参阅此处的文档,它比创建 Ui + 处理程序函数要简单...即使布局和外观相当基本,它也很容易和高效。
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)};
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.