-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
333 lines (302 loc) · 9.4 KB
/
index.ts
File metadata and controls
333 lines (302 loc) · 9.4 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
// Note: getVersionInfo, VERSION, COMMIT are defined in src/version.ts
// Apps Script will compile all .ts files together, making them globally available
let activeUserLocale = Session.getActiveUserLocale().split("_")[0];
const supportedLocales = ["en", "es"];
const defaultLocale = "en";
let locale = supportedLocales.includes(activeUserLocale)
? activeUserLocale
: defaultLocale;
function onOpen() {
// Log version info to Apps Script console
// VERSION and COMMIT constants are defined in src/version.ts
if (typeof VERSION !== 'undefined' && typeof COMMIT !== 'undefined') {
console.log(`CoMapeo Config Spreadsheet Plugin v${VERSION} (${COMMIT})`);
if (typeof getVersionInfo !== 'undefined') {
console.log(`Full version: ${getVersionInfo()}`);
}
}
const ui = SpreadsheetApp.getUi();
const mainMenu = ui.createMenu(menuTexts[locale].menu)
.addItem(
menuTexts[locale].translateCoMapeoCategory,
"translateCoMapeoCategory",
)
.addItem(menuTexts[locale].generateIcons, "generateIcons")
.addSeparator()
.addItem(menuTexts[locale].generateCoMapeoCategory, "generateCoMapeoCategory")
.addItem(menuTexts[locale].importCoMapeoCategory, "importCoMapeoCategory")
.addSeparator()
.addItem(menuTexts[locale].lintAllSheets, "lintAllSheets")
.addItem(menuTexts[locale].cleanAllSheets, "cleanAllSheets")
.addSeparator();
const debugMenu = ui.createMenu(menuTexts[locale].debugMenuTitle)
.addItem("Create Test Spreadsheet for Regression", "createTestSpreadsheetForRegression")
.addItem("Test Runner", "runAllTests")
.addItem("Capture Baseline Performance Metrics", "captureAndDocumentBaselineMetrics")
.addItem("Turn on legacy compatibility", "toggleLegacyCompatibility")
.addItem(
menuTexts[locale].generateCoMapeoCategoryDebug,
"generateCoMapeoCategoryDebug",
);
mainMenu
.addSubMenu(debugMenu)
.addSeparator()
.addItem(menuTexts[locale].openHelpPage, "openHelpPage")
.addItem("About / Version", "showVersionInfo")
.addToUi();
// Add developer menu in development environment
if (
PropertiesService.getScriptProperties().getProperty("ENVIRONMENT") ===
"development"
) {
ui.createMenu("Developer")
.addItem("Test Format Detection", "testFormatDetection")
.addItem("Test Translation Extraction", "testTranslationExtraction")
.addItem("Test Category Import", "testImportCategory")
.addItem("Test Details and Icons", "testDetailsAndIcons")
.addItem("Test Field Extraction", "testFieldExtraction")
.addSeparator()
.addItem("Run All Tests", "runAllTests")
.addItem("Capture Baseline Metrics", "captureAndDocumentBaselineMetrics")
.addItem("Generate Performance Report", "generatePerformanceReport")
.addSeparator()
.addItem("Clear Language Cache", "clearLanguagesCacheMenuItem")
.addToUi();
}
}
function translateCoMapeoCategory() {
try {
showSelectTranslationLanguagesDialog();
} catch (error) {
SpreadsheetApp.getUi().alert(
translateMenuTexts[locale].error,
translateMenuTexts[locale].errorText + error.message,
SpreadsheetApp.getUi().ButtonSet.OK,
);
}
}
function translateToSelectedLanguages(selectedLanguages: string[]) {
const ui = SpreadsheetApp.getUi();
try {
autoTranslateSheetsBidirectional(selectedLanguages as TranslationLanguage[]);
ui.alert(
translateMenuTexts[locale].completed,
translateMenuTexts[locale].completedText,
ui.ButtonSet.OK,
);
} catch (error) {
ui.alert(
translateMenuTexts[locale].error,
translateMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
// This function is defined in generateCoMapeoConfig.ts and will be available globally
function generateIcons() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
iconMenuTexts[locale].action,
iconMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
generateIconsConfig();
} catch (error) {
ui.alert(
iconMenuTexts[locale].error,
iconMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function showVersionInfo() {
const ui = SpreadsheetApp.getUi();
// getVersionInfo is defined in src/version.ts
const versionInfo = typeof getVersionInfo !== 'undefined'
? getVersionInfo()
: (typeof VERSION !== 'undefined' ? VERSION : 'Unknown');
ui.alert(
"CoMapeo Config Spreadsheet Plugin",
`Version: ${versionInfo}\n\nRepository: https://github.com/digidem/comapeo-config-spreadsheet-plugin`,
ui.ButtonSet.OK
);
}
function generateCoMapeoCategory() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
categoryMenuTexts[locale].action,
categoryMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
generateCoMapeoConfig();
} catch (error) {
ui.alert(
categoryMenuTexts[locale].error,
categoryMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function generateCoMapeoCategoryDebug() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
categoryDebugMenuTexts[locale].action,
categoryDebugMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
generateCoMapeoConfigWithDriveWrites();
} catch (error) {
ui.alert(
categoryDebugMenuTexts[locale].error,
categoryDebugMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function importCoMapeoCategory() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
importMenuTexts[locale].action,
importMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
importCoMapeoCatFile();
} catch (error) {
ui.alert(
importMenuTexts[locale].error,
importMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function lintCoMapeoCategory() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
lintMenuTexts[locale].action,
lintMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
lintAllSheets();
ui.alert(
lintMenuTexts[locale].completed,
lintMenuTexts[locale].completedText,
ui.ButtonSet.OK,
);
} catch (error) {
ui.alert(
lintMenuTexts[locale].error,
lintMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function cleanAllSheets() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
cleanAllMenuTexts[locale].action,
cleanAllMenuTexts[locale].actionText,
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
removeTranslationAndMetadataSheets();
ui.alert(
cleanAllMenuTexts[locale].completed,
cleanAllMenuTexts[locale].completedText,
ui.ButtonSet.OK,
);
} catch (error) {
ui.alert(
cleanAllMenuTexts[locale].error,
cleanAllMenuTexts[locale].errorText + error.message,
ui.ButtonSet.OK,
);
}
}
}
function openHelpPage() {
showHelpDialog();
}
/**
* Menu item handler for clearing language cache
* Useful for debugging or forcing fresh language data fetch
*/
function clearLanguagesCacheMenuItem() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
"Clear Language Cache",
"This will clear the cached language data, forcing a fresh fetch from the remote source on next use. This is useful for debugging. Continue?",
ui.ButtonSet.YES_NO,
);
if (result === ui.Button.YES) {
try {
clearLanguagesCache();
ui.alert(
"Cache Cleared",
"Language cache has been successfully cleared. Next language operation will fetch fresh data.",
ui.ButtonSet.OK,
);
} catch (error) {
ui.alert(
"Error",
"An error occurred while clearing the cache: " + error.message,
ui.ButtonSet.OK,
);
}
}
}
/**
* Toggle legacy compatibility flag in metadata sheet
* Adds or updates legacyCompat key with TRUE/FALSE value
*/
function toggleLegacyCompatibility() {
const ui = SpreadsheetApp.getUi();
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
let metadataSheet = spreadsheet.getSheetByName('Metadata');
if (!metadataSheet) {
// Create Metadata sheet if it doesn't exist
metadataSheet = spreadsheet.insertSheet('Metadata');
metadataSheet.getRange(1, 1, 1, 2).setValues([['Key', 'Value']]).setFontWeight('bold');
}
const data = metadataSheet.getDataRange().getValues();
let currentValue = 'FALSE';
let rowIndex = -1;
// Find existing legacyCompat key
for (let i = 1; i < data.length; i++) {
if (String(data[i][0]).trim() === 'legacyCompat') {
currentValue = String(data[i][1]).trim().toUpperCase();
rowIndex = i;
break;
}
}
// Toggle value
const newValue = currentValue === 'TRUE' ? 'FALSE' : 'TRUE';
if (rowIndex === -1) {
// Append new row
metadataSheet.appendRow(['legacyCompat', newValue]);
} else {
// Update existing row
metadataSheet.getRange(rowIndex + 1, 2).setValue(newValue);
}
// Show confirmation
ui.alert(
'Legacy Compatibility',
`Legacy compatibility has been turned ${newValue === 'TRUE' ? 'ON' : 'OFF'}.`,
ui.ButtonSet.OK
);
}