-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingSheet.js
66 lines (57 loc) · 1.82 KB
/
SettingSheet.js
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
function getSettingSheet() {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SETTING_SHEET);
}
function showSettingDialog() {
const settings = getSettings(); // Fetch existing settings
const html = HtmlService.createHtmlOutputFromFile('SettingDialog')
.setWidth(550)
.setHeight(240)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.append(`<script>window.settings = ${JSON.stringify(settings)};</script>`);
SpreadsheetApp.getUi().showModalDialog(html, 'Settings');
}
function getSettings() {
const sheet = getOrCreateSettingSheet();
const data = sheet.getDataRange().getValues();
const settings = {};
// Map key-value pairs from the settings table
for (let i = 0; i < data.length; i++) {
settings[data[i][0]] = data[i][1]; // Key is in column 1, value in column 2
}
return settings;
}
function getOrCreateSettingSheet() {
let sheet = getSettingSheet();
if (!sheet) {
sheet = initSettingSheet(SETTING_SHEET);
}
return sheet;
}
function getSetting(key) {
const sheet = getOrCreateSettingSheet();
const data = sheet.getDataRange().getValues();
const row = data.find(row => row[0] === key);
return row ? row[1] : null;
}
function saveSettings(parameters) {
const sheet = getSettingSheet();
// Iterate through the parameters and save them to the .SETTINGS sheet
Object.keys(parameters).forEach(key => {
const rowIndex = findRowByKey(sheet, key);
if (rowIndex > 0) {
// Update existing parameter
sheet.getRange(rowIndex, 2).setValue(parameters[key]);
} else {
alert("Unknown " + key);
}
});
}
function findRowByKey(sheet, key) {
const data = sheet.getDataRange().getValues();
for (let i = 0; i < data.length; i++) {
if (data[i][0] === key) {
return i + 1; // Return row index (1-based)
}
}
return -1; // Key not found
}