不替换e.parameter

时间:2012-12-05 00:31:00

标签: google-apps-script

我创建了这个脚本,以便在网格填充信息后发送电子邮件。在发送电子邮件之前,另一个显示屏会警告用户仅在信息正确时才能继续。最后,脚本会向我发送一封电子邮件,其中包含用户输入的信息。问题在于,当我收到电子邮件时,脚本要替换的字段显示为未定义且没有信息。关于这里有什么问题的任何想法? 谢谢!

function runmyapp() {
 var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Title 1');
  var grid = app.createGrid(4, 5);
  grid.setWidget(0, 0, app.createLabel('Time '));
  grid.setWidget(0, 1, app.createTextBox().setName('Time'));
  grid.setWidget(1, 0, app.createLabel('Minutes'));
  grid.setWidget(1, 1, app.createTextBox().setName('Minutes'));
  grid.setWidget(2, 0, app.createLabel('Enter Name'));
  grid.setWidget(2, 1, app.createTextBox().setName('Name'));
  grid.setWidget(3, 0, app.createLabel('Email'));
  grid.setWidget(3, 1, app.createTextBox().setName('email'));
  var panel = app.createVerticalPanel();
  panel.add(grid);

  var button = app.createButton('Submit').setId("button");
  var handler2 = app.createServerHandler('dis');
  handler2.addCallbackElement(grid);
  button.addClickHandler(handler2); 
  var handler = app.createServerHandler('disc');
  handler.addCallbackElement(grid);
  button.addClickHandler(handler);   

  // Add the button to the panel and the panel to the application, then display the                              application app
    panel.add(button);
    app.add(panel);
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);
  }

function dis(e){
 var app = UiApp.getActiveApplication();
   app.getElementById("button").setText("Request is in process, please     wait!").setEnabled(false);
   return app;
   }; 

function disc(e){
   var app = UiApp.getActiveApplication();
   var html1 = app.add(app.createHTML("<p><p>Hello Expert,</p>"+
"<p>By clicking OK you agree that your information is correct</p>");
    var button = app.createButton('Ok').setId("button");
    app.add(button);
    var handler2 = app.createServerHandler('gsnot');
    button.addClickHandler(handler2); 
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);
  return app;}




  function gsnot(e) {
    var advancedArgs = {bcc:e.parameter.email};
    var emailSubject = "Subject";
    var address ="albdominguez25@gmail.com";

    var emailTemplate     =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A1").getValue(    ) ; 
    emailTemplate = emailTemplate.replace("TIME", e.parameter.Times).replace("MIN",     e.parameter.Minutes).replace("EXP", e.parameter.Name);   

    MailApp.sendEmail(address, emailSubject, emailTemplate, advancedArgs);

    Browser.msgBox("Your Email has been sent!");

    var app = UiApp.getActiveApplication();
    app.close();
    // The following line is REQUIRED for the widget to actually close.
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);
  }

2 个答案:

答案 0 :(得分:1)

要在e.parameter上显示窗口小部件值,您需要在处理程序中将其作为callback element添加(或父窗格)。像这样:

function runmyapp() {
  //...
  var grid = app.createGrid(4, 5).setId('grid');
  //...
}

function disc(e){
  //...
  var grid = app.getElementById('grid');
  var handler2 = app.createServerHandler('gsnot').addCallbackElement(grid);
  //...
}

答案 1 :(得分:1)

请将Henrique的答案保持为最佳答案。 您可以尝试更改disc(e)功能,如下所示:

function disc(e){
   var app = UiApp.getActiveApplication();
   var html1 = app.add(app.createHTML("<p><p>Hello Expert,</p>"+
"<p>By clicking OK you agree that your information is correct</p>"));
    var grid = app.getElementById('grid')
    var button = app.createButton('Ok').setId("button");
    app.add(button);
    var handler3 = app.createServerHandler('gsnot');// use a different name to avoid confusion
    handler3.addCallbackElement(grid);
    button.addClickHandler(handler3); 
  return app;}

此外,您的代码中存在拼写错误:

emailTemplate = emailTemplate.replace("TIME", e.parameter.Times).replace("MIN",     e.parameter.Minutes).replace("EXP", e.parameter.Name);   

Time在原始名称末尾没有's'! ;)

,最后一点,如果我不在show(app)中调用disc,那么UI会在确认时保持数据验证...(但这是一个选择问题; - )