Google Apps脚本超时"最长执行时间"

时间:2014-05-12 14:39:13

标签: google-apps-script google-calendar-api

嗨,我有一个脚本可以改变日历事件的可见性。脚本达到最长执行时间。有什么办法可以阻止这个吗?或者,更好的是,现在脚本会遍历每个事件,即使它们已经设置为私有。我可以修改脚本以便它只检查当天吗?

function ChangeCalendarEvents() {


      //Room1
    var mycal = 'comp.com_38353134313283637333434@resource.calendar.google.com';
    var cal = CalendarApp.getCalendarById(mycal);
      var events = cal.getEvents(new Date('January 1, 2014 00:00:00 CST'), new Date('December 31, 2014 23:59:59 CST'), {visibility: 'default' || 'public'});
      for (var i=0;i<events.length;i++) {
        events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
      }


      //Room2
      var mycal2 = 'comp.com_319373936383633831343039@resource.calendar.google.com';
    var cal2 = CalendarApp.getCalendarById(mycal2);
      var events = cal2.getEvents(new Date('January 1, 2014 00:00:00 CST'), new Date('December 31, 2014 23:59:59 CST'), {visibility: 'default' || 'public'});
      for (var i=0;i<events.length;i++) {
        events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
      }
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下修改后的代码版本。

它有以下变化:
1. mycal现在是一个数组(使代码更容易阅读,更容易添加/删除日历进行修改)
2.开始/结束边界现在是变量(默认情况下,start =今天,结束=今天+ 1周)

这应该很快就会运行。如果您仍然遇到问题,请将调试点设置为&#34; for(var i = 0 ...&#34;并告诉我在events数组中有多少元素。

function ChangeCalendarEvents() {
  var mycal      = ['comp.com_38353134313283637333434@resource.calendar.google.com',   //Room 1 
                    'comp.com_319373936383633831343039@resource.calendar.google.com'], //Room 2
      start      = new Date(),
      end        = new Date(start.getYear(),start.getMonth(),start.getDate()+7);
  for (var index = 0; index < mycal.length;index++) {
       var cal   = CalendarApp.getCalendarById(mycal[index]),
          events = cal.getEvents(start, end, {visibility: 'default' || 'public'});
      for (var i=0;i<events.length;i++) {
            events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
          }
      };
    }