-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-apps-script.js
More file actions
556 lines (532 loc) · 18.2 KB
/
google-apps-script.js
File metadata and controls
556 lines (532 loc) · 18.2 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//what language your data is in (this affects days, types, and addresses) - en, es, fr, ja
const language = "ja";
//if you have a link column, customize this so the link redirects to the meeting detail page on your site
const baseUrl = "https://tsml-ui.code4recovery.org/tests/japan.html?meeting=";
//ok to make these a flat array if you need to customize (they are like this for copy/paste-ability from other projects)
const days = Object.values(
{
en: {
friday: "Friday",
monday: "Monday",
saturday: "Saturday",
sunday: "Sunday",
thursday: "Thursday",
tuesday: "Tuesday",
wednesday: "Wednesday",
},
es: {
friday: "Viernes",
monday: "Lunes",
saturday: "Sábado",
sunday: "Domingo",
thursday: "Jueves",
tuesday: "Martes",
wednesday: "Miércoles",
},
fr: {
friday: "Vendredi",
monday: "Lundi",
saturday: "Samedi",
sunday: "Dimanche",
thursday: "Jeudi",
tuesday: "Mardi",
wednesday: "Mercredi",
},
ja: {
friday: "金曜日",
monday: "月曜日",
saturday: "土曜日",
sunday: "日曜日",
thursday: "木曜日",
tuesday: "火曜日",
wednesday: "水曜日",
},
}[language]
);
//ok to make these a flat array if you need to customize (they are like this for copy/paste-ability from other projects)
const types = Object.values(
{
en: {
11: "11th Step Meditation",
"12x12": "12 Steps & 12 Traditions",
//active: 'Active',
"AL-AN": "Concurrent with Al-Anon",
A: "Secular",
ABSI: "As Bill Sees It",
AL: "Concurrent with Alateen",
ASL: "American Sign Language",
B: "Big Book",
BA: "Babysitting Available",
BE: "Newcomer",
BRK: "Breakfast",
BI: "Bisexual",
C: "Closed",
CAN: "Candlelight",
CF: "Child-friendly",
D: "Discussion",
DB: "Digital Basket",
DD: "Dual Diagnosis",
DR: "Daily Reflections",
EN: "English",
FF: "Fragrance Free",
FR: "French",
G: "Gay",
GR: "Grapevine",
H: "Birthday",
HE: "Hebrew",
//inactive: 'Inactive',
//'in-person': 'In-person',
ITA: "Italian",
JA: "Japanese",
KOR: "Korean",
L: "Lesbian",
LGBTQ: "LGBTQ",
LIT: "Literature",
LS: "Living Sober",
M: "Men",
MED: "Meditation",
N: "Native American",
NDG: "Indigenous",
O: "Open",
//online: 'Online',
OUT: "Outdoor",
P: "Professionals",
POC: "People of Color",
POL: "Polish",
POR: "Portuguese",
PUN: "Punjabi",
RUS: "Russian",
S: "Spanish",
SEN: "Seniors",
SM: "Smoking Permitted",
SP: "Speaker",
ST: "Step Study",
T: "Transgender",
TC: "Location Temporarily Closed",
TR: "Tradition Study",
W: "Women",
X: "Wheelchair Access",
XB: "Wheelchair-accessible Bathroom",
XT: "Cross Talk Permitted",
Y: "Young People",
},
es: {
11: "Meditación del Paso 11",
"12x12": "12 Pasos y 12 Tradiciones",
//active: 'Activo',
"AL-AN": "Concurrente con Al-Anon",
A: "Secular",
ABSI: "Como lo ve Bill",
AL: "Concurrente con Alateen",
ASL: "Lenguaje por señas",
B: "Libro Grande",
BA: "Guardería disponible",
BE: "Principiantes",
BI: "Bisexual",
BRK: "Desayuno",
C: "Cerrada",
CAN: "Luz de una vela",
CF: "Niño amigable",
D: "Discusión",
DB: "Canasta digital",
DD: "Diagnóstico dual",
DR: "Reflexiones Diarias",
EN: "Inglés",
FF: "Sin fragancia",
FR: "Francés",
G: "Gay",
GR: "La Viña",
H: "Cumpleaños",
HE: "Hebreo",
//inactive: 'Inactiva',
//'in-person': 'En persona',
ITA: "Italiano",
JA: "Japonés",
KOR: "Coreano",
L: "Lesbianas",
LGBTQ: "LGBTQ",
LIT: "Literatura",
LS: "Viviendo Sobrio",
M: "Hombres",
MED: "Meditación",
N: "Nativo Americano",
NDG: "Indígena",
O: "Abierta",
//online: 'En Línea',
OUT: "Al aire libre",
P: "Profesionales",
POC: "Gente de color",
POL: "Polaco",
POR: "Portugués",
PUN: "Punjabi",
RUS: "Ruso",
S: "Español",
SEN: "Personas mayores",
SM: "Se permite fumar",
SP: "Orador",
ST: "Estudio de pasos",
T: "Transgénero",
TC: "Ubicación temporalmente cerrada",
TR: "Estudio de tradicion",
W: "Mujer",
X: "Acceso en silla de ruedas",
XB: "Baño accesible para sillas de ruedas",
XT: "Se permite opinar",
Y: "Gente joven",
},
fr: {
11: "Méditation sur la 11e Étape",
"12x12": "12 Étapes et 12 Traditions,",
//active: 'Actives',
"AL-AN": "En même temps qu’Al-Anon",
A: "Séculier",
ABSI: "Réflexions de Bill",
AL: "En même temps qu’Alateen",
ASL: "Langage des Signes",
B: "Gros Livre",
BA: "Garderie d’enfants disponible",
BE: "Nouveau/nouvelle",
BI: "Bisexuel",
BRK: "Petit déjeuner",
C: "Fermé",
CAN: "À la chandelle",
CF: "Enfants acceptés",
D: "Discussion",
DB: "Panier numérique",
DD: "Double diagnostic",
DR: "Réflexions quotidiennes",
EN: "Anglais",
FF: "Sans parfum",
FR: "Français",
G: "Gai",
GR: "Grapevine",
H: "Anniversaire",
HE: "Hébreu",
//inactive: 'Inactives',
//'in-person': 'En personne',
ITA: "Italien",
JA: "Japonais",
KOR: "Coréen",
L: "Lesbienne",
LGBTQ: "LGBTQ",
LIT: "Publications",
LS: "Vivre… Sans alcool",
M: "Hommes",
MED: "Méditation",
N: "Autochtone",
NDG: "Indigène",
O: "Ouvert(e)",
//online: 'En ligne',
OUT: "En plein air",
P: "Professionnels",
POC: "Gens de couleur",
POL: "Polonais",
POR: "Portugais",
PUN: "Pendjabi",
RUS: "Russe",
S: "Espagnol",
SEN: "Séniors",
SM: "Permis de fumer",
SP: "Conférencier",
ST: "Sur les Étapes",
T: "Transgenre",
TC: "Emplacement temporairement fermé",
TR: "Étude des Traditions",
W: "Femmes",
X: "Accès aux fauteuils roulants",
XB: "Toilettes accessibles aux fauteuils roulants",
XT: "Conversation croisée permise",
Y: "Jeunes",
},
ja: {
11: "第11ステップの瞑想",
"12x12": "12のステップと12の伝統",
//active: 'アクティブ',
"AL-AN": "アラノンと同時進行",
A: "世俗的な",
ABSI: "ビルの見方",
AL: "アラティーンと同時進行",
ASL: "アメリカの手話",
B: "ビッグブック",
BA: "託児あり",
BE: "新人",
BRK: "朝ごはん",
BI: "バイセクシャル",
C: "閉まっている",
CAN: "キャンドルライト",
CF: "子供に優しい",
D: "討論",
DB: "デジタルバスケット",
DD: "二重診断",
DR: "日々の振り返り",
EN: "英語",
FF: "無香料",
FR: "フランス語",
G: "ゲイ",
GR: "グレープバイン",
H: "誕生日",
HE: "ヘブライ語",
//inactive: '非活性',
//'in-person': '対面',
ITA: "イタリアの",
JA: "日本",
KOR: "韓国語",
L: "レズビアン",
LGBTQ: "LGBTQ",
LIT: "文学",
LS: "しらふ生活",
M: "男性",
MED: "瞑想",
N: "ネイティブアメリカン",
NDG: "先住民",
O: "開ける",
//online: 'オンライン',
OUT: "アウトドア",
P: "プロフェッショナル",
POC: "色の人々",
POL: "研磨",
POR: "ポルトガル語",
PUN: "パンジャブ語",
RUS: "ロシア",
S: "スペイン語",
SEN: "高齢者",
SM: "喫煙可",
SP: "スピーカー",
ST: "ステップスタディ",
T: "トランスジェンダー",
TC: "場所は一時的に閉鎖されています",
TR: "伝統研究",
W: "女性",
X: "車椅子アクセス",
XB: "車椅子対応バスルーム",
XT: "クロストーク許可",
Y: "若者たち",
},
}[language]
);
const errors = {
en: {
day: "Invalid day: %d%",
name: "Name is required",
slug_required: "Slug is required",
slug_unique: "Slug must be unique",
types: "Invalid types: %t%",
},
es: {
day: "Día inválido: %d%",
name: "Se requiere el nombre",
slug_required: "Se requiere babosa",
slug_unique: "Babosa debe ser única",
types: "Tipos no válidos: %t%",
},
fr: {
day: "Jour invalide : %d%",
name: "Le nom est requis",
slug_required: "Slug est requis",
slug_unique: "Slug doit être unique",
types: "Types non valides : %t%",
},
ja: {
day: "無効な日: %d%",
name: "お名前必須",
slug_required: "スラグが必要です",
slug_unique: "スラッグは一意でなければなりません",
types: "無効なタイプ: %t%",
},
}[language];
function getColumnValues(sheet, headers, header) {
const columnIndex = headers.indexOf(header);
if (columnIndex === -1) return [];
return sheet
.getRange(2, columnIndex + 1, sheet.getLastRow() - 1)
.getValues()
.map((arr) => arr.pop());
}
function getHeaders(sheet) {
return sheet
.getSheetValues(1, 1, 1, sheet.getLastColumn())
.pop()
.map((value) =>
value.toString().trim().toLowerCase().replaceAll(" ", "_")
);
}
function isInvalid(sheet, headers, header, value) {
if (typeof value === "undefined") value = false;
if (header === "types") {
const invalidTypes = value
.split(",")
.map((type) => type.trim())
.filter((type) => !types.includes(type));
return invalidTypes.length
? errors.types.replace("%t%", invalidTypes.join(", "))
: null;
} else if (header === "day" && value && !days.includes(value)) {
return errors.day.replace("%d%", value);
} else if (header === "name" && !value) {
return errors.name;
} else if (header === "slug") {
if (!value) {
return errors.slug_required;
} else if (
getColumnValues(sheet, headers, "slug").filter((e) => e === value)
.length > 1
) {
return errors.slug_unique;
}
}
return null;
}
function onEdit(e) {
const sheet = SpreadsheetApp.getActiveSheet();
const row = e.range.getRow();
const value = e.range.getValue().toString();
const headers = getHeaders(sheet);
const header = headers[e.range.getColumn() - 1];
const rowValues = sheet.getRange(row, 1, 1, headers.length).getValues()[0];
//stop if first, after last, or row is empty
if (
row === 1 ||
row > sheet.getLastRow() ||
!rowValues.filter((e) => e).length
)
return;
//dynamic data
if (header === "name") {
setSlugIfEmpty(sheet, headers, row, value);
} else if (header === "formatted_address") {
if (value) {
const response = Maps.newGeocoder()
.setLanguage(language)
.geocode(value);
if (response.results.length) {
const {
address_components,
formatted_address,
geometry: { bounds, location, location_type },
} = response.results[0];
//set formatted_address column
setValue(
sheet,
headers,
"formatted_address",
row,
formatted_address
);
//set address column
const address = address_components
.filter(({ types }) =>
types.some((type) =>
[
"street_number",
"route",
"premise",
"sublocality_level_4",
"sublocality_level_3",
].includes(type)
)
)
.map(({ short_name }) => short_name)
.join(" ");
setValue(sheet, headers, "address", row, address);
//set coordinates column
const coordinates =
location_type === "APPROXIMATE"
? [
bounds.northeast.lat,
bounds.northeast.lng,
bounds.southwest.lat,
bounds.southwest.lng,
]
: [location.lat, location.lng];
setValue(
sheet,
headers,
"coordinates",
row,
coordinates
.map(
(coordinate) =>
Math.round(parseFloat(coordinate) * 100000) /
100000
)
.join()
);
//set regions column
const regions = address_components
.filter(({ types }) =>
types.some((type) =>
["locality", "sublocality_level_2"].includes(type)
)
)
.map(({ long_name }) => long_name)
.reverse()
.join(" > ");
setValue(sheet, headers, "regions", row, regions);
}
} else {
setValue(sheet, headers, "address", row, "");
setValue(sheet, headers, "coordinates", row, "");
setValue(sheet, headers, "regions", row, "");
}
} else if (header === "slug") {
const slug = slugify(value);
setValue(sheet, headers, "slug", row, slug);
setLink(sheet, headers, row, slug);
} else if (header === "types") {
const meetingTypes = value.split(",").map((type) => type.trim());
meetingTypes.sort();
setValue(sheet, headers, "types", row, meetingTypes.join(", "));
}
//set updated
if (rowValues.length > 1) {
const now = new Date();
const utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);
setValue(sheet, headers, "updated", row, utc);
} else {
setValue(sheet, headers, "updated", row, null);
}
//set note for invalid data
e.range.setNote(isInvalid(sheet, headers, header, value));
}
function setLink(sheet, headers, row, slug) {
const sheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
const value = `=HYPERLINK("https://sheets.code4recovery.org/${sheetId}?redirectTo=${encodeURIComponent(
baseUrl
)}${slug}", "LINK")`;
setValue(sheet, headers, "link", row, value);
}
function setSlugIfEmpty(sheet, headers, row, name) {
const column = headers.indexOf("slug");
if (column === -1) return;
const range = sheet.getRange(row, column + 1);
if (!range.getValue().toString().trim().length) {
const slug = slugify(name);
range.setValue(slug);
setLink(sheet, headers, row, slug);
}
}
function setValue(sheet, headers, header, row, value) {
const column = headers.indexOf(header);
if (column === -1) return;
const range = sheet.getRange(row, column + 1);
if (value !== range.getValue().toString()) {
range.setValue(value);
}
}
function slugify(str) {
str = str.trim().toLowerCase();
// remove accents, swap ñ for n, etc
const from = "åàáãäâèéëêìíïîòóöôùúüûñç·/_,:;";
const to = "aaaaaaeeeeiiiioooouuuunc------";
for (let i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
}
return (
str
//.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, "-") // collapse whitespace and replace by -
.replace(/-+/g, "-") // collapse dashes
.replace(/^-+/, "") // trim - from start of text
.replace(/-+$/, "")
); // trim - from end of text
}