-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarSync.gs
191 lines (166 loc) · 7.24 KB
/
CalendarSync.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
let counters = {
added: 0,
matchedAndSkipped: 0,
updated: 0,
deleted: 0
};
/**
* Checks if a personal event should be synced based on various criteria.
*/
function shouldBeSynced(personalEvent, personalCalendarId, workCalendarId) {
const startTime = personalEvent.getStartTime();
const endTime = personalEvent.getEndTime();
const dayOfWeek = startTime.getDay();
const durationHours = (endTime - startTime) / (1000 * 60 * 60);
// Check if the user is on the guest list and has accepted the event
const guestList = personalEvent.getGuestList(true); // true to include self
const personalUserGuest = guestList.find(guest => guest.getEmail() === personalCalendarId);
if (!personalUserGuest) {
console.log(`Event [${personalEvent.getTitle()}] - User is not on guest list. Skipping.`);
return false;
}
// Check if the user has explicitly accepted the event
if (personalUserGuest.getGuestStatus() !== CalendarApp.GuestStatus.YES) {
console.log(`Event [${personalEvent.getTitle()}] is not accepted in personal calendar. Skipping.`);
return false;
}
// Check if work account is already added to the event
if (guestList.some(guest => guest.getEmail() === workCalendarId)) {
console.log(`Event [${personalEvent.getTitle()}] already includes work account. Skipping sync.`);
return false;
}
if (personalEvent.isAllDayEvent() || durationHours > 4 || dayOfWeek == 0 || dayOfWeek == 6) {
return false;
}
try {
var freebusyQuery = Calendar.Freebusy.query({
timeMin: startTime.toISOString(),
timeMax: endTime.toISOString(),
items: [{ id: personalCalendarId }]
});
console.log(`Freebusy query response for ${personalEvent.getTitle()}:`, JSON.stringify(freebusyQuery));
var isEventFree = freebusyQuery.calendars[personalCalendarId].busy.length == 0;
if(isEventFree) {
console.log(`Event [${personalEvent.getTitle()}] is marked as Free.`);
} else {
console.log(`Event [${personalEvent.getTitle()}] is marked as Busy.`);
}
return !isEventFree;
} catch (error) {
console.log(`Error during Freebusy.query for event [${personalEvent.getTitle()}]: ${error.toString()}`);
return false;
}
}
/**
* Syncs a personal event to the work calendar if necessary, updating counters along the way.
*/
function syncEvent(personalEvent, workCal, workEventTitle) {
const existingWorkEvent = findMatchingWorkEvent(personalEvent, workCal.getEvents(personalEvent.getStartTime(), personalEvent.getEndTime()));
const personalTitle = personalEvent.getTitle();
const personalDesc = personalEvent.getDescription();
const expectedWorkDesc = personalTitle + '\n\n' + personalDesc;
if (existingWorkEvent) {
if (existingWorkEvent.getDescription() === expectedWorkDesc && existingWorkEvent.getTitle() === workEventTitle) {
console.log(`Event [${personalEvent.getTitle()}] already matches the work event. Skipping.`);
counters.matchedAndSkipped++;
} else {
updateWorkEvent(existingWorkEvent, personalEvent, workEventTitle);
counters.updated++;
}
} else {
createWorkEvent(workCal, personalEvent, workEventTitle);
counters.added++;
}
}
/**
* Finds a matching work event for a given personal event.
*/
function findMatchingWorkEvent(personalEvent, workEvents) {
return workEvents.find(workEvent => {
const startDiff = Math.abs(workEvent.getStartTime().getTime() - personalEvent.getStartTime().getTime());
const endDiff = Math.abs(workEvent.getEndTime().getTime() - personalEvent.getEndTime().getTime());
// Allow for a small time difference (e.g., 1 minute) to account for potential minor discrepancies
return startDiff <= 60000 && endDiff <= 60000;
});
}
/**
* Updates an existing work event to match a personal event.
*/
function updateWorkEvent(workEvent, personalEvent, title) {
workEvent.setTitle(title);
workEvent.setDescription(`${personalEvent.getTitle()}\n\n${personalEvent.getDescription()}`);
workEvent.setTime(personalEvent.getStartTime(), personalEvent.getEndTime());
workEvent.setVisibility(CalendarApp.Visibility.PRIVATE);
console.log(`Updated work event: ${workEvent.getId()}`);
}
/**
* Creates a new work event based on a personal event.
*/
function createWorkEvent(workCal, personalEvent, title) {
const newEvent = workCal.createEvent(title, personalEvent.getStartTime(), personalEvent.getEndTime(), {
description: `${personalEvent.getTitle()}\n\n${personalEvent.getDescription()}`,
visibility: CalendarApp.Visibility.PRIVATE
});
newEvent.removeAllReminders();
console.log(`Created new work event: ${newEvent.getId()}`);
}
/**
* Comprehensive cleanup function for work events.
* Removes unnecessary work events, including:
* - Duplicates
* - Those corresponding to personal events where work account is already added
* - Those no longer matching any personal event
*/
function cleanupWorkEvents(workCal, personalCal, workCalendarId, workEventTitle, startDate, endDate) {
const workEvents = workCal.getEvents(startDate, endDate).filter(e => e.getTitle() === workEventTitle);
const personalEvents = personalCal.getEvents(startDate, endDate);
const seen = new Map();
workEvents.forEach(workEvent => {
const key = `${workEvent.getStartTime().getTime()}-${workEvent.getEndTime().getTime()}`;
const correspondingPersonalEvent = personalEvents.find(personalEvent =>
personalEvent.getStartTime().getTime() === workEvent.getStartTime().getTime() &&
personalEvent.getEndTime().getTime() === workEvent.getEndTime().getTime()
);
if (seen.has(key) ||
(correspondingPersonalEvent && correspondingPersonalEvent.getGuestList().some(guest => guest.getEmail() === workCalendarId)) ||
!correspondingPersonalEvent) {
try {
workEvent.deleteEvent();
counters.deleted++;
console.log(`Deleted unnecessary work event: ${workEvent.getId()}`);
} catch (error) {
console.log(`Error deleting work event: ${error.toString()}`);
}
} else {
seen.set(key, true);
}
});
}
function logSummary() {
console.log(`Summary of Calendar Sync:`);
console.log(`New events added: ${counters.added}`);
console.log(`Events matched and skipped (no update needed): ${counters.matchedAndSkipped}`);
console.log(`Events updated: ${counters.updated}`);
console.log(`Events deleted: ${counters.deleted}`);
}
/**
* Main function to synchronize calendars.
*/
function syncCalendars() {
const personalCalendarId = "[email protected]";
const workCalendarId = "[email protected]";
const workEventTitle = "Time blocked";
const today = new Date();
const endDate = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000); // 7 days from today
const personalCal = CalendarApp.getCalendarById(personalCalendarId);
const workCal = CalendarApp.getCalendarById(workCalendarId);
const personalEvents = personalCal.getEvents(today, endDate);
personalEvents.forEach(personalEvent => {
if (shouldBeSynced(personalEvent, personalCalendarId, workCalendarId)) {
syncEvent(personalEvent, workCal, workEventTitle);
}
});
cleanupWorkEvents(workCal, personalCal, workCalendarId, workEventTitle, today, endDate);
console.log(`Finished syncing calendars.`);
logSummary();
}