-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacationPrep.gs
180 lines (151 loc) · 5.57 KB
/
vacationPrep.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
/* Vacation Prep
*
* clean up your calendar between two dates, for things like a vacation
*/
var vacationPrep = {};
vacationPrep.counters = {
oooDays: 0,
holidays: 0,
meetingDeleted: 0,
one2oneDeleted: 0,
myMeetingNotAttending: 0,
meetingNotAttending: 0,
};
vacationPrep.output = '';
vacationPrep.oooEvents = [
'OOO',
'OoO',
'Out of Office',
'Out of office',
];
vacationPrep.holidays = [
'US Holiday',
'Holiday',
];
vacationPrep.log = function(message) {
vacationPrep.output = vacationPrep.output + '\n' + message;
};
vacationPrep.shouldIgnoreEvent = function(title) {
// TODO check for new OoO event type, requires Advanced API
for (var i=0; i < vacationPrep.oooEvents.length; i++) {
if ( title.indexOf(vacationPrep.oooEvents[i]) > -1 ) {
vacationPrep.log("....Out of office event, ignore");
vacationPrep.counters.oooDays++;
return true;
}
}
for (var i=0; i < vacationPrep.holidays.length; i++) {
if ( title.indexOf(vacationPrep.holidays[i]) > -1 ) {
vacationPrep.log("....Holiday, ignore");
vacationPrep.counters.holidays++;
return true;
}
}
return false;
};
vacationPrep.deleteEvent = function(event, note, dryRun) {
vacationPrep.log("...." + note);
if (!dryRun) {
event.deleteEvent();
}
};
vacationPrep.notAttending = function(event, note, dryRun) {
vacationPrep.log("...." + note);
if (!dryRun) {
event.setMyStatus(CalendarApp.GuestStatus.NO);
}
};
vacationPrep.numGuests = function(event) {
// return the number of human guests
return event.getGuestList(true)
.map(g => g.getEmail())
.filter(e => !(e.indexOf('resource.calendar.google.com') > -1))
.length;
};
vacationPrep.run = function(startDate, endDate, dryRun) {
vacationPrep.log("Getting events from: " + startDate + " to " + endDate);
var events = CalendarApp.getDefaultCalendar().getEvents(startDate, endDate);
vacationPrep.log("found " + events.length + " events");
for (var i=0; i < events.length; i++) {
var title = events[i].getTitle();
var numGuests = vacationPrep.numGuests(events[i]);
vacationPrep.log(title);
if (vacationPrep.shouldIgnoreEvent(title)) {
continue;
}
if ( events[i].isOwnedByMe() ) {
// Handle meetings I own
vacationPrep.log("..my meeting. Guests: " + numGuests);
if (numGuests == 0) {
vacationPrep.deleteEvent(events[i], "reminder/placeholder, delete", dryRun);
vacationPrep.counters.meetingDeleted++;
} else if (numGuests == 2) {
vacationPrep.deleteEvent(events[i], "1:1, delete", dryRun);
vacationPrep.counters.one2oneDeleted++;
} else {
// I own a meeting with multiple attendees, hopefully they don't need me to continue
vacationPrep.notAttending(events[i], "multiple guests, not attending", dryRun);
vacationPrep.counters.myMeetingNotAttending++;
}
} else {
// Handle meetings owned by others
vacationPrep.log("..someone else's meeting. Guests: " + numGuests);
if (numGuests == 0) {
// I don't own this but there are no guests, reminder created by an admin?
// delete from my calendar
vacationPrep.deleteEvent(events[i], "reminder/placeholder, delete", dryRun);
vacationPrep.counters.meetingDeleted++;
} else {
vacationPrep.notAttending(events[i], "mark as not attending", dryRun);
vacationPrep.counters.meetingNotAttending++;
}
}
}
return [vacationPrep.counters, vacationPrep.output];
};
vacationPrep.formatCounters = function(counters) {
var msg = '';
for (key in counters) {
if (counters.hasOwnProperty(key)) {
msg = msg + '<br />' + key + ': ' + counters[key];
}
}
return msg;
}
function processVacationPrepForm(formObject) {
/* HTML input type=date return YYYY-MM-DD
* these two strings will give you different results based on TZ...wtf Javascript
* new Date("2019/02/18");
* new Date("2019-02-18");
*/
var startDate = new Date(formObject.startDate.replace(/-/g, '/') + " 00:01");
var endDate = new Date(formObject.endDate.replace(/-/g, '/') + " 23:59");
var dryRun = parseBooleanFormValue(formObject.dryRun);
// TODO support overriding oooEvents and holidays
[counters, output] = vacationPrep.run(startDate, endDate, dryRun);
return '<pre>' + output + '<br />' + vacationPrep.formatCounters(counters) + '</pre>';
}
function manualRun() {
var startDate = new Date("2022/05/27 00:01");
var endDate = new Date("2022/05/30 23:59");
var dryRun = true;
[counters, output] = vacationPrep.run(startDate, endDate, dryRun);
Logger.log(vacationPrep.formatCounters(counters));
Logger.log(output);
}
function testGuestList() {
var startDate = new Date("2022/05/27 00:01");
var endDate = new Date("2022/05/30 23:59");
var dryRun = true;
vacationPrep.log("Getting events from: " + startDate + " to " + endDate);
var events = CalendarApp.getDefaultCalendar().getEvents(startDate, endDate);
vacationPrep.log("found " + events.length + " events");
for (var i=0; i < events.length; i++) {
var title = events[i].getTitle();
var mine = events[i].isOwnedByMe() ? "mine" : "others";
var guestListWithOwner = events[i].getGuestList(true).map(g => g.getEmail());
var guestListWithoutOwner = events[i].getGuestList(false).map(g => g.getEmail());
Logger.log("%s: %s without owner: %s, %s", title, mine,guestListWithoutOwner.length, guestListWithoutOwner);
Logger.log("%s: %s with owner: %s, %s", title, mine, guestListWithOwner.length, guestListWithOwner);
}
}