Last active
July 19, 2024 11:29
-
-
Save paina/480e786e6f0cca7d84ea09300c7d2a03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EMAILS_ADD = ["john@example.com", "doe@example.co.jp"]; | |
const EVENT_LIST_BEFORE_MONTH = 30; | |
const EVENT_LIST_AFTER_MONTH = 2; | |
const DRY_RUN = false; | |
function addAttendeeToAllEvents() { | |
const calendarId = 'primary'; | |
let start = new Date(); | |
let end = new Date(); | |
start.setMonth(start.getMonth() - EVENT_LIST_BEFORE_MONTH); | |
end.setMonth(end.getMonth() + EVENT_LIST_AFTER_MONTH); | |
const cond = { | |
timeMin: start.toISOString(), | |
timeMax: end.toISOString(), | |
singleEvents: false, // include recurring events themselves | |
eventTypes: 'default', // exclude focusTime, fromGmail, outOfOffice and workingLocation | |
showDeleted: false | |
}; | |
let pageToken = ''; | |
let first = true; | |
while (pageToken || first) { | |
Logger.log('---'); | |
if (!first) { | |
cond.pageToken = pageToken; | |
} | |
page = Calendar.Events.list(calendarId, cond); | |
pageToken = page.nextPageToken; | |
first = false; | |
for (let event of page.items) { | |
if ( | |
// cancelled instances of recurring events may be included in results. skip them. | |
('status' in event ? (event.status != 'cancelled') : true) | |
&& ( | |
// default of guestsCanInviteOthers is true | |
('guestsCanInviteOthers' in event ? event.guestsCanInviteOthers : true) | |
|| event.organizer.self | |
) | |
// events having recurringEventId are instances of recurring events | |
// XXX: これでも recurring events が複数ヒットすることがある模様 | |
// その場合は ETag が変わって Precondition Failed になる | |
&& !event.recurringEventId) { | |
Logger.log('[%s] %s (%s)', event.id, event.summary, event.start.dateTime); | |
if ('attendees' in event) { // XXX: どういう場合に attendees が含まれないのかよく分からない | |
// XXX: event.attendees にグループが含まれることがありそうだが対応できていない | |
attendees = event.attendees.map((attendee) => attendee.email); | |
let emails = EMAILS_ADD.filter((t) => !attendees.includes(t)); | |
if (emails.length) { | |
Logger.log('[%s] -> %s', event.id, emails) | |
for (const email of emails) { | |
event.attendees.push({ email: email }); | |
} | |
if (!DRY_RUN) { | |
try { | |
Calendar.Events.update( | |
event, | |
calendarId, | |
event.id, | |
{}, | |
{ 'If-Match': event.etag } | |
); | |
} catch (e) { | |
Logger.log(e); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment