-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonFunctions.js
264 lines (230 loc) · 7.5 KB
/
commonFunctions.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
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
const scriptProperties = PropertiesService.getScriptProperties();
const server = scriptProperties.getProperty("cred_server");
const port = Number.parseInt(scriptProperties.getProperty("cred_port"), 10);
const dbName = scriptProperties.getProperty("cred_dbName");
const username = scriptProperties.getProperty("cred_username");
const password = scriptProperties.getProperty("cred_password");
const url = `jdbc:mysql://${server}:${port}/${dbName}`;
const apidomain = scriptProperties.getProperty("cred_apidomain");
const apiusername = scriptProperties.getProperty("cred_apiusername");
const apipassword = scriptProperties.getProperty("cred_apipassword");
const cc_location = "Summit Up Climbing Centre";
const cell = setupCell("Dashboard", "B5");
const colors = {
lightRed: "#ffcccb",
lightGreen: "#90ee90",
lightYellow: "#ffd898",
lightBlue: "#ADD8E6",
grey: "#a9a9a9",
pink: "#ff75d8",
yellow: "#fad02c",
white: "#FFFFFF",
green: "#5CFF5C",
brightYellow: "#FFFF00",
};
function setupSheet(name) {
const spreadsheet = SpreadsheetApp.getActive();
const sheet = spreadsheet.getSheetByName(name);
sheet.clearContents();
sheet.clearFormats();
return sheet;
}
function setupCell(name, range) {
const spreadsheet = SpreadsheetApp.getActive();
const sheet = spreadsheet.getSheetByName(name);
const cellValue = sheet.getRange(range).getValue();
return cellValue;
}
function appendToSheet(sheet, results) {
const metaData = results.getMetaData();
const numCols = metaData.getColumnCount();
const rows = [];
// First row with column labels
const colLabels = [];
for (let col = 0; col < numCols; col++) {
colLabels.push(metaData.getColumnLabel(col + 1));
}
rows.push(colLabels);
// Remaining rows with results
while (results.next()) {
const row = [];
for (let col = 0; col < numCols; col++) {
row.push(results.getString(col + 1));
}
rows.push(row);
}
sheet.getRange(1, 1, rows.length, numCols).setValues(rows);
// Set the font size of the rows with column labels to 14
sheet.getRange(1, 1, 1, numCols).setFontSize(14);
sheet.autoResizeColumns(1, numCols);
}
function getColumnRange(sheet, columnHeader) {
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const columnIndex = headers.indexOf(columnHeader) + 1;
if (columnIndex === 0) {
throw new Error(`Column '${columnHeader}' not found in the sheet.`);
}
const lastRow = sheet.getLastRow();
const numRows = Math.max(5, lastRow - 1);
return sheet.getRange(2, columnIndex, numRows, 1);
}
function setColoursFormat(sheet, columnHeader, search, colour) {
const range = getColumnRange(sheet, columnHeader);
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains(search)
.setBackground(colour)
.setRanges([range])
.build();
const rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setTextFormat(sheet, columnHeader, search, colour) {
const range = getColumnRange(sheet, columnHeader);
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains(search)
.setFontColor(colour)
.setRanges([range])
.build();
const rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setWrapped(sheet, columnHeader) {
const range = getColumnRange(sheet, columnHeader);
range.setWrap(true);
sheet.setColumnWidth(range.getColumn(), 300); // Set column width to 300 pixels
}
function setColoursFormatLessThanOrEqualTo(
sheet,
columnHeader,
search,
colour,
) {
const numberSearch = Number(search);
const range = getColumnRange(sheet, columnHeader);
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberLessThanOrEqualTo(numberSearch)
.setBackground(colour)
.setRanges([range])
.build();
const rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setNumberFormat(sheet, columnHeader, format) {
const range = getColumnRange(sheet, columnHeader);
range.setNumberFormat(format);
}
function makeReport(stmt, reportConfig) {
const sheet = setupSheet(reportConfig.sheetName);
let query = reportConfig.query;
// Only replace ${cell} if it exists in the query
if (query.includes("${cell}")) {
if (Number.isNaN(Number(cell)) || cell === "") {
throw new Error("Invalid event selected - please try again");
}
query = query.replace(/\${cell}/g, cell);
}
const results = stmt.executeQuery(query);
appendToSheet(sheet, results);
if (reportConfig.formatting) {
for (const format of reportConfig.formatting) {
if (format.type === "color") {
setColoursFormat(sheet, format.column, format.search, format.color);
} else if (format.type === "text") {
setTextFormat(sheet, format.column, format.search, format.color);
} else if (format.type === "wrap") {
setWrapped(sheet, format.column);
} else if (format.type === "numberFormat") {
setNumberFormat(sheet, format.column, format.format);
} else if (format.type === "colorLessThanOrEqual") {
setColoursFormatLessThanOrEqualTo(
sheet,
format.column,
format.value,
format.color,
);
} else if (format.type === "columnWidth") {
setColumnWidth(sheet, format.column, format.width);
}
}
}
results.close();
}
function setColumnWidth(sheet, columnHeader, width) {
const range = getColumnRange(sheet, columnHeader);
sheet.setColumnWidth(range.getColumn(), width);
}
function refreshOutput() {
const conn = Jdbc.getConnection(url, username, password);
const stmt = conn.createStatement();
readOutput(stmt);
stmt.close();
conn.close();
}
function getIP() {
const url = "http://api.ipify.org";
const json = UrlFetchApp.fetch(url);
Logger.log(json);
}
function appendToSheetWithNoClear(sheet, results) {
const metaData = results.getMetaData();
const numCols = metaData.getColumnCount();
const rows = [];
// First row with column labels
const colLabels = [];
for (let col = 0; col < numCols; col++) {
colLabels.push(metaData.getColumnLabel(col + 1));
}
rows.push(colLabels);
// Remaining rows with results
while (results.next()) {
const row = [];
for (let col = 0; col < numCols; col++) {
row.push(results.getString(col + 1));
}
rows.push(row);
}
// Find the last row containing a value
const lastRow = sheet.getDataRange().getLastRow();
// Set the values of the rows starting from the row below the last row containing a value
// or the top row if it is empty
let startRow = lastRow + 1;
const topRowValues = sheet.getRange(1, 1, 1, numCols).getValues();
if (topRowValues[0].every((value) => value === "")) {
startRow = 1;
}
sheet.getRange(startRow, 1, rows.length, numCols).setValues(rows);
// Set the font size of the rows with column labels to 14
sheet.getRange(1, 1, 1, numCols).setFontSize(14);
sheet.autoResizeColumns(1, numCols + 1);
}
function searchEmailsSheet(ccVolunteerOld) {
const sheetName = "Emails";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const dataRange = sheet.getDataRange();
const values = dataRange.getValues();
const numRows = dataRange.getNumRows();
const numCols = dataRange.getNumColumns();
const returnObj = {};
for (let i = 1; i < numRows; i++) {
if (values[i][0] === "cc_volunteer_old") {
let foundColumn = -1;
for (let j = 1; j < numCols; j++) {
if (values[i][j] === ccVolunteerOld) {
foundColumn = j;
break;
}
}
if (foundColumn === -1) {
throw new Error("Column not found");
}
for (let k = 1; k < numRows; k++) {
returnObj[values[k][0]] = values[k][foundColumn];
}
break;
}
}
return returnObj;
}