-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.gs
More file actions
168 lines (147 loc) · 5.74 KB
/
Copy pathscript.gs
File metadata and controls
168 lines (147 loc) · 5.74 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
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
const CONFIG = {
SOURCE_CALENDAR_IDS: [
'sourceCalendar1@gmail.com',
'sourceCalendar2@gmail.com'
],
TARGET_CALENDAR_ID: 'targetWorkCalendar@gmail.com',
DAYS_AHEAD: 60,
SYNC_TAG_KEY: 'busyBlockerCalSyncKey',
TRIGGER_INTERVAL_MINUTES: 15,
};
// ─── SETUP: Run once ─────────────────────────────────────────────────────────
function setupTrigger() {
ScriptApp.getProjectTriggers()
.filter(t => t.getHandlerFunction() === 'syncBusyBlocks')
.forEach(t => ScriptApp.deleteTrigger(t));
ScriptApp.newTrigger('syncBusyBlocks')
.timeBased()
.everyMinutes(CONFIG.TRIGGER_INTERVAL_MINUTES)
.create();
Logger.log('Trigger installed.');
}
// ─── HELPERS ─────────────────────────────────────────────────────────────────
function toRFC3339(date) {
return date.toISOString();
}
function getSyncKey(event) {
return event?.extendedProperties?.private?.[CONFIG.SYNC_TAG_KEY] ?? null;
}
// ─── MAIN SYNC ───────────────────────────────────────────────────────────────
function syncBusyBlocks() {
const now = new Date();
const end = new Date();
end.setDate(now.getDate() + CONFIG.DAYS_AHEAD);
// ── 1. Collect source events ──────────────────────────────────────────────
const desiredMap = {};
CONFIG.SOURCE_CALENDAR_IDS.forEach(calId => {
let pageToken = null;
do {
const params = {
timeMin: toRFC3339(now),
timeMax: toRFC3339(end),
singleEvents: true,
maxResults: 250,
};
if (pageToken) params.pageToken = pageToken;
let response;
try {
response = Calendar.Events.list(calId, params);
} catch (e) {
Logger.log('ERROR reading calendar %s: %s', calId, e.message);
return;
}
(response.items || []).forEach(event => {
// Skip all-day events (they have date, not dateTime)
if (!event.start?.dateTime) return;
// Skip cancelled events
if (event.status === 'cancelled') return;
const key = `${calId}::${event.id}`;
desiredMap[key] = {
key,
start: event.start.dateTime,
end: event.end.dateTime,
};
});
pageToken = response.nextPageToken;
} while (pageToken);
});
// ── 2. Load existing sync blocks from target ──────────────────────────────
const existingMap = {};
let pageToken = null;
do {
const params = {
timeMin: toRFC3339(now),
timeMax: toRFC3339(end),
privateExtendedProperty: `calSyncMarker=exists`, // filter to only our blocks
singleEvents: true,
maxResults: 250,
};
if (pageToken) params.pageToken = pageToken;
let response;
try {
response = Calendar.Events.list(CONFIG.TARGET_CALENDAR_ID, params);
} catch (e) {
Logger.log('ERROR reading target calendar: %s', e.message);
return;
}
(response.items || []).forEach(event => {
if (event.status === 'cancelled') return;
const key = getSyncKey(event);
if (key) existingMap[key] = event;
});
pageToken = response.nextPageToken;
} while (pageToken);
// ── 3. Delete stale blocks ────────────────────────────────────────────────
Object.keys(existingMap).forEach(key => {
if (!desiredMap[key]) {
try {
Calendar.Events.remove(CONFIG.TARGET_CALENDAR_ID, existingMap[key].id);
Logger.log('Deleted stale block: %s', key);
} catch (e) {
Logger.log('ERROR deleting block %s: %s', key, e.message);
}
}
});
// ── 4. Create or update blocks ────────────────────────────────────────────
Object.values(desiredMap).forEach(desired => {
const existing = existingMap[desired.key];
if (!existing) {
// Create
try {
Calendar.Events.insert({
summary: 'Busy (auto-synced)',
start: { dateTime: desired.start },
end: { dateTime: desired.end },
visibility: 'private',
transparency: 'opaque', // shows as Busy to others
extendedProperties: {
private: {
[CONFIG.SYNC_TAG_KEY]: desired.key, // the source event key
calSyncMarker: 'exists', // used for list filter
}
}
}, CONFIG.TARGET_CALENDAR_ID);
Logger.log('Created block: %s', desired.key);
} catch (e) {
Logger.log('ERROR creating block %s: %s', desired.key, e.message);
}
} else {
// Update only if times changed
const startChanged = existing.start.dateTime !== desired.start;
const endChanged = existing.end.dateTime !== desired.end;
if (startChanged || endChanged) {
try {
Calendar.Events.patch({
start: { dateTime: desired.start },
end: { dateTime: desired.end },
}, CONFIG.TARGET_CALENDAR_ID, existing.id);
Logger.log('Updated block: %s', desired.key);
} catch (e) {
Logger.log('ERROR updating block %s: %s', desired.key, e.message);
}
}
}
});
Logger.log('Sync complete. Desired: %s, Existing: %s',
Object.keys(desiredMap).length, Object.keys(existingMap).length);
}