- You want to retrieve all recurring events and all day events from a Google calendar.
- Especially, You want to retrieve the date object of the start event of the recurring event.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification points:
- In this case, the methods of
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.
Modified script:
From:
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;
}, []);
Result:
When above script is run, the following value is returned.
[
{
"eventTitle": "###",
"eventId": "###",
"startDate": ### date object ###,
"endDate": ### date object ###
},
,
,
]
References:
If I misunderstood your question and this was not the direction you want, I apologize.
Added:
- So you would for loop through the result array firstEvents to get the desired array with the event titles as keys and Date objects as values?
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.
Pattern 1:
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;
});
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.
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;
}, {});