-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
387 lines (323 loc) · 12.7 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Barrie Toastmasters ~ Executive Meeting Minutes Helper
// sloppyright Paul Will Gamble, 2020
// paulywill.com | https://github.com/paulywill/barrie-toastmasters-google-action-script
// ----------------------------------------------------------------------------------------
// Modeled in part after this fantastic tutorial by Kurt Kaiser
// ref:https://kurtkaiser.us/how-to-code-a-html-user-interface-in-google-apps-script/
// UI Sheets Email Notifications
// Kurt Kaiser
// kurtkaiser.us
// All Rights Reserved, 2019
// ----------------------------------------------------------------------------------------
//GLOBAL VARIABLES
var sp = PropertiesService.getScriptProperties();
var headers = Sheets.Spreadsheets.Values.get('1ldvFVn7wGK0ccafr6bv7-mYjn_GyUuDuwI8o5U_KNFg', 'A1:C1');
var data = Sheets.Spreadsheets.Values.get('1ldvFVn7wGK0ccafr6bv7-mYjn_GyUuDuwI8o5U_KNFg', 'A2:C8');
Logger.log(headers);
Logger.log(data);
// ------------------------ Replace Placeholders -----------------------
function replaceText(){
/*
PLACEHOLDERS PROPERTIES AND TEST DATA
============ ========================
{Names_Present},
{Names_Regrets}
role1Hidden=true settingRole1=Fleurette Knaggs ( President )
role2Hidden=false settingRole2=Dan Bell ( VP of Edu. )
role3Hidden=true settingRole3=Jennifer Keresztesi ( VP of Mem. )
role4Hidden=false settingRole4=Shelly Grant ( VP of PR )
role5Hidden=true settingRole5=Brian MacGregor ( Treasurer )
role6Hidden=false settingRole6=Rufus Willett ( SAA )
role7Hidden=true settingRole7=Paul Gamble ( Secretary )
{Meeting_Date} meetingDate=2020-08-04
{Meeting_Location} meetingLocation=Zoom
{Meeting_startTime} startTime=17:45
{Meeting_endTime} endTime=18:46
{Name_Chair} meetingChair=Fleurette Knaggs
{Name_Secretary} meetingRecorder=Paul Gamble
{Next_Date} nextDate=2020-09-01
{Next_Location} nextLocation=Zoom
{Next_Time} nextStart=17:45
*/
Logger.log('replaceText');
let doc = DocumentApp.getActiveDocument().getBody();
let properties = getAllProperties();
let present = [];
let regrets = [];
let meeting = {
Meeting_Date: "meetingDate",
Meeting_Location: "meetingLocation",
Meeting_startTime: "startTime",
Meeting_endTime: "endTime",
Name_Chair: "meetingChair",
Name_Secretary: "meetingRecorder",
Next_Date: "nextDate",
Next_Location: "nextLocation",
Next_Time: "nextStart"
}
//logic for 'present' and 'regrets'
for (i = 1; i < 8; i++){
if(properties['role'+i+'Hidden'] == 'true'){
present.push(' ' + properties['settingRole'+i]);
} else {
regrets.push(' ' + properties['settingRole'+i]);
}
}
//replace {Names_Present} placeholder with array
var foundPresent = doc.findText('{Names_Present}');
var elemPresent = foundPresent.getElement();
elemPresent.setForegroundColor("#000000");
doc.replaceText('{Names_Present}', present);
//replace '{Names_Regrets}' placeholder with array
var foundRegrets = doc.findText('{Names_Regrets}');
var elemRegrets = foundRegrets.getElement();
elemRegrets.setForegroundColor("#000000");
doc.replaceText('{Names_Regrets}', regrets);
//logic for replace placeholders with properties
for (const [key, value] of Object.entries(meeting)) {
//Logger.log(`${key}: ${value}`);
var found = doc.findText(`{${key}}`);
var elem = found.getElement();
elem.setForegroundColor("#000000");
//format date entry for readability
if(key == 'Meeting_Date'){
doc.replaceText(`{${key}}`, formatDate(properties[value]));
}
if(key.includes('Time')){
doc.replaceText(`{${key}}`, formatTime(properties[value]));
}
doc.replaceText(`{${key}}`, properties[value]);
}
};
// ------------------------ Submit Item to Table -----------------------
function submitItem(form) {
Logger.log('submitItem');
var properties = getAllProperties();
let doc = DocumentApp.getActiveDocument().getBody();
var inputNames = ['Done','ASAP'];
let itemDateOption;
Logger.log('properties.itemDate: ' + properties.itemDate);
if(properties.itemDate == 'Done' || 'ASAP'){
Logger.log("There's a match.");
itemDateOption = properties.itemDate;
}else{
Logger.log("Date Format");
itemDateOption = formatDate(properties.itemDate);
}
var cells = [
properties.itemTitle,
properties.itemPoints,
properties.itemAction,
itemDateOption,
properties.itemLeadStaff
];
let tables = doc.getTables()
//Second table holds meeting items; first one the meeting details
let table1 = tables[1]
//doc.replaceText('{Item_Entry}', '');
let lastRow = table1.getNumRows();
let addRow1 = table1.insertTableRow(lastRow, );
//setup for non bold styling
var style = {};
style[DocumentApp.Attribute.BOLD] = false;
cells.forEach(function(e, i){
addRow1.insertTableCell(i, e);
//set normal (i.e. not bold) for the first cell (TESTING)
});
//setup for border around row
var tableStyle = {};
tableStyle[DocumentApp.Attribute.BORDER_WIDTH] = 1;
tableStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
//tableStyle[DocumentApp.Attribute.BOLD] = false;
table1.setAttributes(tableStyle);
}
// ------------------------ Submit and Save ----------------------
function submitAndSaveItem(form) {
//input form then submit to table
itemInput(form);
submitItem(form);
}
// ------------------------ Create Menu --------------------------
function onOpen(e){
DocumentApp.getUi()
.createMenu("TM Executive Meeting Helper")
.addItem('Meeting details', 'showDetails')
.addItem('Item/Tasks', 'showTasks')
.addSeparator()
.addItem('Settings', 'showSettings')
.addItem('Delete data', 'deleteData')
.addItem('Help', 'showHelp')
.addToUi();
}
// ------------------------ Save Details ------------------------
function meetingInput(form) {
Logger.log('meetingInput');
Logger.log(form);
sp.setProperty('meetingLocation', form.meetingLocation);
sp.setProperty('meetingDate', form.meetingDate);
sp.setProperty('startTime', form.startTime);
sp.setProperty('endTime', form.endTime);
sp.setProperty('meetingChair', form.meetingChair);
sp.setProperty('meetingRecorder', form.meetingRecorder);
sp.setProperty('nextLocation', form.nextLocation);
sp.setProperty('nextDate', form.nextDate);
sp.setProperty('nextStart', form.nextStart);
for (i = 1; i <8; i++){
sp.setProperty('role'+i+'Hidden', form['role'+i+'Hidden']);
}
}
// ------------------------ Save Settings -----------------------
function settingsInput(form) {
Logger.log('settingsInput');
Logger.log(form);
//loop through and save all names for roles
for (i = 1; i < 8; i++){
sp.setProperty('settingRole'+i, form['settingRole'+i]);
}
//loop through and save all the names of locations
for (i = 1; i < 6; i++){
sp.setProperty('settingLocation'+i, form['settingLocation'+i]);
}
}
// ------------------------ Test Email Settings -----------------------
/*function testEmailSettings() {
Logger.log('testEmailSettings');
var properties = getAllProperties();
//loop through and save all names for roles
for (i = 1; i < 8; i++){
Logger.log(properties['settingRole' + i] + ': ' + properties['settingRole' + i + 'Email']);
}
}
*/
// ------------------------ Email Settings -----------------------
function emailInput(form) {
Logger.log('emailInput');
Logger.log('form.emailAddress: '+ form.emailAddress);
var properties = getAllProperties();
var roleEmail = properties.emailSettingOpen+'Email';
Logger.log('roleEmail: '+ roleEmail);
sp.setProperty(roleEmail,form.emailAddress);
}
// ------------------------ Save Item Details --------------------
function itemInput(form) {
Logger.log('itemInput');
Logger.log(form);
if(form.itemDate !== null && form.itemDate !== ''){
itemDate = form.itemDate;
}
//set properties for all the item details
sp.setProperty('itemTitle', form.itemTitle);
sp.setProperty('itemPoints', form.itemPoints);
sp.setProperty('itemAction', form.itemAction);
//logic for if the 'Done' or 'ASAP' checkboxes are selected instead of date input
if(form.DoneHidden == 'true' | form.ASAPHidden == 'true'){
if(form.DoneHidden == 'true'){
sp.setProperty('itemDate', 'Done');
} else if(form.ASAPHidden == 'true'){
sp.setProperty('itemDate', 'ASAP');
}
} else{
if (form.itemDate){
sp.setProperty('itemDate', form.itemDate);
}
}
//logic for selecting more than one lead staff
var leadArray = [];
leadArray = form.itemLeadStaff;
if (Array.isArray(leadArray)){
var joinLeadArray = leadArray.join(', ');
sp.setProperty('itemLeadStaff', joinLeadArray);
} else {
sp.setProperty('itemLeadStaff', form.itemLeadStaff != null ? form.itemLeadStaff : '' );
}
}
// --------------- Returns Array of All Properties ---------------------
function getAllProperties() {
var propertiesAndKeys = {}
var data = sp.getProperties();
for (var key in data) {
propertiesAndKeys[key] = sp.getProperty(key);
//Logger.log('Key: %s - %s', key, data[key]);
}
return propertiesAndKeys;
}
// --------------- Show Meeting Details Sidebar ------------------------
function showDetails() {
var html = HtmlService.createHtmlOutputFromFile('detailsSidebar')
.setTitle('Meeting Details')
.setWidth(550);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
// --------------- Show Item/Tasks Sidebar -----------------------------
function showTasks() {
var html = HtmlService.createHtmlOutputFromFile('tasksSidebar')
.setTitle('Tasks')
.setWidth(300);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
// --------------- Open Item/Tasks Popup Dialog ------------------------
function showTaskDialog() {
var html = HtmlService.createHtmlOutputFromFile('taskDialog')
.setWidth(600)
.setHeight(575);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Item/Tasks');
}
// --------------- Open Email Popup Dialog ----------------------------
function showEmailDialog(emailRole, roleTitle) {
//TESTING ---> some refactoring required
Logger.log('showEmailDialog(emailRole): '+ emailRole);
Logger.log('showEmailDialog(roleTitle): '+ roleTitle);
sp.setProperty('emailSettingOpen', emailRole);
sp.setProperty('emailSettingTitle', roleTitle);
var html = HtmlService.createHtmlOutputFromFile('emailDialog')
.setWidth(500)
.setHeight(300);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Email setting');
}
// --------------- Show Settings Sidebar -------------------------------
function showSettings() {
//TESTING: email settings
//testEmailSettings();
var html = HtmlService.createHtmlOutputFromFile('settingsSidebar')
.setTitle('Settings')
.setWidth(550);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
// --------------- Show Help Sidebar -----------------------------------
function showHelp() {
var html = HtmlService.createHtmlOutputFromFile('helpSidebar')
.setTitle('Help')
.setWidth(550);
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
// --------------- Wait Function ---------------------------------------
// Limits the Saved message in the sidebar to 6 seconds
function waitSeconds() {
Utilities.sleep(6000);
}
// --------------- Delete all saved data -------------------------------
// Deletes all properties
function deleteData() {
sp.deleteAllProperties();
}
// --------------- Format entries --------------------------------------
//format date for readability; ref ~ https://stackoverflow.com/a/31732581
function formatDate(ISOdate) {
var date = new Date(ISOdate.replace(/-/g, '\/'));
return date.toDateString();
}
//format time for readability; ref ~ https://stackoverflow.com/a/13898483
function formatTime(militaryTime) {
var timeString = militaryTime;
var H = +timeString.substr(0, 2);
var h = (H % 12) || 12;
var ampm = H < 12 ? "AM" : "PM";
timeString = h + timeString.substr(2, 3) + ' '+ ampm;
return timeString;
}