-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.js
More file actions
140 lines (129 loc) · 4.73 KB
/
Copy pathConfig.js
File metadata and controls
140 lines (129 loc) · 4.73 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
/**
* Config.gs
* Global Constants & Shared Utilities
*/
const APP_TITLE = "Order System";
const CURRENT_VERSION = "v0.9.23";
const SHEET_NAMES = {
DASHBOARD: "DASHBOARD",
WELCOME: "Welcome",
ORDERS_EXPORT: "ORDERS_EXPORT",
SETTINGS: "SETTINGS",
ORDERS: "ORDERS",
CLIENT_DATA: "CLIENT DATA",
PRODUCTS: "PRODUCTS",
DELETED_PRODUCTS: "DELETED_PRODUCTS",
DAILY_OPERATIONS: "DAILY_OPERATIONS",
EXPORT_SUMMARY: "EXPORT",
CLIENT_INFO_UPDATES: "CLIENT_INFO_UPDATES"
};
/**
* Order Form PDF — Brand Colour Config
* Edit these to change the look of all generated Order Form PDFs.
* Layout is still read dynamically from ORDER_FORM_1; only colours are fixed here.
*/
const ORDER_FORM_COLORS = {
categoryBg: '#cc66cc', // Pink/magenta category header background
categoryText: '#ffffff', // White text on category headers
mcPriceBg: '#ffff00', // Yellow highlight for MC price cells
totalText: '#1a6b2a', // Green for Total $ values
accentBorder: '#b050b0', // Darker purple for borders / header outline
};
/**
* Shared Utilities
*/
const superNormalize = (s) => String(s || "").toLowerCase().replace(/[^a-z0-9]/g, '').replace(/s$/, '');
function columnToLetter(column) {
if (column < 1) return "A";
let temp, letter = "";
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
function getSheet(sheetName) {
// In web app context, getActiveSpreadsheet() may fail or return wrong spreadsheet.
// For container-bound scripts, we can use getActive() which should work.
// If this fails, fall back to opening by ID from script properties.
let ss;
try {
ss = SpreadsheetApp.getActiveSpreadsheet();
} catch (e) {
// Fallback: Try to get the spreadsheet this script is bound to
const scriptId = ScriptApp.getScriptId();
const file = DriveApp.getFileById(scriptId);
const parentId = file.getParents().next().getId();
ss = SpreadsheetApp.openById(parentId);
}
if (!ss) {
throw new Error("Could not access the spreadsheet. Please ensure the script is bound to a spreadsheet.");
}
let sheet = ss.getSheetByName(sheetName);
if (!sheet) {
// Fallback: Case-insensitive search
const sheets = ss.getSheets();
sheet = sheets.find(s => s.getName().toLowerCase() === sheetName.toLowerCase());
}
if (!sheet) throw new Error(`Sheet "${sheetName}" not found. Available: ${ss.getSheets().map(s => s.getName()).join(', ')}`);
return sheet;
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
/**
* Return the sheet name for a given Order Form number.
* Reads from SETTINGS: key = "FORM_{n}_SHEET", value = sheet name.
* Falls back to "ORDER_FORM_{n}" if not configured.
*/
function getOrderFormSheetName(formNum) {
const key = 'FORM_' + formNum + '_SHEET';
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName(SHEET_NAMES.SETTINGS);
if (settingsSheet) {
const data = settingsSheet.getDataRange().getValues();
for (let i = 0; i < data.length; i++) {
if (String(data[i][0]).trim().toUpperCase() === key.toUpperCase()) {
const val = String(data[i][1] || '').trim();
if (val) return val;
}
}
}
} catch (e) { /* ignore — fall through to default */ }
return 'ORDER_FORM_' + formNum;
}
/**
* Return all configured Order Form template mappings for the Admin UI.
* Reads every "FORM_N_SHEET" row from SETTINGS.
* Always includes at least Form 1 as a default.
*/
function getOrderFormTemplates() {
const templates = [];
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName(SHEET_NAMES.SETTINGS);
if (settingsSheet) {
const data = settingsSheet.getDataRange().getValues();
data.forEach(row => {
const key = String(row[0] || '').trim();
const val = String(row[1] || '').trim();
const m = key.match(/^FORM_(\d+)_SHEET$/i);
if (m && val) {
templates.push({
formNum: m[1], sheetName: val,
label: 'Form ' + m[1] + ' \u2014 ' + val
});
}
});
}
} catch (e) { /* ignore */ }
if (templates.length === 0) {
templates.push({
formNum: '1', sheetName: 'ORDER_FORM_1',
label: 'Form 1 \u2014 ORDER_FORM_1'
});
}
return templates;
}