Google Apps脚本停止运行功能

时间:2013-01-09 20:26:33

标签: google-apps-script

更新:添加了doGet功能。

我有一个用于注册过程的网络应用程序。我创建了一封电子邮件确认,在用户点击“注册”按钮时向其发送电子邮件。

我的问题是,如果电子邮件字段留空,如何停止运行该功能?此时,当我运行整个脚本时,如果我没有输入电子邮件地址,当我点击注册时会显示一条错误消息。我不希望函数抛出错误,我希望能够仍然注册。

function doGet(e) {
 var app = UiApp.createApplication().setTitle('Education Registration');
  app.setStyleAttribute("background", "#DBE8C4");
var panel1 = app.createAbsolutePanel().setId('panel1');
  panel1.setHeight(900);
  panel1.setWidth(1500);

  //Register button

var dateSelection = app.createButton('Register').setSize(140, 40);
var loadingWait = app.createLabel('After clicking Register, please allow 5 - 30 seconds for the webpage to process the request.');
var clickHandler = app.createServerHandler("respondToDateSelection");
  dateSelection.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel1);

//Email Handler
var emailHandler = app.createServerHandler("emailConfirmation");
  dateSelection.addClickHandler(emailHandler);
  emailHandler.addCallbackElement(panel1);

  return app;

}


function emailConfirmation(e) {
 var app = UiApp.getActiveApplication();

  app.getElementById('fNameText1');
  app.getElementById('lNameText1');
  app.getElementById('eAddressText1');
  app.getElementById('dataItemsLB');
  app.getElementById('aemailAddressText');

  var fNameText1 = e.parameter.fNameText1;
  var lNameText1 = e.parameter.lNameText1;
  var eAddressText1 = e.parameter.eAddressText1;
  var dataItemsLB = e.parameter.dataItemsLB;
  var aemailAddressText = e.parameter.aemailAddressText;

  var subject = "Class Registration Confirmation - " + fNameText1 + " " + lNameText1;
  var emailBody = "This is an Email Confirmation.";

            MailApp.sendEmail(eAddressText1, subject,
                 emailBody, {cc: aemailAddressText});


  return app;

}

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,最“优雅”的方式可能是在你的doGet函数中使用client handler validator,但你没有显示它...(有一个特定的电子邮件验证器) 另一种方法是在UI中创建一个警告标签,该警告标签最初是不可见的,当e.parameter.eAddressText1不是有效电子邮件或为空时可见,相同的条件将适用于sendEmail命令并跳过它如果无效,则返回UI。

随时发布您的doGet功能,以便更准确地回答。


编辑: 感谢您发布您的代码,虽然它不完整,需要一些工作才能使它成为一个有趣的示例,我最终使用此代码来说明使用clientHandlers,验证器和try / catch进行电子邮件地址...

以下是代码,我将其更改为在电子表格上工作以避免部署和版本控制,这只是一个测试,仅此而已......

function doGet(e) {
 var app = UiApp.createApplication().setTitle('Education Registration');
  app.setStyleAttribute("background", "#DBE8C4");
  var panel0 = app.createFlowPanel().setId('panel0');
  var panel1 = app.createVerticalPanel().setId('panel1');
  var fNameText1=app.createTextBox().setName('fNameText1').setId('fNameText1');;
  var lNameText1=app.createTextBox().setName('lNameText1').setId('lNameText1');;
  var eAddressText1=app.createTextBox().setName('eAddressText1').setId('eAddressText1').setText('mail')
  var dataItemsLB=app.createTextBox().setName('dataItemsLB').setId('dataItemsLB');;
  var aemailAddressText=app.createTextBox().setName('aemailAddressText').setId('aemailAddressText').setText('mail');
  panel1.add(fNameText1).add(lNameText1).add(eAddressText1).add(dataItemsLB).add(aemailAddressText)
  panel0.add(panel1)

//Register button
  var dateSelection = app.createButton('Register').setSize(140, 40).setId('dateSelection');
  var loadingWait = app.createLabel('After clicking Register, please allow 5 - 30 seconds for the webpage to process the request.').setVisible(false).setId('loadingWait');
  var clickHandler = app.createServerHandler("respondToDateSelection").validateEmail(eAddressText1);
  dateSelection.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel0);

//Email Handler
  var emailHandler = app.createServerHandler("emailConfirmation").validateEmail(eAddressText1);
  dateSelection.addClickHandler(emailHandler);
  emailHandler.addCallbackElement(panel1);

//client handlers
  var warning = app.createLabel('Please enter your email where necessary').setId('warning').setVisible(false).setStyleAttribute('background','yellow')
  var clientHandlerwait = app.createClientHandler().forTargets(loadingWait).setVisible(true).validateEmail(eAddressText1)
  var clientHandler1 = app.createClientHandler().validateNotEmail(eAddressText1)
    .forTargets(warning).setVisible(true).forEventSource().setStyleAttribute('color','red')
  var clientHandler2 = app.createClientHandler().validateNotEmail(aemailAddressText)
    .forTargets(warning).setVisible(true).forEventSource().setStyleAttribute('color','red')

  dateSelection.addClickHandler(clientHandlerwait).addClickHandler(clientHandler1).addClickHandler(clientHandler2)

  app.add(panel1.add(dateSelection).add(loadingWait).add(warning))
  SpreadsheetApp.getActive().show(app)
//  return app;
}

function respondToDateSelection(){
return
}

 function emailConfirmation(e) {
  var app = UiApp.getActiveApplication();
   app.getElementById('warning').setVisible(false);
   app.getElementById('dateSelection').setStyleAttribute('color','black')

  var fNameText1 = e.parameter.fNameText1;
  var lNameText1 = e.parameter.lNameText1;
  var eAddressText1 = e.parameter.eAddressText1;
  var dataItemsLB = e.parameter.dataItemsLB;
  var aemailAddressText = e.parameter.aemailAddressText;
  var subject = "Class Registration Confirmation - " + fNameText1 + " " + lNameText1;
  var emailBody = "This is an Email Confirmation.";
    try{
//            MailApp.sendEmail(eAddressText1, subject,emailBody, {cc: aemailAddressText});
        Utilities.sleep(500)// simulate a duration to read the message
        app.getElementById('loadingWait').setText('mail sent').setVisible(true)
}catch(err){
        app.getElementById('loadingWait').setText('error sending mail').setVisible(true)
}
  return app;
}