-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats-script.html
174 lines (148 loc) · 4.91 KB
/
stats-script.html
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
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.13.3/math.min.js"></script>
<script type="text/javascript">
var columnDefs = null;
var template = null;
var statTemplate = null;
var summary = null;
var $ = function (sel) { return document.querySelector(sel); };
var enable = function (node) { node.removeAttribute('disabled'); }
var disable = function (node) { node.setAttribute('disabled', true); }
// polyfill for ChildNode.remove()
if (!('remove' in Element.prototype)) {
Element.prototype.remove = function () {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
};
}
function showStatus(msg, classId) {
var status = document.querySelectorAll('div[data-ui="status"]');
var icon = '';
if (msg !== '' && classId === 'error') {
icon = '<i class="fa fa-exclamation-circle" aria-hidden="true"></i>';
} else if (msg !== '') {
icon = '<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>';
} else {
icon = '';
}
for (var i = 0; i < status.length; i++) {
status[i].className = ""
status[i].innerHTML = icon + " " + msg;
if (classId) {
status[i].classList.add(classId);
}
}
}
function failureHandler(err) {
console.error(err);
showStatus('Something went wrong: ' + err, 'error');
}
// use async call to determine initial state of dropdown lists
// from contents of chart cells
function getVariables() {
showStatus('Getting variables...');
google.script.run
.withSuccessHandler(function (response) {
columnDefs = response;
// Handle if user is not on a sheet with no data
if (response.length === 0) {
showStatus('No variables to choose from.', 'error');
disable($('#calcBtn'));
} else {
showStatus('');
}
addVariableOptions();
})
.withFailureHandler(failureHandler)
.clientGetColumnVariables();
}
getVariables();
function addVariableOptions() {
var options = '<option value="" disabled selected>Select the variable</option>';
var selects = document.querySelectorAll('select');
var sortedVariableDefs = columnDefs.concat().sort(function (a, b) { return a - b; });
sortedVariableDefs.forEach(function (variable) {
var newOption = '<option value="' + variable + '">' + variable + '</option>';
options += newOption;
});
for (var i = 0; i < selects.length; i++) {
selects[i].innerHTML = options;
}
}
function checkSelectionState() {
var variable = $('#varName');
if (variable.value) {
enable($('#calcBtn'));
}
}
function calculateNew() {
var varName = $('#varName').value;
var resultsList = $('#resultList');
showStatus('Calculating...');
google.script.run
.withSuccessHandler(function (response) {
summary = {
name: varName,
min: math.round(math.min(response), 3),
max: math.round(math.max(response), 3),
mean: math.round(math.mean(response), 3),
median: math.round(math.median(response), 3),
stdev: math.round(math.std(response), 3)
}
resultsList.innerHTML = statTemplate(summary);
resultsList.classList.remove('hide');
resultsList.classList.add('show');
enable($('#addToSheetBtn'));
showStatus('');
})
.withFailureHandler(failureHandler)
.clientGetColumnValues(varName);
}
var compile = function (selector) {
var r = /(\{\{.+?\}\})/;
var elem = document.querySelector(selector);
var markup = elem.outerHTML;
elem.remove();
markup = markup.replace(/\s+/, ' ');
var fragments = markup.split(r);
var template = (function (tpl) {
return function (obj) {
var r = /(\{\{.+?\}\})/;
var accum = [];
for (var idx in fragments) {
var fragment = fragments[idx];
if (r.test(fragment)) {
fragment = fragment.replace(/(\{\{)|(\}\})/g, '');
accum.push(obj[fragment]);
} else {
accum.push(fragment);
}
}
return accum.join('');
};
})(fragments);
return template;
};
var statTemplate = compile('#result');
function clientAddToSheet() {
var dataTable = [
[summary.name + " Min", summary.min],
[summary.name + " Max", summary.max],
[summary.name + " Mean", summary.mean],
[summary.name + " Median", summary.median],
[summary.name + " Std. Dev.", summary.stdev]
];
showStatus('Sending...');
google.script.run
.withSuccessHandler(function () {
var resultsList = $('#resultList')
resultsList.classList.remove('show');
resultsList.classList.add('hide');
disable($('#addToSheetBtn'));
showStatus('');
getVariables();
})
.withFailureHandler(failureHandler)
.clientAddStats(dataTable);
}
</script>