如何使用GAS激活DateBox的onValueChangeHandler

时间:2014-07-21 18:33:11

标签: google-apps-script datebox

我有2个日期框并为它们添加了一个onValueChangeHandler,以便在更改日期时更新事件的灵活性。有用。

加载时(在doGet返回之后)我为两个日期框提供有效数据,并希望执行onValueChangeHandler。不幸的是,与复选框不同,日期框没有setValue(date, true);方法。

   var onValueChangeShowEvents = app.createServerHandler('onValueChangeShowEvents');
   var dbxDateFrom  = app.createDateBox().setId(wid_dbxDateFrom).setWidth('85')
                              .addValueChangeHandler(onValueChangeShowEvents);
   var dbxDateUntil = app.createDateBox().setId(wid_dbxDateUntil).setWidth('85')
                              .addValueChangeHandler(onValueChangeShowEvents);
   var eventsTable  = app.createFlexTable().setId(wid_eventsTable);


function onValueChangeShowEvents(e)
{
var func = 'onValueChangeShowEvents';
Logger.log('in ' + func + '   source= ' + e.parameter.source + '   value= ' + e.parameter[e.parameter.source]);
   var app  = UiApp.getActiveApplication();

   var dbxDateFrom  = app.getElementById(wid_dbxDateFrom);
   var dbxDateUntil = app.getElementById(wid_dbxDateUntil);

   var dateFrom  = new Date(e.parameter[wid_dbxDateFrom]);
   var dateUntil = new Date(e.parameter[wid_dbxDateUntil]);
   dateUntil     = new Date(dateUntil.getTime() + (24 * 60 * 60 - 1) * 1000); // Full day until 23 u 59.59

   var cal       = CalendarApp.getCalendarsByName(MyCalender)[0]; // Retrieve the calender (verify !!)  
   var events    = cal.getEvents(dateFrom, dateUntil);              // All events within the timeframe
   var numEvents = events.length;

   var eventsTable = app.getElementById(wid_eventsTable);
   eventsTable.clear().setCellSpacing(1).setCellPadding(1);

//   eventsTable.setColumnStyleAttributes(0, { textAlign: "right" } ); // Does not work
   eventsTable.setStyleAttributes({ textAlign: "right" } );

   var numFields   = 5;
   var text        = '';
   var widthFields = [ '26', '85', '50', '50', '200'];

   for (var i=0; i<numEvents; i++)
   {
      var event       = events[i];
      var numRow      = i + 1;
//      var id          = event.getId();
      var dateStart   = event.getStartTime();
      var dateEnd     = event.getEndTime();
      var title       = event.getTitle();
//      var location    = event.getLocation();
//      var guests      = event.getGuestList();

      for (var j=0; j<numFields; j++)
      {  
         var fieldId  = evtPrefix + bell + numRow + bell + j; // Coding the fields
         var tbxField = app.createTextBox().setReadOnly(true).setStyleAttribute('fontWeight', 'bold')
                                .setId(fieldId).setName(fieldId).setWidth(widthFields[j]);
         if (j == 0)      text = numRow; // Must be changed into a button or anchor in order to allow show and update the event
         else if (j == 1) text = dateStart.format('yyyy-mm-dd'); // Uses http://blog.stevenlevithan.com/archives/date-time-format
         else if (j == 2) text = dateStart.format('HH:MM');
         else if (j == 3) text = dateEnd.format('HH:MM');
         else if (j == 4) text = title;

         tbxField.setText(text);
         eventsTable.setWidget(numRow, j, tbxField);
      }      
   };

   return app;
}

我如何从代码中初始激活onValueChangeHandler?

1 个答案:

答案 0 :(得分:0)

不确定为什么你不能直接在doGet中赋值,但如果你真的希望能够从外部函数调用你的函数(不是由处理程序触发),你可以使用有效的{{1}来调用它。参数。 e实际上是一个具有一些属性的JavaScript对象,并且很容易构建。

e