-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.gs
More file actions
78 lines (61 loc) · 2.17 KB
/
Copy pathscript.gs
File metadata and controls
78 lines (61 loc) · 2.17 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
/*
-- This script necessitates the following spreadsheet schema:
Sheet for auto-generated summary of risks called "Summary"
Sheet with checklist of hazards called "Generic Hazard Checklist"
- First col contains checkboxes for each hazard
- Second col contains ID for the hazard
Sheets with ID numbers in the first col matching those ID numbers in the checklist
All must be done within very secific row and column ranges which need to be
de-cyphered from this code.
*/
function updateSummarySheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var allSheets = spreadsheet.getSheets();
var summarySheet = spreadsheet.getSheetByName("Summary");
var checklistSheet = spreadsheet.getSheetByName("Generic Hazard Checklist");
var lastRow = checklistSheet.getDataRange().getLastRow();
var checklistData = checklistSheet.getSheetValues(11, 1, lastRow - 10, 2);
var sheetNumberRegex = /^[0-9]+$/;
var sheetNumber = 1;
var hazardData = {};
var hazardSheet = allSheets[sheetNumber];
var summary = [];
for (var row = 0; row < checklistData.length; row++) {
var isTicked = checklistData[row][0];
var id = checklistData[row][1].toString();
if (!id) {
continue;
}
if (id.match(sheetNumberRegex)) {
// Move onto the next sheet
hazardSheet = allSheets[++sheetNumber];
hazardData = parseHazardSheet(hazardSheet);
continue;
}
if (isTicked) {
summary = summary.concat(hazardData[id]);
}
}
var currentLength = Math.max(1, summarySheet.getDataRange().getLastRow() - 5);
summarySheet.getRange(6, 1, currentLength, 19).clearContent();
summarySheet.getRange(6, 1, summary.length, 19).setValues(summary);
}
function parseHazardSheet(sheet) {
// Hack for now...
var lastRow = sheet.getDataRange().getLastRow();
var data = sheet.getRange(20, 1, lastRow - 19, 19).getValues();
var info = {};
var id = "";
var endRow = data.length;
while (!data[endRow-1][6] && endRow > 1){
endRow--;
}
for (var row = endRow-1; row >= 0; row--) {
id = data[row][0].toString();
if (id) {
info[id] = data.slice(row, endRow);
endRow = row;
}
}
return info;
}