-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
312 lines (241 loc) · 7.68 KB
/
Code.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//georgetown.edu_rb1h0lgbkesf7re227b0q28d50@group.calendar.google.com
var MeritBadges = [
'Laser Cutter',
'3D Printing',
'Hand Tools',
'HandiBot',
'Power Tools',
'Print Shop',
'Sewing Machine',
'Embroidery Machine',
'Vinyl Cutter',
'FormLabs',
'Soldering',
'Arduino',
'Button Maker',
'Raspberry Pi'
];
var futuredays = 14; // how far in the future to look for creating new events
function getUserTable(){
var userTable = SpreadsheetApp.openById(getUserTableId());
return userTable;
}
function deleteSkillCalEvents(startTime, endTime){
var realStartDate = new Date(startTime);
var realEndDate = new Date(endTime);
var skillCalendar = getSkillCal();
try{
var events = skillCalendar.getEvents(new Date(startTime), new Date(endTime));
Logger.log(events.length);
for(var i = events.length-1; i >= 0; i--){
Logger.log("deleting");
events[i].deleteEvent();
}
}catch(e){
Logger.log(e);
}
}
function generateSkillEvents(startTime, endTime){
// two weeks in ms: 1000 * 60 * 60 * 24 * 14
var futuretime = 1000 * 60 * 60 * 24 * futuredays;
if(!startTime){
startTime = new Date();
// startTime = "2018/06/01 01:00:00";
}
if(!endTime){
var endTime = new Date(startTime.getTime() + futuretime);
}
Logger.log(startTime.toString());
Logger.log(endTime.toString());
// return;
deleteSkillCalEvents(startTime.toString(), endTime.toString());
// return;
var skillCalendar = getSkillCal();
var staffEvents = getStaffScheduleEvents(startTime.toString(), endTime.toString());
for(var i=0;i<staffEvents.length; i++){
staffEvent = staffEvents[i];
var eventStart = staffEvent.startTime;
var eventEnd = staffEvent.endTime;
Logger.log(JSON.stringify(staffEvent, null, ' '));
if(staffEvent.guestList.length == 0){
continue;
}
staff = staffEvent.guestList[0].email;
var netid = staff.split("@")[0];
Logger.log(netid);
var userData= getUserTableDataFromNetId(netid);
Logger.log(JSON.stringify(userData, null, " "));
var badgeList = [];
for(var j = 0; j < MeritBadges.length; j++){
var badge = MeritBadges[j];
if(userData[badge] && userData[badge].trim().toLowerCase() != "no"){
badgeList.push(badge);
}
}
badgeList = badgeList.join(", ");
Logger.log(badgeList);
var title = badgeList;
if(badgeList.trim() == ""){
continue;
}
createCalEvent(skillCalendar, eventStart, eventEnd, title, "", []);
}
}
var userTableData = false;
var allUserData = false;
function getUserData(filter, sort){
var userlist = {};
var userTable = getUserTable();
allUserData = userTable
.getActiveSheet()
.getDataRange()
.getValues();
/*
Logger.log("all data");
Logger.log(JSON.stringify(allUserData, null, " "));
*/
userTableData = dataIntoHashRows(allUserData, 0, 1); //, function(row){ return row['NetId'] == netId;}).data;
//Logger.log(userTableData);
return JSON.parse(JSON.stringify(userTableData));
}
function getUserTableDataFromNetId(netId){
if(!userTableData){
getUserData();
}
var userData = dataIntoHashRows(allUserData, 0, 1, function(row){
if(row["netid"].toString().toLowerCase().trim() == netId.toString().toLowerCase().trim()){
return true;
}
return false;
}); //, function(row){ return row['NetId'] == netId;}).data;
if(userData.data.length > 0){
return JSON.parse(JSON.stringify(userData.data[0]));
}
return false;
}
function getStaffScheduleEvents(startTime, endTime){
if(!startTime){
startTime = "2018/06/01 01:00:00";
}
if(!endTime){
endTime = "2018/06/18 01:00:00";
}
var cal = getStaffScheduleCal();
var staffCalEvents = getCalEvents(cal, startTime, endTime);
return staffCalEvents;
}
function createSkillCalEvent(startTime, endTime, title, description, guests){
var cal = getSkillCal();
return createCalEvent(cal, startTime, endTime, title, description, guests);
}
function getCalEvents(cal, startTime, endTime){
var realStartDate = new Date(startTime);
var realEndDate = new Date(endTime);
try{
var events = cal.getEvents(new Date(startTime), new Date(endTime));
var returnEvents = events.map(function(event){
var returnEvent = {
id: event.getId(),
calendarName : cal.getName(),
startTime: event.getStartTime().toString(),
endTime : event.getEndTime().toString(),
title : event.getTitle(),
description : event.getDescription(),
guestList : event.getGuestList(true).map(function(guest){
return {name : guest.getName(),
email : guest.getEmail(),
status: guest.getGuestStatus()
};
}),
creators : event.getCreators(),
dateCreated : event.getDateCreated.toString(),
location : event.getLocation()
}
return returnEvent;
});
return returnEvents;
}catch (error){
Logger.log(error);
throw error;
}
}
/// Create Events
function createCalEvent(cal, startTime, endTime, title, description, guests){
var guestlist = "";
if(guests){
var guestEmails = guests.map(function(g){
if(g.indexOf("@") < 0){
return g+"@georgetown.edu";
}
});
guestlist = guestEmails.join(",");
}
var options = {
location: "Maker Hub",
description: description,
guests : guestlist
};
var startDate = new Date(startTime);
var endDate = new Date(endTime);
var event = cal.createEvent(title, new Date(startTime), new Date(endTime), options)
var returnEvent = {
id: event.getId(),
startTime: event.getStartTime().toString(),
endTime : event.getEndTime().toString(),
title : event.getTitle(),
description : event.getDescription(),
guestList : event.getGuestList(true).map(function(guest){
return {name : guest.getName(),
email : guest.getEmail(),
status: guest.getGuestStatus()
};
}),
creators : event.getCreators(),
dateCreated : event.getDateCreated.toString(),
location : event.getLocation()
}
return returnEvent;
}
/*
=========================== DATAINTOHASHROWS ===============================
Any time you get some rows from a google sheet, run it through this function
so that it uses the column names as keys, instead of just numbers.
data : the data from the sheet
keysRow : the row that holds the column names (starts a 0, NOT 1)
startsRow : the row that holds the FIRST row of data (starts a 0, NOT 1)
filterFunction: a function that gets a row of data (with the column names as
keys), and returns true or false, based on whatever criteria you want.
============================================================================
*/
function dataIntoHashRows(data, keysRow, startRow, filterFunction){
var idKey= {};
var keyId= {};
var newData = [];
// Logger.log("data");
// Logger.log(keysRow);
// Logger.log(JSON.stringify(data, null, " "));
for (var k = 0; k < data[keysRow].length; k++) {
var key = data[keysRow][k];
key = key.replace("?","");
key = key.replace("'","");
key = key.replace(":","");
if(key.trim() == ""){
continue;
}
idKey[k] = key;
keyId[key] = k;
}
for (var i = startRow; i < data.length; i++) {
var newRow = {};
for (var j = 0; j < data[i].length; j++) {
if(!idKey[j] || idKey[j].trim() == ""){
continue;
}
newRow[idKey[j]] = data[i][j];
}
if(!filterFunction || filterFunction(newRow) == true){
newData.push(newRow);
}
}
return {data:newData, keyId: keyId, idKey: idKey};
}