Google UiApp中的Google Apps脚本验证问题

时间:2013-12-02 03:28:33

标签: javascript javascript-events google-apps-script google-sites

这是我在这里发表的第一篇文章,我希望你能够向我展示我做错了什么。我已经连续两天试图在Google协作平台中找出Google Apps脚本验证,但尚未使验证部分正常运行。

我需要验证两件事:

  1. “位置”文本框中至少有5个字符(最多100个)
  2. 已从下拉列表中选择日期
  3. 如果两个条件都不满足,那么它应该显示两件事:

    1. warnException
    2. warnExceptionMes
    3. 就是这样。

      我的其余逻辑工作得很好。我刚刚开始,所以请不要打扰我;-)

      真的坚持这个,真的可以在这里使用一些帮助。下面列出了完整的逻辑。我已将域名信息替换为xxxxxxxxx

      到目前为止,它要么从不显示消息,也不做任何事情,要么只是让其中一个项目正确,就可以向前推进。它们都应该满足要求或者应该抛出warnExceptions。如果用户加载页面并且没有填充任何/或区域并且只是按下按钮,这也是相同的。

      有人可以告诉我如何验证位置文本框和日期框吗?

      主要提前感谢!

      var templateIDToCopy = 'xxxxxxxxxx';
      var folderIDtoCopyTo = 'xxxxxxxxxx';
      var councilMembers = ['Unknown','Name 1','Name 2'];
      
      function doGet(e) {
        var text= new Array();
        var app = UiApp.createApplication();
        var hpanel = app.createGrid(4, 6).setId('pannel');
        var hpanelException = app.createGrid(2,3).setId('hpanelException');
        var location = app.createTextBox().setName('location').setId("location").setWidth('200');
        var minuteTaker = app.createListBox().setName('minuteTaker').setId("minuteTaker").setWidth('200')
         for (var i = 0 ; i < councilMembers.length; i++) {
          minuteTaker.addItem(councilMembers.valueOf()[i]);
                          }
      
        var dateBox = app.createDateBox().setId('dateBox').setName('dateBox').setFireEventsForInvalid(false);
        var hour = app.createListBox(false).setId('hour').setName('hour')
      //  var hour = app.createListBox(false).setId('hour').setName('hour')
        for(h=1;h<13;++h){hour.addItem(h)}
        var min = app.createListBox(false).setId('minute').setName('minute')
        .addItem('00').addItem('15').addItem('30').addItem('45'); 
        var amPm = app.createListBox(false).setId('am').setName('amPm')
        .addItem('AM').addItem('PM');  
        var dateTimeLabel = app.createLabel('',false).setId('dateTimeLabel');
        var submit = app.createButton('Create Minutes').setId('submit').setPixelSize(196, 25);
        var nextSteps = app.createAnchor('Please click here to see the minutes archive.', 'https://drive.google.com/xxxxxxxxxx/folderview?xxxxxxxxxx').setId('nextSteps').setVisible(false);
      
      // Setup error message 
        var warnException =app.createImage('https://sites.google.com/xxxxxxxxxx/minutes/create-new-minutes/Opps.png').setId('warnException').setVisible(false);
        var warnExceptionMes = app.createLabel('The date and Location are required. Please try again.').setStyleAttribute('font-weight', 'normal').setStyleAttribute('font-size','14px').setVisible(false);
      
      // handlers
        var handler1 = app.createClientHandler()
        .validateLength(location, 0, 50).validateMatches(dateBox, '2', 'g')
        .forTargets(warnException).setVisible(true)
        .forTargets(warnExceptionMes).setVisible(true);
      
        var handler2 = app.createServerHandler('handlerFunction')
        .validateLength(location, 1, 100).validateNotMatches(dateBox, '2', 'g')
        .addCallbackElement(location).addCallbackElement(dateBox).addCallbackElement(hpanel);
      
        submit.addClickHandler(handler1).addClickHandler(handler2);
      
        hpanel.setWidget(0,0,app.createLabel('Select Date'))
         .setWidget(0,1,app.createLabel('Hour'))
         .setWidget(0,2,app.createLabel('Minutes'))
         .setWidget(0,3,app.createLabel('AM/PM'))
         .setWidget(0,4,app.createLabel('Location'))
         .setWidget(0,5,app.createLabel('Minute Taker'))
      
        hpanel.setWidget(1,0,dateBox)
         .setWidget(1,1,hour)
         .setWidget(1,2,min)
         .setWidget(1,3,amPm)
         .setWidget(1,4,location)
         .setWidget(1,5,minuteTaker)
      
         hpanel.setWidget(2,5,submit)
         app.add(hpanel);//.add(warnException).add(warnExceptionMes);
      
        hpanelException.setWidget(1,1,warnException).setStyleAttribute("text-align", "right")
         .setWidget(1,2,warnExceptionMes)
        // .setWidget(1,2,nextSteps)
         app.add(hpanelException);
         return app;
           }
      
      
      function handlerFunction(e) {
       var app = UiApp.getActiveApplication();
       app.getElementById('submit').setText('Building, please wait...').setEnabled(false);
      
        var location = e.parameter.location; 
        var determineName = e.parameter.minuteTaker;  
        var date = e.parameter.dateBox;
        var timeZone = date.toString().substr(25,6)+":00";  
        var dateMilli = date.getTime();  
        var hour = parseInt(e.parameter.hour);  
        var amPm = e.parameter.amPm;
        if (amPm == 'PM' && hour != 12) hour = hour + 12;  
        if (hour == 12 && amPm == 'AM') hour = 0;  
        var hourMilli = hour * 3600000;  
        var minMilli = parseInt(e.parameter.minute) * 60000;  
        var milliTotal = dateMilli + hourMilli + minMilli; 
      
        // create custom format
        var newDate = Utilities.formatDate(new Date(milliTotal), timeZone, 'MM/dd/yy hh:mm aaa');
        app.getElementById('dateTimeLabel').setText(newDate);
      
        // make a copy of the minutes template to use
        var duplicateID = DriveApp.getFileById(templateIDToCopy)
        .makeCopy('Simply Minutes v1.0 - Stage 1: Building new minutes...')
       .getId();
      
      // get the id of the annual folder where minutes will be stored
        var getFolderID = DriveApp.getFolderById(folderIDtoCopyTo);
      
      // copy new minutes sheet to the annual folder where minutes are stored
        var moveIT = DriveApp.getFileById(duplicateID).makeCopy('Simply Minutes v1.0 - Stage 2: Building new minutes...', getFolderID).getId();
      
      // get the new minutes doc that was created
        var template = DocumentApp.openById(moveIT);
        var templateHeader = template.getHeader();
        var templateBody = template.getActiveSection();
      
      // fill in the values
        templateHeader.replaceText("<date>", newDate);
        templateBody.replaceText("<date>", newDate);
        templateHeader.replaceText("<location>", location);
        templateBody.replaceText("<location>", 'N/A');
        var email = Session.getEffectiveUser().getEmail();
        var eUser = Session.getEffectiveUser().getUsername();
        var createdBy = '';
      
        if(ContactsApp.getContact(email)){     
           var fullName = ContactsApp.getContact(email).getFullName();
           createdBy = fullName;
        }
        else {
           createdBy = 'N/A';
        };
      
        var determineName = e.parameter.minuteTaker;
        templateHeader.replaceText("<minutetaker>", determineName);
        templateHeader.replaceText("<createdby>", createdBy)
        templateBody.replaceText("<minutetaker>", determineName);
        templateBody.replaceText("<createdby>", createdBy); 
        template.setName(newDate + ' TAC Minutes Recorded By ' + determineName);
      
      // close out the doc
        template.saveAndClose();
      
      
        // remove the copy that was left in the root directory
      
       //  DriveApp.getFileById(duplicateID).isTrashed();
         DriveApp.getFileById(duplicateID).setTrashed(true);
      
        app = UiApp.getActiveApplication();
        app.getElementById('submit').setText('Completed!').setEnabled(false);
        app.getElementById('nextSteps').setVisible(true);
        return app;
          }
      

