簡體   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