如何在 Google 日历中的事件重复中使用多个例外

这是我的问题:


基于其中输入了培训课程预订的电子表格,我正在构建一个 Apps 脚本以将每个预订放入日历中。每个预订都包含从开始到结束的时间段,从而导致每周都有一次培训课程的重复活动。


到目前为止一切顺利,主要是因为我在 SO 找到了有价值的帮助和代码示例。我现在可以根据电子表格添加、更改和删除日历事件。当这一切发生时,我在 Google Apps 脚本编程方面达到了一个新水平:-)


但我一直在努力解决的一件事是如何向每个具有例外的预订添加多个例外。


表中的字段之一包含应从预订期限中排除的日期,格式为:“DD-MM-YYYY,DD-MM-YYYY”。我可以将它们转换为日期数组(例外):


[Sat Dec 05 00:00:00 GMT+01:00 2020, Sat Dec 26 00:00:00 GMT+01:00 2020]

当我想构建重复部分时,我已经失去了动力。我能够找到使用一种排除的示例,但我需要能够为一个事件构建多个排除。


我尝试过使用数组,但这对我不起作用。


这就是我所拥有的:


 for (var k=0;k < exceptions.length ;++k) {             // doorloop het hele array

     var recurrence = CalendarApp.newRecurrence().addWeeklyRule()

                      .addWeeklyExclusion(exceptions[k])

                      .until(new Date(CEUntil));

  }

这是我收到的错误:“异常:参数((类))与 CalendarApp.RecurrenceRule.addWeeklyExclusion 的方法签名不匹配。 ”


我究竟做错了什么?


开心每一天1111
浏览 209回答 2
2回答

交互式爱情

您需要注意传递给函数的值。它们被列为“参数”,并在下面链接的方法的文档中清楚地描述。addWeeklyExclusion()不接受任何值,但您试图向其传递一个日期。setRecurrence(recurrence, startTime, endTime)需要一个EventRecurrence对象,但您要向它传递一个数组。每个事件系列都有一个recurrence,并且recurrence可能有多个例外。无需创建重复数组。function createRecurringEvent() {  const Tstart = new Date('December 5, 2020 04:00:00 PM EST');  const Tstop = new Date('December 5, 2020 05:00:00 PM EST');  const CEUntil = new Date('January 10, 2021');    const recurrence = CalendarApp.newRecurrence()  const rule = recurrence.addWeeklyRule().until(CEUntil);    const exceptions = [ 'December 5, 2020', 'December 26, 2020' ];  exceptions.forEach(function(exception) {    rule.addDateExclusion(new Date(exception));  });    CalendarApp.getDefaultCalendar().createEventSeries(    'Recurring Event',    Tstart,    Tstop,    recurrence  );}

肥皂起泡泡

在迭戈的大力帮助下,我的代码得以运行。这是更新事件的相关代码片段,将异常排除在一系列事件之外:var exceptions = record[26].split(',');var event = calIndoorHall.getEventSeriesById(CEId);var recurrence = CalendarApp.newRecurrence();var rule = recurrence.addWeeklyRule().until(CEUntil);exceptions.forEach(function(exception) {&nbsp; rule.addDateExclusion(new Date(exception.toString()));});event.setRecurrence(recurrence, Tstart, Tstop);看着这段代码的美丽简洁,我希望自己在 JS/GAS 方面能更流利一些。我想,这是(更多)练习的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript