-
Notifications
You must be signed in to change notification settings - Fork 9
/
AlarmClock.gs
50 lines (42 loc) · 1.35 KB
/
AlarmClock.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
function doGet()
{
var _calendarName = 'AlarmClock2'; // Calendar used.
var _checkInRate = 172800; // Only look a week ahead.
return ContentService.createTextOutput(getOutput(_calendarName, _checkInRate));
}
function getOutput(calendarName, offset)
{
return JSON.stringify(getResponse(calendarName, offset));
}
function getResponse(calendarName, offset)
{
var cal = CalendarApp.getCalendarsByName(calendarName)[0];
//return {status:"OK","time":[1501974000000,1502974000000],"event":["Wake Up","Business Lunch"],"info":["",""]};
if (cal)
{
var retEvent = [];
var retTitle = [];
var retInfo = [];
var now = new Date();
var later = new Date();
later.setSeconds(now.getSeconds() + offset);
var events = cal.getEvents(now, later);
var count = events.length;
if (events.length)
{
for(i in events)
{
retTitle[i] = events[i].getTitle();
var date = events[i].getStartTime();
retEvent[i] = date.getTime() / 1000;
retInfo[i] = events[i].getDescription();
// responses.push({"title" : title, "when" : epoch, "info" : info});
}
return {status: 'OK', event: retEvent, title: retTitle, info: retInfo};
}
else
return {status: 'EMPTY'};
}
else
return {status: 'NOK', error: 'Error, calendar "' + calendarName + '" does not exist.'};
}