-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCode.gs
131 lines (111 loc) · 4.32 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
/**
* - en2gcal
* - Evernote to Google Calendar Reminder
*
* Copyright (c) 2012 Chris Lawson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Chris Lawson
* @version 0.5
* @since 2015-10-22
*/
/**
* Find Gmail thread labeled with .reminder and create a Google Calendar event based
* on the free text description included on the subject line.
*/
/**
*
* pop:{minutes} - optional - adds a popup reminder using specified minutes
*
* sms:{minutes} - optional - adds an sms remidner using specified minutes
*
* email:{minutes} - optional - adds an email reminder using specified minutes
*
* cal:"{calendar_name}" - optional - adds the reminder to the specified calendar
* otherwise default calendar is used.
* Calendar names with spaces must be enclosed in quotes. "" or ''.
*
* inc:{0|1} - optional - 1 by default - include copy of note contents in the description
* of the event. All html will be removed however breaks and paragraphs are maintained.
*
* {Evernote note link} - optional - Include the Evernote note link on the subject and it will
* be added to the event description as text. e.g. evernote:///view/999999/s1/...
*
*/
function addReminder() {
var label = GmailApp.getUserLabelByName('.reminder');
if (!label) return;
var threads = label.getThreads();
var subject = '';
var cal = '';
for(i in threads){
var params = parseSubject_(threads[i].getFirstMessageSubject());
var msg = threads[i].getMessages();
var desc = '';
params = checkParams_(params);
if ('cal' in params) {
cal = CalendarApp.getCalendarsByName(params.cal)[0];
} else {
cal = CalendarApp.getDefaultCalendar();
}
if (cal !== '' && cal !== undefined) {
var event = cal.createEventFromDescription(params.subject);
if (event === undefined) continue;
if ('evernote' in params) desc = "evernote:" + params.evernote + "\n";
if ('inc' in params && (params.inc === '1' || params.inc === 'true')) {
str = msg[0].getBody();
str=str.replace('From Evernote:','\n');
str=str.replace(/<\s*br\/*>/gi, "\n");
str=str.replace(/<\s*a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1) ");
str=str.replace(/<\s*\/*.+?>/ig, "\n");
str=str.replace(/ {2,}/gi, " ");
str=str.replace(/\n+\s*/gi, "\n\n");
desc += str;
}
if (desc !== '') event.setDescription(desc);
if ('sms' in params) event.addSmsReminder(params.sms);
if ('pop' in params) event.addPopupReminder(params.pop);
if ('email' in params) event.addEmailReminder(params.email);
}
}
label.removeFromThreads(threads);
}
function checkParams_(params) {
// getUserProperties() currently returns an empty object not matter what
// values are stored. Using Script Properties instead.
var properties = PropertiesService.getScriptProperties();
var sms = properties.getProperty('sms');
var pop = properties.getProperty('pop');
var email = properties.getProperty('email');
var cal = properties.getProperty('cal');
var inc = properties.getProperty('inc') || '1';
// if param not already specified on subject set to ScriptProperty if exists
if (!('sms' in params) && sms) params.sms = sms;
if (!('pop' in params) && pop) params.pop = pop;
if (!('email' in params) && email) params.email = email;
if (!('cal' in params) && cal) params.cal = cal;
if (!('inc' in params) && inc) params.inc = inc;
return params;
}
function parseSubject_(subject) {
var keyval = /([^ \d]+):\s*([^ '"]+|'[^']+'|"[^"]+")/g,
cleanup = /([^ \d]+):\s*([^ '"]+\s*|'[^']+'\s*|"[^"]+"\s*)/g,
match, params = {};
while (match = keyval.exec(subject)) {
params[match[1]] = match[2].replace(/"/g,'').replace(/'/g,"");
}
params.subject = subject.replace(cleanup,'');
return params;
}