-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapp-owa-extractor.gs
84 lines (71 loc) · 3.15 KB
/
gapp-owa-extractor.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
// uncomment if you want to save log file into your G Drive folder
// var dir = DriveApp.getFolderById("<< YOUR GOOGLE DRIVE FOLDER ID>>");
// GOOGLE CALENDAR IDENTIFICATOR in form - aaaa1123@group.calendar.google.com
var calendar = CalendarApp.getCalendarById("<<YOUR GOOGLE CALENDAR IDENTIFICATOR>>");
function doGet(e) {
return insertData(e, null);
}
function insertData(req, calendar){
// for test purpose uncomment line down below
// var data = req.parameter.data;
var data = JSON.parse(quoteKeys(decodeURIComponent(req.parameter.data)));
// var file = dir.createFile("log-" + new Date().getTime() + ".txt", JSON.stringify(data));
var calendarEvents = createEvents(data);
insertToCalendar(calendarEvents);
return ContentService.createTextOutput(req.parameter.callback + '('+ JSON.stringify('[{"message":"insert successfull."},{"data": ' + JSON.stringify(req) + '}]')+')').setMimeType(ContentService.MimeType.JAVASCRIPT);
}
function createEvents(data){
for(var i = 0; i < data.length; i++){
for(var k = 0; k < data[i].events.length; k++){
var dateStr = "";
var wholeDay = false;
if(data[i].events[k][0].toString().indexOf(':') !== -1){
dateStr = data[i].date + " " + data[i].events[k][0];
} else {
dateStr = data[i].date + " " + "00:00";
wholeDay = true;
}
var eStartTime = new Date(dateStr);
var eEndTime = new Date(dateStr);
var eDuration = data[i].events[k][2];
if(eDuration && eDuration.length > 0){
eEndTime.setMinutes(parseInt(eEndTime.getMinutes()) + parseInt(eDuration.split(':')[1]));
eEndTime.setHours(parseInt(eEndTime.getHours()) + parseInt(eDuration.split(':')[0]));
if(wholeDay){
eEndTime.setMinutes(59);
eEndTime.setHours(23);
}
}
var e = {title: data[i].events[k][1], startTime: eStartTime, endTime: eEndTime, location: data[i].events[k][3]}
if(typeof e !== "undefined"){
insertToCalendar(e);
}
}
}
}
function insertToCalendar(e){
if(!e){
Logger.log("Undefined event.");
return;
}
var eventExists = false;
calendar.getEventsForDay(e.startTime).forEach(function(calEvent){
if(e && e.title == calEvent.getTitle()){
Logger.log(calEvent.getTitle());
eventExists = true;
return;
}
});
if(!eventExists){
calendar.createEvent(e.title, e.startTime, e.endTime, {location: e.location});
Logger.log("Added - " + e.title);
}
}
function quoteKeys(data){
return data.replace(/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/g, '$1"$3":');
}
// test request
function test(){
doGet({'parameter':{'data':[{"date": "8/31/2018", "events": [["Dovolenka", "1 de\u0148", "", ""]]}, {"date": "8/27/2018", "events": [["12:00", "Design a architekt\u00fara EMANS - Workshop", "4:0", "ANA-multifunkcna zasadacka"]]}, {"date": "8/28/2018", "events": [["9:30", "Lunys intro", "1:30", "ANA-multifunkcna zasadacka"], ["13:00", "Architekt\u00fara a design Lunysu", "2:0", "ANA-multifunkcna zasadacka"]]}, {"date": "9/4/2018", "events": [["14:00", "Lunys PP - aktualne info + najblizsi plan", "1:30", "ANA-velka stredna zasadacka"]]}]
,'callback':'callbackkk'}});
}