This repository has been archived by the owner on Dec 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifications.gs
286 lines (235 loc) · 8.1 KB
/
Notifications.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
/**
* Notify counselors that have been recently approved to teach a merit badge
*
*/
function sendNewlyApprovedEmails() {
//get counselors information from spreadsheet
var counselors = getCounselors();
//establish user interface for spreadsheet
var ui = SpreadsheetApp.getUi();
//get benchmark date to determine if a counselor was 'recently approved'
var benchmark = getBenchmarkDate();
//iterate through each counselor
for (i = 0; i < counselors.length; i++) {
var counselor = counselors[i];
var recentlyApproved = false;
//determine if a conselor was recently approved
for (j = 0; j < counselor.mbs.length; j++) {
//approval date is more recent then benchmark date
if (counselor.mbs[j].approved > benchmark) {
recentlyApproved = true;
break;
/**
* Notify counselors that have been recently approved to teach a merit badge
*
*/
function sendNewlyApprovedEmails() {
//get counselors information from spreadsheet
var counselors = getCounselors();
//establish user interface for spreadsheet
var ui = SpreadsheetApp.getUi();
//get benchmark date to determine if a counselor was 'recently approved'
var benchmark = getBenchmarkDate();
//iterate through each counselor
for (i = 0; i < counselors.length; i++) {
var counselor = counselors[i];
//skip retired counselors
if (counselor.status != 'Retired') {
//determine if a conselor was recently approved
var recentlyApproved = false;
for (j = 0; j < counselor.mbs.length; j++) {
//approval date is more recent then benchmark date
if (counselor.mbs[j].status == 'Active' && counselor.mbs[j].approved > benchmark) {
recentlyApproved = true;
break;
}
}
if (recentlyApproved) {
//only email counselors that have an email address
if (counselor.email != '') {
//confirm with the user before emailing a counselor
var response = ui.alert(
'Notify Newly Approved Counselor',
'Are you sure you want to email ' + counselor.name + '?',
ui.ButtonSet.YES_NO
);
//user clicks YES button
if (response == ui.Button.YES) {
//get templated html file with counselor informtion
var htmlMsg = getHTMLTemplate(counselor, 'TemplateNewlyApproved').getContent();
//send email through gmail
GmailApp.sendEmail(
counselor.email, //counselor.email,
'Approved Merit Badge Counselor', //email subject
'Congratualtions, you have been approved to be a merit badge counselor.', //plain text
{
htmlBody: htmlMsg, //html message
name: 'Mountaineer Area Council Advancement' //senders name
}
);
ui.alert('Email successfully sent to ' + counselor.name);
//user clicks NO button
} else if (response == ui.Button.NO) {
ui.alert('Alright, ' + counselor.name + ' was not emailed.');
//user closes the prompt window without responding
} else {
ui.alert('Notifications canceled.');
return;
}
//notify user that the counselor is missing an email address
} else {
ui.alert('Error: Cannot email ' + counselor.name + '.\nNo email address found.');
}
}
}
}
}
/**
* Email counselors to reconfrim that they wish to continue serving as counselors
*
*/
function sendReconfirmEmail() {
//get counselors information from spreadsheet
var counselors = getCounselors();
//iterate through each counselor
for (i = 0; i < counselors.length; i++) {
var counselor = counselors[i];
//only email counselors with an unknown status and an email address
if (counselor.status == 'Unknown' || counselor.email != '') {
var htmlMsg = getHTMLTemplate(counselor, 'TemplateReconfirm').getContent();
//send email through gmail
GmailApp.sendEmail(
counselor.email, //counselor.email,
'Follow Up: Merit Badge Counselor', //email subject
'Thank you for serving as a merit badge counselor in 2016. Would you like to continue serving in 2017? Click Here: ' + getReconfirmFormLink(counselor,'Yes'),
{
htmlBody: htmlMsg, //html message
name: 'Mountaineer Area Council Advancement' //senders name
}
);
}
}
}
/**
* Get benchmark date from spreadsheet
*
* @return {date} benchmark date
*/
function getBenchmarkDate() {
//establish spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//establish sheet for Counselors
var sheet = ss.getSheetByName('Settings');
//establish range from sheet
var range = sheet.getRange("F1");
//extract data from range
var value = range.getValues();
return value[0][0];
}
/**
* Get html template file with counselor's information
*
* @return {HtmlOutput}
*/
function getHTMLTemplate(counselor, template) {
var t = HtmlService.createTemplateFromFile(template);
t.counselor = counselor;
return t.evaluate();
}
/**
* Get file location for HTML Services
* (Used to link stylesheet)
*
* @return {HTMl Output}
*/
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
/**
* Genearte url to prefiled Google Form with counselor's informtion
*
* @param {counselor}
* @param {string} answer to reconfirm question
* @return {string} url
*/
function getReconfirmFormLink(counselor,reconfirm) {
var link = '';
//form url
link = 'https://docs.google.com/forms/d/e/1FAIpQLSeW-YZmHQy8JJHVMlNtOv2NWVo8ekgTOGz_kxAEGPg8UNLFlg/viewform?';
//reconfirm
link+= 'entry.1638971109=' + reconfirm.toString();
//full name
link += '&entry.1382249611=' + counselor['name'].toString().replace(' ', '+');
//email
link += '&entry.687380762=' + counselor['email'].toString();
//phone
link += '&entry.47665138=' + counselor['phone'].toString().replace(' ', '+');
//unit type
link += '&entry.1762778775=' + counselor['unitType'].toString().replace(' ', '+');
//unit number
link += '&entry.1739667129=' + counselor['unitNum'].toString();
//help other units
link += '&entry.268023147=' + counselor['help'].toString();
//youth protection training
var expirationDate = new Date(2015, 1, 20);
if ( isDate(counselor['ypt']) && counselor['ypt'] > expirationDate) {
link += '&entry.2058760198=' + formatDate(counselor['ypt'],'yyyy-mm-dd');
}
//merit badges
for (linkBdg = 0; linkBdg < counselor.mbs.length; linkBdg++) {
if ( counselor.mbs[linkBdg].status == 'Active' || counselor.mbs[linkBdg].status == '') {
link += '&entry.2018708776=' + counselor.mbs[linkBdg]['badge'].toString().replace(' ', '+');
}
}
return link;
}
/**
* Determine if value is a date
*
* @param {variant} value, What is checked to detremine if it is a date.
* @return {boolean} true if value is a date.
*/
function isDate(value) {
return Date.parse(value) > 0;
}
/**
* Format date value
*
* @param {date} value, The date to be formated.
* @param {string} format, The format of the date.
* @return {string} a formated date.
*/
function formatDate(value, format) {
if (isDate(value)) {
var day = value.getDate();
var month = value.getMonth() + 1;
var year = value.getFullYear();
switch (format) {
case 'mm/dd/yyyy':
return month + '/' + day + '/' + year;
break;
case 'yyyy-mm-dd':
return year + '-' + leadingZero(month,2) + '-' + leadingZero(day,2);
break;
default:
return value;
}
} else {
return value;
}
}
/**
* Convert a number to a string with leading zeros
*
* @param {number} value
* @param {number} expectedLength
* @return {string} formated number
*/
function leadingZero(value, expectedLength) {
var zeroStr = value.toString();
for (charLength = value.toString().length; charLength < expectedLength; charLength++) {
zeroStr = "0" + zeroStr;
}
return zeroStr;
}