forked from kevin-vaghasiya/restaurant-menu-webapp-gas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.ts
138 lines (114 loc) · 4.31 KB
/
App.ts
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
const doGet = (e: GoogleAppsScript.Events.DoGet) => {
const data = getData();
if (!data) return ContentService.createTextOutput('No data found');
const { settings, translations } = data;
const isCompactView = e.parameter.v && e.parameter.v == "compact";
const template = isCompactView ? HtmlService.createTemplateFromFile('menu_compact') : HtmlService.createTemplateFromFile('menu');
template.data = JSON.stringify(data);
template.logo_light = settings.logo_light;
template.logo_dark = settings.logo_dark;
template.bg_image = settings.bg_image;
template.order_now_actions = isCompactView ? settings.order_now_actions_compact : settings.order_now_actions;
template.translations = translations;
template.translations_obj = JSON.stringify(translations);
return template
.evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setTitle(`${settings.name} Menu`);
};
const getData = () => {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settings = getSettings(ss);
if (!settings) return null;
const translations = getTranslations(ss);
if (!translations) return null;
const categories = getCategories(ss);
const menu_items = getMenuItems(ss);
if (!menu_items) return null;
const data = {
settings,
translations,
categories,
menu_items,
};
return data;
};
const getSettings = (ss: GoogleAppsScript.Spreadsheet.Spreadsheet) => {
const settings_sheet = ss.getSheetByName(SHEET_NAMES.SETTINGS);
if (!settings_sheet) return null;
const data = settings_sheet.getDataRange().getValues();
const settings = {
name: data[0][1],
logo_light: data[1][1],
logo_dark: data[2][1],
currency: data[3][1].split(','),
order_now_actions: data[4][1],
order_now_actions_compact: data[5][1],
bg_image: data[6][1],
};
if (String(settings.logo_light).includes('drive.google.com')) {
const fileId = String(settings.logo_light).split('/d/')[1].split('/')[0];
const blob = DriveApp.getFileById(fileId).getBlob();
const base64 = Utilities.base64Encode(blob.getBytes());
settings.logo_light = `data:image/png;base64,${base64}`;
}
if (String(settings.logo_dark).includes('drive.google.com')) {
const fileId = String(settings.logo_dark).split('/d/')[1].split('/')[0];
const blob = DriveApp.getFileById(fileId).getBlob();
const base64 = Utilities.base64Encode(blob.getBytes());
settings.logo_dark = `data:image/png;base64,${base64}`;
}
return settings;
};
const getTranslations = (ss: GoogleAppsScript.Spreadsheet.Spreadsheet) => {
const translations_sheet = ss.getSheetByName(SHEET_NAMES.TRANSLATIONS);
if (!translations_sheet) return null;
const data = translations_sheet.getDataRange().getValues();
var translations = {};
for (let i = 1; i < data.length; i++) {
translations[data[i][0]] = data[i][1];
}
return translations;
};
const getMenuItems = (ss: GoogleAppsScript.Spreadsheet.Spreadsheet) => {
const menu_sheet = ss.getSheetByName(SHEET_NAMES.MENU);
if (!menu_sheet) return null;
const data = menu_sheet.getDataRange().getValues();
const menu_items: any = [];
for (let i = 1; i < data.length; i++) {
const [name, description, image, price, category, isAdditional] = data[i];
if (!name) continue;
const id = Math.random().toString(36).substring(2, 15);
const item = {
id,
name,
description,
image,
price,
category,
isAdditional,
};
if (String(image).includes('drive.google.com')) {
const fileId = String(image).split('/d/')[1].split('/')[0];
const blob = DriveApp.getFileById(fileId).getBlob();
const base64 = Utilities.base64Encode(blob.getBytes());
item.image = `data:image/png;base64,${base64}`;
}
menu_items.push(item);
}
return menu_items;
};
const getCategories = (ss: GoogleAppsScript.Spreadsheet.Spreadsheet) => {
const categories_sheet = ss.getSheetByName(SHEET_NAMES.CATEGORIES);
if (!categories_sheet) return [];
const data = categories_sheet.getDataRange().getValues();
const categories: any = [];
for (let i = 1; i < data.length; i++) {
const [item] = data[i];
const name = String(item).trim();
if (name) categories.push(name);
}
return categories;
};