-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonFunctions.js
247 lines (215 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
const scriptProperties = PropertiesService.getScriptProperties();
const server = scriptProperties.getProperty('cred_server');
const port = 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 cell = setupCell("Dashboard", "B5");
const cc_location = "Parthian Climbing Manchester";
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) {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName(name);
sheet.clearContents();
sheet.clearFormats();
return sheet;
}
function setupCell(name, range) {
var spreadsheet = SpreadsheetApp.getActive();
let sheet = spreadsheet.getSheetByName(name);
let cellValue = sheet.getRange(range).getValue();
return cellValue;
}
function appendToSheet(sheet, results) {
let metaData = results.getMetaData();
let 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) {
let range = getColumnRange(sheet, columnHeader);
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains(search)
.setBackground(colour)
.setRanges([range])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setTextFormat(sheet, columnHeader, search, colour) {
let range = getColumnRange(sheet, columnHeader);
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains(search)
.setFontColor(colour)
.setRanges([range])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setWrapped(sheet, columnHeader) {
var range = getColumnRange(sheet, columnHeader);
range.setWrap(true);
sheet.setColumnWidth(range.getColumn(), 300); // Set column width to 300 pixels
}
function setColoursFormatLessThanOrEqualTo(sheet, columnHeader, search, colour) {
search = Number(search);
let range = getColumnRange(sheet, columnHeader);
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberLessThanOrEqualTo(search)
.setBackground(colour)
.setRanges([range])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
function setNumberFormat(sheet, columnHeader, format) {
let range = getColumnRange(sheet, columnHeader);
range.setNumberFormat(format);
}
function makeReport(stmt, reportConfig) {
let sheet = setupSheet(reportConfig.sheetName);
if (isNaN(cell) || cell === "") {
throw new Error("Invalid event selected - please try again");
}
var results = stmt.executeQuery(reportConfig.query.replace(/\${cell}/g, cell));
appendToSheet(sheet, results);
if (reportConfig.formatting) {
reportConfig.formatting.forEach(format => {
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) {
var range = getColumnRange(sheet, columnHeader);
sheet.setColumnWidth(range.getColumn(), width);
}
function refreshOutput() {
var conn = Jdbc.getConnection(url, username, password);
var stmt = conn.createStatement();
readOutput(stmt);
stmt.close();
conn.close();
}
function getIP() {
var url = "http://api.ipify.org";
var json = UrlFetchApp.fetch(url);
Logger.log(json);
}
function appendToSheetWithNoClear(sheet, results) {
let metaData = results.getMetaData();
let 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) {
var sheetName = 'Emails';
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
var numRows = dataRange.getNumRows();
var numCols = dataRange.getNumColumns();
var returnObj = {};
for (var i = 1; i < numRows; i++) {
if (values[i][0] == 'cc_volunteer_old') {
var foundColumn = -1;
for (var j = 1; j < numCols; j++) {
if (values[i][j] == ccVolunteerOld) {
foundColumn = j;
break;
}
}
if (foundColumn == -1) {
throw new Error('Column not found');
}
for (var k = 1; k < numRows; k++) {
returnObj[values[k][0]] = values[k][foundColumn];
}
break;
}
}
return returnObj;
}