-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
112 lines (96 loc) · 3.69 KB
/
Code.gs
File metadata and controls
112 lines (96 loc) · 3.69 KB
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
function syncCalendars() {
const personalCalendarId = "example@gmail.com";
const workCalendarId = "primary";
const eventInfo = {
summary: "OOO",
eventType: "outOfOffice",
visibility: "public",
outOfOfficeProperties: {
autoDeclineMode: "declineNone", // declineAllConflictingInvitations, declineOnlyNewConflictingInvitations
},
reminders: {
useDefault: false,
overrides: [],
},
};
const today = new Date();
const endDate = new Date();
endDate.setDate(today.getDate() + 30); // How many days in advance to monitor and block off time.
const personalCalendar = CalendarApp.getCalendarById(personalCalendarId);
const personalEvents = personalCalendar
.getEvents(today, endDate)
.filter(
(event) =>
event.getVisibility() !== CalendarApp.Visibility.PRIVATE &&
event.getMyStatus() !== CalendarApp.GuestStatus.NO,
);
const workCalendar = CalendarApp.getCalendarById(workCalendarId);
const workEvents = workCalendar.getEvents(today, endDate);
const workEventsFiltered = []; // To contain work calendar events that were previously created from personal calendar.
const workEventsExisted = []; // To contain work calendar events that already correctly existed.
const workEventsCreated = []; // To contain work calendar events that were created.
const workEventsDeleted = []; // To contain work calendar events previously created that have been deleted from personal calendar.
Logger.log("Number of work events: " + workEvents.length);
Logger.log("Number of personal events: " + personalEvents.length);
// Create filtered list of existing work calendar events that were previously created from the personal calendar.
for (const workEvent of workEvents) {
if (workEvent.getTitle() === eventInfo.summary) {
workEventsFiltered.push(workEvent);
}
}
for (const personalEvent of personalEvents) {
let existed = false;
// Check if copy of personal event already exists in the work calendar.
for (const workEvent of workEventsFiltered) {
if (
workEvent.getStartTime().getTime() ===
personalEvent.getStartTime().getTime() &&
workEvent.getEndTime().getTime() ===
personalEvent.getEndTime().getTime()
) {
workEventsExisted.push(workEvent.getId());
existed = true;
}
}
if (existed) continue;
// Create the event in the work calendar.
const createdEvent = createEvent(workCalendarId, eventInfo, personalEvent);
if (createdEvent) {
workEventsCreated.push(createdEvent.id);
}
}
// If a work event previously created no longer exists in the personal calendar, delete it.
for (const workEvent of workEventsFiltered) {
const workEventId = workEvent.getId();
if (!workEventsExisted.includes(workEventId)) {
try {
workEvent.deleteEvent();
workEventsDeleted.push(workEventId);
} catch (e) {
Logger.log("Failed to delete event: " + e.message);
}
}
}
Logger.log("Work events previously created: " + workEventsFiltered.length);
Logger.log("Work events already correct: " + workEventsExisted.length);
Logger.log("Work events created: " + workEventsCreated.length);
Logger.log("Work events deleted: " + workEventsDeleted.length);
}
function createEvent(calendarId, eventInfo, eventToCopy) {
const newEvent = {
...eventInfo,
start: {
dateTime: eventToCopy.getStartTime().toISOString(),
},
end: {
dateTime: eventToCopy.getEndTime().toISOString(),
},
};
try {
const createdEvent = Calendar.Events.insert(newEvent, calendarId);
return createdEvent;
} catch (e) {
Logger.log("Failed to create event: " + e.message);
}
return null;
}