错误:“您在短时间内创建或删除了太多日历或日历事件。”

时间:2015-03-27 19:23:32

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

第一次在这里问一个问题。

我已经构建了一个Google App脚本,可以从Google表格中获取事件信息,并根据@Mogsdad's answer here在Google日历上创建活动。 (因为我的名声不存在,我无法在该帖子上发帖提问)

" exportEvents"功能第一次很漂亮。但是,一旦电子表格有更多的信息行,它就没有完成脚本,而是说"您在短时间内创建或删除了太多的日历或日历事件。请稍后再试。"

这不是由于我设置的每日配额,因为我没有创建每天接近10,000个事件的任何内容。我搜索了一个解决方案,并找到了@ JEvans-GSA的this on Google Product Forum,他的脚本基于像我这样的@Mogsdad脚本。

我调整了他的解决方案并将" updateEvents"函数来自" exportEvents" (还创建了" deleteEvents"功能)。现在" exportEvents"不要碰到"您在短时间内创建或删除了太多的日历或日历事件。请稍后重试"问题。 但是" updateEvents"仍然得到相同的警告,执行的一半!

  • 例如,我创建了83个标题为" 1"到" 83"并出口了 事件成功日历。
  • 然后我将活动标题更改为" 100"至" 182"并试图更新活动。
  • 它只更新了" 1"〜" 15"到" 101"〜" 114",其他一切都保持不变。 - 我让功能更新了" status"列为"更新(今天' s 日期)",也没有执行。

所以函数已经启动但还没有完成,这让我觉得错误必须在设置重复的某个地方。是否有解决方法来制作" updateEvents" work或@Mogsdad suggested in the original answer是删除和重新创建事件的唯一方法吗?

提前感谢您的帮助。以下是我的电子表格的链接(由于我的代表性较低,我不允许使用超过2个链接): - https://docs.google.com/spreadsheets/d/1V6eioCo4QDPO2DdCyW93sLRWT_xtzKM-drDPVI1Gd8s/edit?usp=sharing

我目前的剧本:

//Global settings
var calId = 'n02m72vanqabnt1gcjluk2vosk@group.calendar.google.com';
var moderatorEmail = 'EMAIL_ADDRESS';

var idId = 0;
var startTimeId = 1;
var endTimeId = 2;
var titleId = 3;
var organizerId = 4;
var locId = 5;
var descId = 6;
var urlId = 7;
var topicId = 8;
var typeId = 9;
var contactId = 10;
var contactEmailId = 11;
var actionId = 12;
var statusId = 13;

//Add a custom menu
function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var items = [
    {name: 'Export Events', functionName: 'exportEvents'},
    {name: 'Update Events', functionName: 'updateEvents'},
    {name: 'Delete Events', functionName: 'deleteEvents'}
  ];
  ss.addMenu('Calendar', items);
}

//Actual functions
function exportEvents() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 2; //Number of rows of header info to skip
  var range = spreadsheet.getDataRange();
  var data = range.getValues();
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; //Skip header rows
    var row = data[i];
    var title = row[titleId];
    var tstart = new Date(row[startTimeId]);
      tstart.setDate(tstart.getDate());
      tstart.setMonth(tstart.getMonth());
      tstart.setYear(tstart.getYear());
      tstart.setTime(tstart.getTime());
    var tstop = new Date(row[endTimeId]);
      tstop.setDate(tstop.getDate());
      tstop.setMonth(tstop.getMonth());
      tstop.setYear(tstop.getYear());
      tstop.setTime(tstart.getTime());
    var loc = row[locId];
    var contact = row[contactId];
    var organizer = row[organizerId];
    var topic = row[topicId];
    var type = row[typeId];
    var contactEmail = row[contactEmailId];
    var url = row[urlId];
    var status = row[statusId];
    var desc = (row[descId]
        +"\n\n-Organizer: "+organizer
        +"\n-Event URL: "+url
        +"\n-Event Type: "+type
        +"\n-Event Contact: "+contact+" ("+contactEmail+")")
    var id = row[idId];
    // Check if the event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
    // Do nothing - we just want to avoid the exception when event doesn't exist
    }

    if (!event) {
      //cal.createEvent(title, new Date("Month D, YYYY 00:00:00"), new Date("Month D, YYYY 00:00:00), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
      row[idId] = newEvent; // Update the data array with event ID 

      var d = new Date();
      var time = d.getMonth()+1 + "/" + d.getDate() + "/" + d.getFullYear()
      row[statusId] = "Exported " + time
      }
    debugger;
  }
  range.setValues(data);
}

function updateEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 2; //Number of rows to skip
  var range = sheet.getDataRange();
  var data = range.getValues();
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue;
    var row = data[i];
    var title = row[titleId]; //Second column
    var tstart = new Date(row[startTimeId]);
      tstart.setDate(tstart.getDate());
      tstart.setMonth(tstart.getMonth());
      tstart.setYear(tstart.getYear());
      tstart.setTime(tstart.getTime());
    var tstop = new Date(row[endTimeId]);
      tstop.setDate(tstop.getDate());
      tstop.setMonth(tstop.getMonth());
      tstop.setYear(tstop.getYear());
      tstop.setTime(tstart.getTime());
    var loc = row[locId];
    var contact = row[contactId];
    var organizer = row[organizerId];
    var topic = row[topicId];
    var type = row[typeId];
    var contactEmail = row[contactEmailId];
    var url = row[urlId];
    var action = row[actionId];
    var status = row[statusId];
    var desc = (row[descId]
        +"\n\n-Organizer: "+organizer
        +"\n-Event URL: "+url
        +"\n-Type: "+type
        +"\n-Event Contact: "+contact+" ("+contactEmail+")")
    var id = row[idId];
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing
    }
    if (event) {
      if (action === "Update") {
        event.setTitle(title);
        event.setDescription(desc);
        event.setLocation(loc);
      // eventSetTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we Can set recurrence!
        var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
        event.setRecurrence(recurrence, tstart, tstop);

        var d = new Date();
        var time = d.getMonth()+1 + "/" + d.getDate() + "/" + d.getFullYear()
        row[statusId] = "Updated " + time
      }
    }
  }
  debugger;
  range.setValues(data);
}


function deleteEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 2; //Number of rows to skip
  var range = sheet.getDataRange();
  var data = range.getValues();
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue;
    var row = data[i];
    var action = row[actionId];
    var status = row[statusId];
    var id = row[idId];
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
    }
    if (event) {
      if (action === "Delete") {
        event.deleteEventSeries();

        var d = new Date();
        var time = d.getMonth()+1 + "/" + d.getDate() + "/" + d.getFullYear()
        row[idId] = "Deleted";
        row[statusId] = "Deleted " + time;
      }
    }
  }
  debugger;
  range.setValues(data);
}

3 个答案:

答案 0 :(得分:0)

在周日创建日历事件块的脚本遇到类似问题,只使用嵌套的while循环。我插入了Utilities.sleep(3000),以便脚本在每10次迭代后暂停。我测试了20周,每周创建20个事件(400个事件)并且没有错误消息。我打算玩这个时间来看看我是否可以缩短睡眠时间。这是Google Developer参考信息:

https://developers.google.com/apps-script/reference/utilities/utilities#sleep(Integer)

答案 1 :(得分:0)

我遇到了同样的错误,在Google上搜索让我在这里,所以我会留下我的解决方案。在我的情况下,我添加了电子邮件提醒事件,这导致“您在短时间内创建或删除了太多日历或日历事件。请稍后再试。”创建仅10个事件后出错。删除对Event.addEmailReminder()的调用解决了我的问题。

答案 2 :(得分:0)

我正面临类似的问题,对我来说,事件放在

if (i % 10 == 0) { 
   Utilities.sleep(3000); 
}

我收到此错误:超出了最大执行时间

我的脚本运行了大约20秒钟,并创建了50个事件。