-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCode.gs.js
153 lines (144 loc) · 5.86 KB
/
Code.gs.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
/*!
* Parse Pollen Alert emails from NAAF (The Norwegian Asthma and Allergy
* Association) and add the alert to the Google Calendar
*
* @author Alexander Nilsen <alexander.nilsen.no@gmail.com>
*
* ---------------------------------------------------------------------------
* <https://github.com/alni/digital-signage-samples>
*/
/**
* Use this with timed triggers
*/
function myFunction() {
var emailThreads = getPollenvarslingEmails();
var firstThread = emailThreads && emailThreads[0];
var firstMessage = firstThread && firstThread.getMessages()[0];
var obj = parsePollenvarslingEmail(firstMessage);
createCalendarEvent(obj);
}
/**
* Parse a string to date
* @param {string} sDate - the date string parse.
* The format "DD.MM.YYYY" is assumed
* @returns {Date}
*/
function parseDate(sDate) {
var parts = sDate.split(".");
return new Date(parseInt(parts[2], 10), parseInt(parts[1], 10) - 1, parseInt(parts[0], 10));
}
/**
* Gets all Gmail threads that has the label "Pollenvarsling"
* @returns {GmailThread[]}
*/
function getPollenvarslingEmails() {
var userLabel = GmailApp.getUserLabelByName("Pollenvarsling");
if (!userLabel) {
}
var emailsThreads = userLabel && userLabel.getThreads();
return emailsThreads;
}
/**
* Parse a email and find the Pollen Alert data
* @param {GmailMessage} email - the email to parse
* @returns {object} an object with the following properties:
* {Date} date - the date for the Alert
* {string} title - the title (subject) for the Alert
* {string} location - the location area for the Alert
* {string} desc - the main Alert information description
*/
function parsePollenvarslingEmail(email) {
//var email = email || GmailApp.getMessageById("1538f3023b6dcf5b");
var tmp, date;
var content = email && email.getBody();
var title = email && email.getSubject();
var obj = {
date: title
// If the email has a subject, parse the date found in the subject
? parseDate(title.substring(title.length - "20.03.2016".length))
// Otherwise, use the current date
: new Date(),
// Set the title to the email subject. If email was not found ("title"
// is not set to a value then fall back to to a placeholder text
// indicating that this is a test alert and it should be deleted)
title: (title || "Test varsel (DELETE ME)") + "",
location: "", //"Østlandet med Oslo",
desc: "", //"Det ventes kraftig spredning av pollen fra hassel og moderat fra or."
};
if (content) {
// Find content within a H3 tag (the location area of the email alert)
tmp = content.match(/\<h3\>(.*)+\<\/h3\>/);
// Set the alert location to the found content (or empty string of none
// was found)
obj.location = (tmp && tmp[1]) ? tmp[1].trim() : "";
//tmp = content.match(/\<div\>(Det ventes(.*)+)\<\/div\>/);
//obj.desc = (tmp && tmp[1]) ? tmp[1].trim() : "";
// The index of the Pollen type alert table (It is the only table in
// the email that specifies the 'width="100%"' attribute)
var index = content.indexOf("<table width=\"100%\"");
// Set "tmp" to start at the Pollen type alert table index. Also trim
// the string
tmp = content.substring(index).trim();
// Set the end of "tmp" to the index of the first closing table tag,
// and append a closing table tag (as the found tag is not included
// with the sub string)
tmp = tmp.substring(0, tmp.indexOf("</table")) + "</table>";
// Set the alert description to the current value of "tmp"
obj.desc = tmp + "";
}
return obj;
};
/**
* Creates or updates existing event from the first calendar found with the
* name "Pollenvarsling"
* @param {object} obj - the object to create or update a calendar event for.
* The object should be in the same format as returned by
* "parsePollenvarslingEmail()"
*
* - If an event for the alert date already exists AND it has the same
* location as the alert, then replace the title and the description of
* the event
* - Otherwise create a new event for the alert
*/
function createCalendarEvent(obj) {
//var obj = parsePollenvarslingEmail();
var cal = CalendarApp.getCalendarsByName("Pollenvarsling")[0];
var events = cal && cal.getEventsForDay(obj.date);
var calEvent = null;
if (events && events.length > 0) {
// Existing events for date found
Logger.log('Found %s matching events for %s', events.length, obj.date.toDateString());
// Loop through each one
for (var i = 0; i < events.length; i++) {
//var calEvent = events[i];
// Check if event location is the same as the alert location
if (obj.location == events[i].getLocation()) {
// Location is the same, so set "calEvent" to the current event
// and break out of the loop
calEvent = events[i];
break;
}
//calEvent.deleteEvent();
}
}
var description = [obj.title, "", obj.location, "", obj.desc].join("\n");
if (calEvent) {
// Existing calendar event found, so just update the title and the
// description
calEvent.setTitle(obj.title);
calEvent.setDescription(obj.desc);
//calEvent.setTitle(obj.desc);
//calEvent.setDescription(description);
} else {
/*calEvent = cal.createAllDayEvent(obj.desc, obj.date, {
location: obj.location,
description: description
});*/
// Calendar event does not already exists, so create a new from the
// alert object data
calEvent = cal.createAllDayEvent(obj.title, obj.date, {
location: obj.location,
description: obj.desc
});
}
}