forked from klausrheum/hopeful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.gs
207 lines (165 loc) · 5.61 KB
/
updater.gs
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
//var COLS = {
// "COMMENT":{"col":25}, // "text": "Comment", "width":
// "TABS": {"col": 26},
// "DATE": {"col":27},
// "EXPORTYN": {"col": 28}
//};
//Logger.log(COLS.COMMENT);
function updatePortfolios() {
// add days absent & late
}
function updateReportbooks() {
var rbIds = getRbIds();
var aaa_testerbook = "1CGQAR4QafGnC_LarUQqECY2Fy9Dv8jBkIsNlwUyuS3Y";
var phy09copy = "1dQra-gLWOZ0oLiUCsGXPGeGNnZQaqI2rEynAYbstdS8";
var rbIds = [aaa_testerbook, phy09copy];
for (var i in rbIds) {
//if (i>2) break;
id = rbIds[i];
var ss = SpreadsheetApp.openById(id);
console.info("Updating " + ss.getName());
//updateCommentsColumn(ss);
//updateExportColumns(ss);
//updateFreezeRows(ss);
updateRBFormulas(ss);
//updateGradeScale(ss);
//updateConditionalFormatting(ss); // doesn't work in this scope :(
// sheet(report)
// // display comment
// .insertFormula(I4,
// =iferror(index(Grades!$D$7:$Y$46, match($B$4,Grades!$D$7:$D$46,0),22),"")
// .chartType(scatter)
// .trendLines(false)
SpreadsheetApp.flush();
}
}
function updateCommentsColumn(ss) {
var sheet = ss.getSheetByName(template.gradesSheetName);
// ensure we have 25 columns 'Comment' column
var lastCol = sheet.getLastColumn();
while (lastCol < 25) {
sheet.insertColumnBefore(lastCol);
lastCol ++;
}
// if last column isn't 'Comment', make it so
var lastTitle = sheet.getRange(3, lastCol).getValue();
Logger.log(lastTitle);
if (lastTitle != "Comment") {
sheet.getRange("Y3:Y4").setValues([["Comment"],[""]]);
sheet.getRange("Y1:Y").setHorizontalAlignment("left");
sheet.setColumnWidth(25, 250);
}
}
// END updateCommentsColumn
function updateExportColumns(ss) {
// not working in this scope, using Y/N for now :/
// var checkBoxes =
// SpreadsheetApp
// .newDataValidation()
// .setAllowInvalid(false)
// .requireCheckbox()
// .build();
var sheet = ss.getSheetByName("Grades");
// add admin columns
var lastCol = sheet.getLastColumn();
while (lastCol < 28) {
sheet.insertColumnAfter(sheet.getLastColumn());
lastCol ++;
}
sheet.getRange("Y:AB")
.setBorder(null, true, null, true, true, null, '#999999', SpreadsheetApp.BorderStyle.SOLID);
sheet.getRange("Z1:AB5")
.setBackground("#e8eaf6")
.setFontColor("#303f9f");
// Tabs
sheet.setColumnWidth(27, 170);
sheet.getRange('AA3').setValue('Tabs');
sheet.getRange("Y1:Y5")
.setBackground("#333333")
.setFontColor("#FFFFFF");
// Date
sheet.setColumnWidth(26, 170);
sheet.getRange('Z3').setValue('Last exported:');
// Export
// var ss = SpreadsheetApp.openById(aaa);
// var sheet = ss.getSheetByName("Grades");
var checkboxValidation = SpreadsheetApp
.newDataValidation()
.requireCheckbox("Y", "N")
.build();
sheet.getRange("AB7:AB46").setDataValidation(checkboxValidation);
sheet.setColumnWidth(28, 50);
sheet.getRange('AB3').setValue('Export Y / N');
// Logger.log("Setting checkboxes");
// ss.getRange('AB7:AB')
// .setDataValidation(checkBoxes);
sheet.getRange("Y:AA")
.setHorizontalAlignment("left");
sheet.getRange("AB:AB")
.setHorizontalAlignment("center");
sheet.getRange("AA7:AA")
.setNumberFormat('hh":"mm":"ss" "ddd" "dd" "mmm" "yyyy');
};
// END updateExportColumns
function updateConditionalFormatting(ss) {
var conditionalFormatRules = ss.getActiveSheet().getConditionalFormatRules();
ss.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
conditionalFormatRules = ss.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, SpreadsheetApp.newConditionalFormatRule()
.setRanges([ss.getRange('Z7:Z46')])
.whenTextEqualTo('y')
.setBackground('#FF00FF')
.build());
ss.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
}
function updateFreezeRows(ss) {
ss.getSheetByName(template.gradesSheetName).setFrozenRows(6);
}
function updateRBFormulas(ss) {
var formulas = [
{
// if the Last name column is empty, don't display a grade (eg E-)
// F6=if(istext(A6), index(Grades, match($G6*100,GradeRange,-1), 1),"")
"sheet": "Grades",
"cell": "F6",
"range": "F7:F",
"formula": '=if(istext(A6), index(Grades, match($G6*100,GradeRange,-1), 1),"")'
},
{
// if the grade is blank, don't include it in the weighting denominator
// G6=sum(arrayformula(iferror(($H$1:$X$1 / sumif($H6:$X6, "<>", $H$1:$X$1)) * (H6:X6 / $H$4:$X$4))))
"sheet": "Grades",
"cell": "G6",
"range": "G7:G",
"formula": '=sum(arrayformula(iferror(($H$1:$X$1 / sumif($H6:$X6, "<>", $H$1:$X$1)) * (H6:X6 / $H$4:$X$4))))'
}
];
updateFormulas(ss, formulas);
}
function updateFormulas(ss, formulas) {
for (var i=0; i<formulas.length; i++) {
var update = formulas[i];
var sheet = ss.getSheetByName(update.sheet);
var oldFormula = sheet.getRange(update.cell).getFormula();
Logger.log(oldFormula);
// update to new formula
sheet.getRange(update.cell)
.setFormula(update.formula);
// fill down
sheet.getRange(update.cell)
.copyTo(sheet.getRange(update.range), SpreadsheetApp.CopyPasteType.PASTE_FORMULA);
}
}
function updateGradeScale(ss) {
var sheet = ss.getSheetByName(template.overviewSheetName);
var aplus = sheet.getRange("B8").getValue();
if (aplus != 80) {
Logger.log("Old scale: " + ss.getName() );
} else {
//Logger.log("Scale okay: " + ss.getName() );
}
}
function exportButton() {
// sheet = "Individual report";
// sheet.copyTo(name, B4:X11
}