Skip to content

Commit 1d58eae

Browse files
authored
Use outOfOffice calendar property instead of filters
No need to define keywords
1 parent 007937d commit 1d58eae

File tree

1 file changed

+8
-39
lines changed
  • solutions/automations/vacation-calendar

1 file changed

+8
-39
lines changed

solutions/automations/vacation-calendar/Code.js

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,11 @@ function sync() {
7272
let count = 0;
7373
users.forEach(function(user) {
7474
let username = user.getEmail().split('@')[0];
75-
KEYWORDS.forEach(function(keyword) {
76-
let events = findEvents(user, keyword, today, maxDate, lastRun);
77-
events.forEach(function(event) {
78-
importEvent(username, event);
79-
count++;
80-
}); // End foreach event.
81-
}); // End foreach keyword.
75+
let events = findEvents(user, today, maxDate, lastRun);
76+
events.forEach(function(event) {
77+
importEvent(username, event);
78+
count++;
79+
}); // End foreach event.
8280
}); // End foreach user.
8381

8482
PropertiesService.getScriptProperties().setProperty('lastRun', today);
@@ -126,9 +124,9 @@ function importEvent(username, event) {
126124
* @param {Date} optSince A date indicating the last time this script was run.
127125
* @return {Calendar.Event[]} An array of calendar events.
128126
*/
129-
function findEvents(user, keyword, start, end, optSince) {
127+
function findEvents(user, start, end, optSince) {
130128
let params = {
131-
q: keyword,
129+
eventTypes: "outOfOffice",
132130
timeMin: formatDateAsRFC3339(start),
133131
timeMax: formatDateAsRFC3339(end),
134132
showDeleted: true,
@@ -151,41 +149,12 @@ function findEvents(user, keyword, start, end, optSince) {
151149
user, keyword, e.toString());
152150
continue;
153151
}
154-
events = events.concat(response.items.filter(function(item) {
155-
return shouldImportEvent(user, keyword, item);
156-
}));
152+
events = events.concat(response.items);
157153
pageToken = response.nextPageToken;
158154
} while (pageToken);
159155
return events;
160156
}
161157

162-
/**
163-
* Determines if the given event should be imported into the shared team
164-
* calendar.
165-
* @param {Session.User} user The user that is attending the event.
166-
* @param {string} keyword The keyword being searched for.
167-
* @param {Calendar.Event} event The event being considered.
168-
* @return {boolean} True if the event should be imported.
169-
*/
170-
function shouldImportEvent(user, keyword, event) {
171-
// Filters out events where the keyword did not appear in the summary
172-
// (that is, the keyword appeared in a different field, and are thus
173-
// is not likely to be relevant).
174-
if (event.summary.toLowerCase().indexOf(keyword) < 0) {
175-
return false;
176-
}
177-
if (!event.organizer || event.organizer.email == user.getEmail()) {
178-
// If the user is the creator of the event, always imports it.
179-
return true;
180-
}
181-
// Only imports events the user has accepted.
182-
if (!event.attendees) return false;
183-
let matching = event.attendees.filter(function(attendee) {
184-
return attendee.self;
185-
});
186-
return matching.length > 0 && matching[0].responseStatus == 'accepted';
187-
}
188-
189158
/**
190159
* Returns an RFC3339 formated date String corresponding to the given
191160
* Date object.

0 commit comments

Comments
 (0)