2013-11-01 25 views
0

我有這樣的谷歌Apps腳本與人的請求,我在電子表格中選擇發送電子郵件:如何使用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 = "[email protected]"; 
    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腳本」(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 

我在這個簡單的項目有些急,所以請原諒,我要問的研究之前,這個問題更加的答案(我在尋找它,而瓦亭爲答案)。那麼,如何在這段代碼中使用確認/取消按鈕?

感謝您的幫助!

回答

2

您顯示的代碼段是文檔嵌入式UI,電子表格上下文的等效(幾乎...)類爲Browser.MsgBox(prompt,buttons),請參閱文檔here,它將比創建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)}; 
    ... 
相關問題