Reputation: 1638
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() {
...
}
What I actually need in the end is an array with the event titles as keys and Date
objects as values.
Upvotes: 1
Views: 1239
Reputation: 201553
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
isRecurringEvent()
and isAllDayEvent()
are used.getEvents()
returns the events with the descending order. Using this, the result you expect is retrieved.When above points are reflected to your script, it becomes as follows.
var firstEvents=events.filter(onlyFirstEvents);
To:
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;
}, []);
When above script is run, the following value is returned.
[
{
"eventTitle": "###",
"eventId": "###",
"startDate": ### date object ###,
"endDate": ### date object ###
},
,
,
]
If I misunderstood your question and this was not the direction you want, I apologize.
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.
Script: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;
});
In this pattern, an object, which includes that the event titles and the start date object are the key and value, respectively, is returned.
Script: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;
}, {});
Upvotes: 2