1 个答案:

答案 0 :(得分:0)

尝试这样(见下文)我更改了一些验证并在2个处理程序中分开+添加了一个“清除”处理程序,以便能够点击删除警告...... test code here

您还需要添加一些内容来清除服务器处理程序中的警告,为什么不在单个小部件中组合图像和文本警告? (更容易清洁)

以下代码:

function doGet(e) {
  var text= new Array();
  var app = UiApp.createApplication();
  var hpanel = app.createGrid(4, 6).setId('pannel');
  var clearHandler = app.createClientHandler().forEventSource().setVisible(false)
  var hpanelException = app.createGrid(2,3).setId('hpanelException');
  var location = app.createTextBox().setName('location').setId("location").setWidth('200');
  var minuteTaker = app.createListBox().setName('minuteTaker').setId("minuteTaker").setWidth('200')
   for (var i = 0 ; i < councilMembers.length; i++) {
    minuteTaker.addItem(councilMembers.valueOf()[i]);
                    }

  var dateBox = app.createDateBox().setId('dateBox').setName('dateBox').setFireEventsForInvalid(false);
  var hour = app.createListBox(false).setId('hour').setName('hour')
//  var hour = app.createListBox(false).setId('hour').setName('hour')
  for(h=1;h<13;++h){hour.addItem(h)}
  var min = app.createListBox(false).setId('minute').setName('minute')
  .addItem('00').addItem('15').addItem('30').addItem('45'); 
  var amPm = app.createListBox(false).setId('am').setName('amPm')
  .addItem('AM').addItem('PM');  
  var dateTimeLabel = app.createLabel('',false).setId('dateTimeLabel');
  var submit = app.createButton('Create Minutes').setId('submit').setPixelSize(196, 25);
  var nextSteps = app.createAnchor('Please click here to see the minutes archive.', 'https://drive.google.com/xxxxxxxxxx/folderview?xxxxxxxxxx').setId('nextSteps').setVisible(false);

// Setup error message 
  var warnException =app.createImage('https://sites.google.com/xxxxxxxxxx/minutes/create-new-minutes/Opps.png').setId('warnException').setVisible(false).addClickHandler(clearHandler);
  var warnExceptionMes = app.createLabel('The date and Location are required. Please try again.').setStyleAttribute('font-weight', 'normal').setStyleAttribute('font-size','14px').setVisible(false).addClickHandler(clearHandler);

// handlers
  var handler0 = app.createClientHandler()
  .validateLength(location, 0, 5)
  .forTargets(warnException).setVisible(true)
  .forTargets(warnExceptionMes).setVisible(true);
  var handler1 = app.createClientHandler()
  .validateNotMatches(dateBox, '2', 'g')
  .forTargets(warnException).setVisible(true)
  .forTargets(warnExceptionMes).setVisible(true);

  var handler2 = app.createServerHandler('handlerFunction')
  .validateLength(location, 6, 100).validateMatches(dateBox, '2', 'g')
  .addCallbackElement(location).addCallbackElement(dateBox).addCallbackElement(hpanel);

  submit.addClickHandler(handler0).addClickHandler(handler1).addClickHandler(handler2);

  hpanel.setWidget(0,0,app.createLabel('Select Date'))
   .setWidget(0,1,app.createLabel('Hour'))
   .setWidget(0,2,app.createLabel('Minutes'))
   .setWidget(0,3,app.createLabel('AM/PM'))
   .setWidget(0,4,app.createLabel('Location'))
   .setWidget(0,5,app.createLabel('Minute Taker'))

  hpanel.setWidget(1,0,dateBox)
   .setWidget(1,1,hour)
   .setWidget(1,2,min)
   .setWidget(1,3,amPm)
   .setWidget(1,4,location)
   .setWidget(1,5,minuteTaker)

   hpanel.setWidget(2,5,submit)
   app.add(hpanel);//.add(warnException).add(warnExceptionMes);

  hpanelException.setWidget(1,1,warnException).setStyleAttribute("text-align", "right")
   .setWidget(1,2,warnExceptionMes)
  // .setWidget(1,2,nextSteps)
   app.add(hpanelException);
   return app;
     }

评论后

编辑第二个版本。 我模拟了一个需要一些时间的服务器功能,因此您可以看到所有步骤。 +我按照建议结合警告。 (演示代码使用版本2更新)

var templateIDToCopy = 'xxxxxxxxxx';
var folderIDtoCopyTo = 'xxxxxxxxxx';
var councilMembers = ['Unknown','Name 1','Name 2'];

function doGet(e) {
  var text= new Array();
  var app = UiApp.createApplication();
  var hpanel = app.createGrid(4, 6).setId('pannel');
  var hpanelException = app.createGrid(2,3).setId('hpanelException');
  var location = app.createTextBox().setName('location').setId("location").setWidth('200');
  var minuteTaker = app.createListBox().setName('minuteTaker').setId("minuteTaker").setWidth('200')
   for (var i = 0 ; i < councilMembers.length; i++) {
    minuteTaker.addItem(councilMembers.valueOf()[i]);
                    }

  var dateBox = app.createDateBox().setId('dateBox').setName('dateBox').setFireEventsForInvalid(false);
  var hour = app.createListBox(false).setId('hour').setName('hour')
//  var hour = app.createListBox(false).setId('hour').setName('hour')
  for(h=1;h<13;++h){hour.addItem(h)}
  var min = app.createListBox(false).setId('minute').setName('minute')
  .addItem('00').addItem('15').addItem('30').addItem('45'); 
  var amPm = app.createListBox(false).setId('am').setName('amPm')
  .addItem('AM').addItem('PM');  
  var dateTimeLabel = app.createLabel('',false).setId('dateTimeLabel');
  var submit = app.createButton('Create Minutes').setId('submit').setPixelSize(195, 65);
  var nextSteps = app.createAnchor('Please click here to see the minutes archive.', 'https://drive.google.com/xxxxxxxxxx/folderview?xxxxxxxxxx').setId('nextSteps').setVisible(false);

  var clearHandler = app.createClientHandler();

// Setup error message 
  var warnException =app.createImage('https://dl.dropboxusercontent.com/u/211279/clock_e0.gif').addClickHandler(clearHandler);
  var warnExceptionMes = app.createLabel('The date and Location are required. Please try again.').setStyleAttribute('font-weight', 'normal').setStyleAttribute('font-size','14px').addClickHandler(clearHandler);
  var warnPanel = app.createHorizontalPanel().add(warnException).add(warnExceptionMes).setId('warning').setVisible(false);

  clearHandler.forTargets(warnPanel).setVisible(false);
// handlers
  var handler0 = app.createClientHandler()
  .validateLength(location, 0, 5)
  .forTargets(warnPanel).setVisible(true)

  var handler1 = app.createClientHandler()
  .validateNotMatches(dateBox, '2', 'g')
  .forTargets(warnPanel).setVisible(true)

  var handler2 = app.createClientHandler()
  .validateLength(location, 6, 100).validateMatches(dateBox, '2', 'g')
  .forEventSource().setText('Server Handler is running...').setEnabled(false)
  .forTargets(warnPanel).setVisible(false);

  var handlerS = app.createServerHandler('handlerFunction')
  .validateLength(location, 6, 100).validateMatches(dateBox, '2', 'g')
  .addCallbackElement(location).addCallbackElement(dateBox).addCallbackElement(hpanel);

  submit.addClickHandler(handler0).addClickHandler(handler1).addClickHandler(handler2).addClickHandler(handlerS);

  hpanel.setWidget(0,0,app.createLabel('Select Date'))
   .setWidget(0,1,app.createLabel('Hour'))
   .setWidget(0,2,app.createLabel('Minutes'))
   .setWidget(0,3,app.createLabel('AM/PM'))
   .setWidget(0,4,app.createLabel('Location'))
   .setWidget(0,5,app.createLabel('Minute Taker'))

  hpanel.setWidget(1,0,dateBox)
   .setWidget(1,1,hour)
   .setWidget(1,2,min)
   .setWidget(1,3,amPm)
   .setWidget(1,4,location)
   .setWidget(1,5,minuteTaker)

  hpanel.setWidget(2,5,submit)
  app.add(hpanel);

  hpanelException.setWidget(1,1,warnPanel).setStyleAttribute("text-align", "right")
  app.add(hpanelException);
  return app;
}


function handlerFunction(e) {
  var app = UiApp.getActiveApplication();
  Utilities.sleep(1000);
  app.getElementById('submit').setText('SERVER HANDLER is DONE');
//  app.getElementById('warning').setVisible(false);// not necassary anymore, see clientHandler2
  return app;
    }