-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calendar Events into Web App
58 lines (56 loc) · 1.79 KB
/
Calendar Events into Web App
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
function getCalendarEvents() {
var calendarId = 'primary'; // Use 'primary' for the primary calendar
var now = new Date();
var startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
var endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
var events = Calendar.Events.list(calendarId, {
timeMin: startOfMonth.toISOString(),
timeMax: endOfMonth.toISOString(),
singleEvents: true,
orderBy: 'startTime'
});
return events.items.map(function(event) {
return {
title: event.summary,
start: event.start.dateTime || event.start.date, // All-day events do not have dateTime
end: event.end.dateTime || event.end.date
};
});
}
function testGetCalendarEvents() {
try {
var events = getCalendarEvents(); // Call the main function
Logger.log('Number of events fetched: ' + events.length);
Logger.log(JSON.stringify(events)); // Log the details of events
} catch (error) {
Logger.log('Error testing getCalendarEvents: ' + error.toString());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Monthly Calendar View</title>
</head>
<body>
<h1>Calendar Events for This Month</h1>
<ul id="events"></ul>
<script>
function fetchEvents() {
google.script.run
.withSuccessHandler(success)
.getCalendarEvents();
}
function success(events) {
console.log(events);
var eventsList = document.getElementById('events');
eventsList.innerHTML = '';
events.forEach(function(event) {
var li = document.createElement('li');
li.textContent = event.title + ' (' + event.start + ' to ' + event.end + ')';
eventsList.appendChild(li);
});
}
window.onload = fetchEvents; // Fetch events when the page loads
</script>
</body>
</html>