|
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) || event.transparency == 'transparent') { |
|
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.eventTitle, |
|
start: event.start, |
|
end: event.end, |
|
extendedProperties: { |
|
private: { |
|
[identifyKeyName]: calendarInfo.id |
|
} |
|
}, |
|
colorId: 8 |
|
}; |
|
|
|
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; |
|
} |