-
Notifications
You must be signed in to change notification settings - Fork 0
/
Office365Calendar.js
264 lines (229 loc) · 6.48 KB
/
Office365Calendar.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function getGraphService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return (
OAuth2.createService("graph")
.setAuthorizationBaseUrl(
"https://login.microsoftonline.com/" +
Settings.getTenantId() +
"/oauth2/v2.0/authorize"
)
.setTokenUrl(
"https://login.microsoftonline.com/" +
Settings.getTenantId() +
"/oauth2/v2.0/token"
)
// Set the client ID and secret, from the Google Developers Console.
.setClientId(Settings.getClientId())
.setClientSecret(Settings.getSecret())
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction("authCallback")
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
.setScope("https://graph.microsoft.com/Calendars.ReadWrite")
);
}
function logout() {
var service = getGraphService();
service.reset();
}
function authorizeIfRequired() {
if (!Settings.getOffice365CalendarEnabled()) {
return true;
}
var graphService = getGraphService();
if (graphService.hasAccess()) {
return true;
}
var authorizationUrl = graphService.getAuthorizationUrl();
var template = HtmlService.createTemplate(
"<style>" +
" .button {" +
" background-color: #1c87c9;" +
" border: none;" +
" color: white;" +
" padding: 20px 34px;" +
" text-align: center;" +
" text-decoration: none;" +
" display: inline-block;" +
" font-size: 20px;" +
" margin: 4px 2px;" +
" cursor: pointer;" +
" border-radius: 4px;" +
" }" +
"</style>" +
"<div>" +
' <a class="button" href="<?= authorizationUrl ?>" target="_blank">Authorize</a>' +
"</div>" +
"<div>" +
"Reopen the sidebar when the authorization is complete." +
"</div>"
);
template.authorizationUrl = authorizationUrl;
var page = template.evaluate();
SpreadsheetApp.getUi().showSidebar(page);
return false;
}
function authCallback(request) {
var graphService = getGraphService();
var isAuthorized = graphService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput("Success! You can close this tab");
} else {
return HtmlService.createHtmlOutput("Denied. You can close this tab");
}
}
function makeRequest(url, options) {
var graphService = getGraphService();
if (options === undefined) {
options = {};
}
options.headers = {
Authorization: "Bearer " + graphService.getAccessToken(),
};
var response = UrlFetchApp.fetch(
"https://graph.microsoft.com/v1.0/me/" + url,
options
);
return response;
}
function test() {
var response = makeRequest(
"calendarview?startDateTime=2024-12-27&endDateTime=2024-12-27&$select=Subject,Start,End,IsAllDay,ShowAs"
);
var x = response.getContentText();
var y = JSON.parse(x);
var z = 1;
}
function test2() {
var calendar = new Office365Calendar();
var events = calendar.getEvents(
new Date("2024-12-27"),
new Date("2024-12-27")
);
var event = events[0];
var sd = event.getStartTime();
var d = new Date(sd);
var y = new Date("2020-01-01T00:00:00.0000000");
// var eventById = calendar.getEventById(event.getId());
var eventByMissingId = calendar.getEventById("xxx");
var x = 1;
}
function formatDate(date) {
return (
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
);
}
var Office365Calendar = function () {
this.createEvent = function (title, startTime, endTime) {
var payload = {
subject: title,
showAs: "Oof",
isAllDay: true,
start: {
dateTime: formatDate(startTime),
timeZone: "GMT",
},
end: {
dateTime: formatDate(endTime),
timeZone: "GMT",
},
};
var options = {
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
};
var response = makeRequest("events", options);
var json = JSON.parse(response.getContentText());
return new Office365CalendarEvent(json);
};
this.getEventById = function (calendarId) {
if (calendarId === null) {
return null;
}
if (calendarId === "") {
return null;
}
try {
var response = makeRequest("events/" + calendarId);
var content = response.getContentText();
var eventData = JSON.parse(content);
return new Office365CalendarEvent(eventData);
} catch (error) {
return null;
}
};
this.getEvents = function (startTime, endTime) {
var response = makeRequest(
"calendarview?startDateTime=" +
formatDate(startTime) +
"&endDateTime=" +
formatDate(endTime) +
"&$select=Subject,Start,End,IsAllDay,ShowAs"
);
var content = response.getContentText();
var eventData = JSON.parse(content).value;
var events = [];
for (var i = 0; i < eventData.length; i++) {
events.push(new Office365CalendarEvent(eventData[i]));
}
return events;
};
this.requiresDayAdjustment = function () {
return true;
};
this.supportsId = function () {
return true;
};
this.getAdjustment = function () {
return 1;
};
this.getType = function () {
return "office365";
};
this.isReadOnly = function () {
return false;
};
};
var Office365CalendarEvent = function (data) {
this.data = data;
this.getId = function () {
return this.data.id;
};
this.getTitle = function () {
return this.data.subject;
};
this.getStartTime = function () {
return new Date(this.data.start.dateTime.substring(0, 19));
};
this.getEndTime = function () {
return new Date(this.data.end.dateTime.substring(0, 19));
};
this.deleteEvent = function () {
var options = {
method: "delete",
};
makeRequest("events/" + this.getId(), options);
};
this.setTime = function (startTime, endTime) {
var payload = {
start: {
dateTime: formatDate(startTime),
timeZone: "GMT",
},
end: {
dateTime: formatDate(endTime),
timeZone: "GMT",
},
};
var options = {
method: "patch",
contentType: "application/json",
payload: JSON.stringify(payload),
};
makeRequest("events/" + this.getId(), options);
};
};