-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
131 lines (105 loc) · 3.36 KB
/
Code.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
/* Trying to solve two problems here:
* 1. gcal won't import remote calendars nicely (into a calendar I can edit)
* 2. gcal only makes one calendar public
*
* Thus, there are "remote" calendars in my gcal that pull from an icloud
* calendar. Then, this script runs nightly and copies everything over to
* my public calendar up to a certain range (a week, a month, etc.)
*/
var daysToCopy = 90;
var cal_default = CalendarApp.getCalendarById("PUBLIC CALENDAR ID HERE");
var cals = [
CalendarApp.getCalendarById("PRIVATE CALENDAR ID HERE"),
CalendarApp.getCalendarById("PRIVATE CALENDAR ID HERE"),
];
// Event labeling library
function templateAutogenLabel(date) {
return `Autogenerated at ${date} by Calendar Curator\n\n`; // also interpreted as a regex
}
function initAutogen() {
// Updates global state!
PropertiesService.getScriptProperties().setProperty('last_updated', Date().toString());
}
function getAutogenLabel() {
let last_updated = PropertiesService.getScriptProperties().getProperty('last_updated')
return templateAutogenLabel(last_updated);
}
function isAutogenerated(event) {
let autogenRegex = templateAutogenLabel(".*?"); // any date
return event.getDescription().match(autogenRegex);
}
function isNewlyAutogenerated(event) {
return event.getDescription().match(getAutogenLabel());
}
function setAutogenLabel(event) {
let desc = event.getDescription();
while (isAutogenerated(event)) { // in case there are multiple labels?
let autogenRegex = templateAutogenLabel(".*?"); // any date
desc = desc.replace(autogenRegex,"");
desc = desc.trim();
}
event.setDescription(getAutogenLabel() + desc);
}
// main
function main() {
initAutogen();
removeAutogen(cal_default);
for (c in cals) {
copyCalendarContents(cals[c], cal_default);
}
}
function removeAutogen(cal) {
var dayInMS = 24 * 60 * 60 * 1000
var today = new Date();
var endDate = new Date(today.getTime() + (daysToCopy * dayInMS));
var events = cal.getEvents(today, endDate);
for (e in events) {
var event = events[e];
if (isAutogenerated(event)) {
event.deleteEvent();
}
}
}
function copyCalendarContents(fromCal, toCal) {
// Requires that autogenerated events are removed
var dayInMS = 24 * 60 * 60 * 1000
var today = new Date();
var endDate = new Date(today.getTime() + (daysToCopy * dayInMS));
var events = fromCal.getEvents(today, endDate);
for (e in events) {
var event = events[e];
copyEvent(fromCal, toCal, event);
}
}
function copyEvent(fromCal, toCal, event) {
// We don't bother with recurring events
// because it is too hard to determine the recurrence
// and we can just copy everything anyways...
if (event.isAllDayEvent()) {
return copyAllDayEvent(fromCal, toCal, event);
} else {
return copyHourlyEvent(fromCal, toCal, event);
}
}
function copyAllDayEvent(fromCal, toCal, event) {
return toCal.createAllDayEvent(
event.getTitle(),
event.getAllDayStartDate(),
event.getAllDayEndDate(),
{
"description": getAutogenLabel() + event.getDescription(),
"location": event.getLocation()
}
);
}
function copyHourlyEvent(fromCal, toCal, event) {
return toCal.createEvent(
event.getTitle(),
event.getStartTime(),
event.getEndTime(),
{
"description": getAutogenLabel() + event.getDescription(),
"location": event.getLocation()
}
);
}