-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.js
88 lines (74 loc) · 2.9 KB
/
Code.js
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
var OTHER_CALENDAR_EMAIL = "example@example.com";
function calendarUpdated(e)
{
// the calendar ID from the trigger event object
var calendarID = e.calendarId;
var scriptProperties = PropertiesService.getScriptProperties();
// get the sync token
var syncToken = scriptProperties.getProperty("syncToken");
// if we don't have one, let user know
// we need a sync token to get the events that changed
if(!syncToken)
{
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), "ERROR: No `syncToken`. Please run `initilize` first.", "ERROR: No `syncToken`. Please run `initilize` first." );
}
else
{
// get the other calendar
var otherCalendar = CalendarApp.getCalendarById(OTHER_CALENDAR_EMAIL);
// get all the events that changed since the last sync
var changedEvents = Calendar.Events.list(calendarID, {"syncToken" : syncToken});
// if we have items
while(changedEvents.items.length)
{
// get one
var eventDetails = changedEvents.items.shift();
// if the event is cancelled then...
if(eventDetails.status == "cancelled")
{
// TBD
}
else
{
// there are lots of calendar events
// an event title could be changed
// an event could be moved
// to do: determine the change type and handle accordingly
// for now, just create a new event on the other calendar
otherCalendar.createEvent("busy", new Date(eventDetails.start.dateTime), new Date(eventDetails.end.dateTime));
}
// if there are no more items
// if there are more pages, then get them
// developer note: how busy is someone that they have more than one page of event changes on each trigger run
if(changedEvents.items.length == 0 && changedEvents.nextPageToken)
{
changedEvents = Calendar.Events.list(calendarID, {"syncToken" : syncToken, "pageToken" : changedEvents.nextPageToken});
}
}
// save the sync token for the next update
scriptProperties.setProperty("syncToken", changedEvents.nextSyncToken);
}
}
function testCalendarUpdated()
{
calendarUpdated({"calendarId" : Session.getEffectiveUser().getEmail()});
}
// initilize this project
// get a next sync token so we can get updated events when the calendar is updated
function initilize()
{
// get my email address
// same as the calendar ID
var myEmail = Session.getEffectiveUser().getEmail();
// get all the events
var allEvents = Calendar.Events.list(myEmail);
// if we have pages, go through them
while(allEvents.nextPageToken)
{
// get the next page
allEvents = Calendar.Events.list(myEmail, {"pageToken" : allEvents.nextPageToken});
}
// at this point we have gone through all of the pages and have a sync token
// so save it for future reference
PropertiesService.getScriptProperties().setProperty("syncToken", allEvents.nextSyncToken);
}