null
Created
August 26, 2023 07:20
-
-
Save kappa4/0b003f8eed7a09313208a9fb205cc10b to your computer and use it in GitHub Desktop.
Synchronize a schedule from another calendar to a specific calendar
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
function syncToday() { | |
for (const calendarInfo of setting.sourceCalendars) { | |
syncEventsWithOtherCalendar(calendarInfo, 1); | |
} | |
} | |
function syncThisWeek() { | |
for (const calendarInfo of setting.sourceCalendars) { | |
syncEventsWithOtherCalendar(calendarInfo, 7); | |
} | |
} | |
function syncOneMonth() { | |
for (const calendarInfo of setting.sourceCalendars) { | |
syncEventsWithOtherCalendar(calendarInfo, 30); | |
} | |
} | |
function syncThreeMonth() { | |
for (const calendarInfo of setting.sourceCalendars) { | |
syncEventsWithOtherCalendar(calendarInfo, 90); | |
} | |
} |
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 setting = { | |
targetCalendarId: 'xxxxxxx@group.calendar.google.com', | |
sourceCalendars: [ | |
{ | |
prefix: 'CalA', | |
id: 'calendar_a', | |
email: 'calendar_a@example.com' | |
}, | |
{ | |
prefix: 'CalB', | |
id: 'calendar_b', | |
email: 'calender_b@example.com' | |
}, | |
{ | |
prefix: 'CalC', | |
id: 'calendar_c', | |
email: 'calendar_c@example.com' | |
} | |
] | |
} | |
const identifyKeyName = 'sourceCalendarId' |
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
function syncEventsWithOtherCalendar(calendarInfo, days) { | |
let date = new Date(); | |
for (let i = 0; i < days; i++) { | |
deleteTargetCalendarForDay(calendarInfo.id, date); | |
const sourceCalendarEvents = getEventsForDay(calendarInfo.id, date) | |
for (const event of sourceCalendarEvents) { | |
if (getEventSource(event) != null || event.eventType != 'default' || isDeclined(calendarInfo.email, event)) { | |
continue; | |
} | |
addAnotherCalendarEvent(calendarInfo, event); | |
} | |
date.setDate(date.getDate() + 1); | |
} | |
} | |
function deleteTargetCalendarForDay(sourceCalendarId, date) { | |
const targetCalendarEvents = getEventsForDay(setting.targetCalendarId, date); | |
for (const event of targetCalendarEvents) { | |
if (getEventSource(event) != sourceCalendarId) { | |
continue; | |
} | |
try { | |
Calendar.Events.remove(setting.targetCalendarId, event.id); | |
} | |
catch(e) { | |
console.log("setting.targetCalendarId: " + setting.targetCalendarId); | |
console.log("event: " + JSON.stringify(event)); | |
console.log("Error message: " + e.message); | |
} | |
} | |
} | |
function isDeclined(email, event) { | |
if (typeof event.attendees == 'undefined') { | |
return false; | |
} | |
for (const attendee of event.attendees) { | |
if (attendee.email != email) { | |
continue; | |
} | |
if (attendee.responseStatus == "declined") { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
return false; | |
} | |
function addAnotherCalendarEvent(calendarInfo, event) { | |
let reservedEvent = { | |
summary: `[${calendarInfo.prefix}]` + event.summary, | |
description: event.description, | |
// 会議用URLが入れてある場合があり、会議情報と両方ある場合があるのでこちらを併せて保留する | |
// location: event.location, | |
// 会議情報を入れたいが簡単にできないようなので保留 | |
// conferenceData: event.conferenceData, | |
// hangoutLink: event.hangoutLink, | |
start: event.start, | |
end: event.end, | |
extendedProperties: { | |
private: { | |
[identifyKeyName]: calendarInfo.id | |
} | |
}, | |
colorId: event.colorId | |
}; | |
try { | |
Calendar.Events.insert(reservedEvent, setting.targetCalendarId); | |
} | |
catch(e) { | |
console.log("sourceCalendarId: " + calendarInfo.id); | |
console.log("event: " + JSON.stringify(event)); | |
console.log("reservedEvent: " + JSON.stringify(reservedEvent)); | |
console.log("Error message: " + e.message); | |
} | |
} | |
function getEventsForDay(calendarId, date) { | |
const endOfDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); | |
const timeMin = new Date(date.setHours(0, 0, 0, 0)).toISOString(); | |
const timeMax = endOfDate.toISOString(); | |
const events = Calendar.Events.list(calendarId, { | |
timeMin: timeMin, | |
timeMax: timeMax, | |
singleEvents: true | |
}); | |
if (!events.items || events.items.length === 0) { | |
return []; | |
} | |
return events.items; | |
} | |
function getEventSource(event) { | |
if ('extendedProperties' in event && 'private' in event.extendedProperties) { | |
return event.extendedProperties.private[identifyKeyName]; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment