Google Apps脚本日历服务:仅获取所有重复(全天)事件中的第一个事件 [英] Google Apps Script Calendar Service: Get only the first events of all recurring (all day) events
问题描述
In addition to this question I'd like to ask how to efficiently retrieve only the first events of all recurring (all day) events. To call the function findFirstEvent()
for each single event seems not to be reasonable. So my approach would be to filter the array of all events.
var cal=CalendarApp.getCalendarById("Calendar Id");
var startTime=new Date(1850,0,1);
var endTime=new Date();
var events=cal.getEvents(startTime, endTime);
var firstEvents=events.filter(onlyFirstEvents);
function onlyFirstEvents() {
...
}
最后,我实际上需要的是一个数组,以事件标题为键,而 Date
对象为值.
What I actually need in the end is an array with the event titles as keys and Date
objects as values.
推荐答案
- 您要从Google日历中检索所有重复发生的事件和全天事件.
- 尤其是,您要检索重复事件的开始事件的日期对象.
- 您想使用Google Apps脚本实现这一目标.
- 在这种情况下,将使用
isRecurringEvent()
和isAllDayEvent()
的方法. -
getEvents()
以降序返回事件.使用此方法,可以检索您期望的结果. - In this case, the methods of
isRecurringEvent()
andisAllDayEvent()
are used. getEvents()
returns the events with the descending order. Using this, the result you expect is retrieved.
如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
当以上几点反映到您的脚本中时,它如下所示.
When above points are reflected to your script, it becomes as follows.
var firstEvents=events.filter(onlyFirstEvents);
至:
var firstEvents = events.reduce(function(ar, e) {
var id = e.getId();
if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
}
return ar;
}, []);
结果:
运行上述脚本时,将返回以下值.
Result:
When above script is run, the following value is returned.
[
{
"eventTitle": "###",
"eventId": "###",
"startDate": ### date object ###,
"endDate": ### date object ###
},
,
,
]
参考文献:
- isRecurringEvent()
- isAllDayEvent()
- getId()
- 那么您将要在结果数组firstEvents中循环获取所需的数组,并将事件标题作为键,将Date对象作为值?
如果我误解了你的问题,而这不是你想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
由此,我无法理解您是要数组还是对象.所以我想提出两种模式.在这种情况下,我认为可以使用当前脚本的 firstEvents
.
From this, I cannot understand whether you want an array or an object. So I would like to propose 2 patterns. In this case, I thought that firstEvents
of the current script can be used.
在此模式下,将返回一个数组,该数组包括事件标题和开始日期对象分别是键和值.请进行如下修改.
In this pattern, an array, which includes that the event titles and the start date object are the key and value, respectively, is returned. Please modify as follows.
var firstEvents = events.reduce(function(ar, e) {
var id = e.getId();
if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
}
return ar;
}, []);
firstEvents = firstEvents.map(function(e) {
var obj = {};
obj[e.eventTitle] = e.startDate;
return obj;
});
模式2:
在此模式中,将返回一个对象,其中包括事件标题和开始日期对象分别是键和值.
Pattern 2:
In this pattern, an object, which includes that the event titles and the start date object are the key and value, respectively, is returned.
var firstEvents = events.reduce(function(ar, e) {
var id = e.getId();
if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
}
return ar;
}, []);
firstEvents = firstEvents.reduce(function(obj, e) {
obj[e.eventTitle] = e.eventTitle in obj ? obj[e.eventTitle].concat(e.startDate) : [e.startDate];
return obj;
}, {});
这篇关于Google Apps脚本日历服务:仅获取所有重复(全天)事件中的第一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!