-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.js
289 lines (265 loc) · 7.09 KB
/
evaluation.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Evaluation script.
// Script to provide tools for the evaluation spreadsheet.
// Provides a "Search" menu item.
//
// :project: gdit_gdocs
// :author: meisterluk
// :date: 11.10.14
// :version: 1.0.0public
// :license: LGPLv3
//
//----------------------------------------------------------------------
// GLOBAL CONFIGURATION
//----------------------------------------------------------------------
MATRNR_ROW = 2; // row with matr.numbers
NAME_ROW = 1; // row with twiki names
//----------------------------------------------------------------------
// FUNCTIONALITY
//----------------------------------------------------------------------
//
// Retrieve a mark by total points of a student.
// Note. Has *always* to return an integer.
//
// @param points int total points to calculate mark for
// @return int the corresponding mark
//
function pointsToMark(points)
{
if (points >= 88)
return 1;
else if (points >= 76)
return 2;
else if (points >= 63)
return 3;
else if (points >= 51)
return 4;
else
return 5;
}
//
// Levenshtein distance calculation
// via phpjs
// http://phpjs.org/functions/levenshtein:463
//
// @param s1 string first string
// @param s2 string second string to calculate similarity of
// @return int
//
function levenshtein(s1, s2)
{
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len;
}
if (s2_len === 0) {
return s1_len;
}
var split = false;
try {
split = !('0')[0];
} catch (e) {
split = true; // Earlier IE may not support access by string index
}
if (split) {
s1 = s1.split('');
s2 = s2.split('');
}
var v0 = new Array(s1_len + 1);
var v1 = new Array(s1_len + 1);
var s1_idx = 0,
s2_idx = 0,
cost = 0;
for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) {
v0[s1_idx] = s1_idx;
}
var char_s1 = '',
char_s2 = '';
for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) {
v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
char_s1 = s1[s1_idx];
cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx + 1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) {
m_min = b;
}
if (c < m_min) {
m_min = c;
}
v1[s1_idx + 1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp;
}
return v0[s1_len];
}
//
// String1 looks like string2?
//
// @param string1 string first string to compare with second
// @param string2 string second string
// @return bool shall I consider string1 to be similar to string2?
//
function stringSimilar(string1, string2)
{
var sensitivity = 3; // levensthein distance sensivitity
return levenshtein(string1, string2) < sensitivity;
}
//
// Number1 looks like number2?
// Note. levenshtein for numbers :-P
//
// @param number1 int first number to compare with second
// @param number2 int second number
// @return bool shall I consider number1 to be similar to number2?
//
function numberSimilar(number1, number2)
{
if (number1 === number2)
return true;
/*if(number1- (Math.pow(10, 4) * parseInt(number1/10000)) === number2)
return true;*/
if (number1 > 1000)
{
var num1 = String(number1);
var num2 = String(number2);
if (num1.length < num2.length)
{
var tmp = num1;
num1 = num2;
num2 = tmp;
}
for (var i=-num1.length+1; i<num1.length; i++)
{
if (i > 0)
{
if (num1.substr(0, i).length > 3 && num1.substr(0, i) === num2)
return true;
} else if (i < 0) {
if (num1.substr(i).length > 3 && num1.substr(i) === num2)
return true;
}
}
}
return false;
}
//
// Search for a student by Matriculation number
// Note. Calls an InputBox to request parameter
// Note. Points focus to search result or shows error message.
//
function searchMatrNr()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var identifier = Browser.inputBox
("Suche nach Matrnr im aktuellen Sheet: ");
var found = false;
if (identifier === "cancel")
return true;
identifier = parseInt(identifier.toString().replace(/[^0-9]/g, ""));
if (isNaN(identifier)) {
Browser.msgBox("Es wurde keine Zahl eingegeben");
return searchMatrNr();
} else {
if (sheet.getName() === "Benotung")
{
for (var row=5; row<=range.getLastRow(); row++)
{
var val = range.getCell(row, 2).getValue();
val = parseInt(val.toString().replace(/[^0-9]/g, ''));
if (!isNaN(val) && numberSimilar(val, identifier))
{
sheet.setActiveCell(range.getCell(row, 2));
found = true;
break;
}
}
} else {
for (var col=1; col<=range.getLastColumn(); col++)
{
var val = range.getCell(MATRNR_ROW, col).getValue();
val = parseInt(val.toString().replace(/[^0-9]/g, ''));
if (!isNaN(val) && numberSimilar(val, identifier))
{
sheet.setActiveCell(range.getCell
(sheet.getActiveCell().getRow(), col));
found = true;
break;
}
}
}
}
if (!found)
Browser.msgBox("Nicht gefunden! :-(");
}
//
// Search for a student by Matr.nr. or TwikiName
// Note. Calls an InputBox to request parameter
// Note. Points focus to search result or shows error message.
//
function searchTwikiName()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var identifier = Browser.inputBox
("Suche nach TwikiName im aktuellen Sheet: ");
var found = false;
if (identifier === "cancel")
return;
identifier = identifier.toString().replace(/\s+/, '').toLowerCase();
if (sheet.getName() === "Benotung")
{
for (var row=5; row<=range.getLastRow(); row++)
{
var val = range.getCell(row, 5).getValue();
val = val.toString().replace(/\s+/,'').toLowerCase();
if (stringSimilar(val, identifier))
{
sheet.setActiveCell(range.getCell(row, 5));
found = true;
break;
}
}
} else {
for (var col=1; col<=range.getLastColumn(); col++)
{
val = range.getCell(NAME_ROW, col).getValue();
val = val.toString().replace(/\s+/,'').toLowerCase();
if (stringSimilar(val, identifier))
{
sheet.setActiveCell(range.getCell
(sheet.getActiveCell().getRow(), col));
found = true;
break;
}
}
}
if (!found)
Browser.msgBox("Nicht gefunden! :-(");
}
//----------------------------------------------------------------------
// CALLBACK
//----------------------------------------------------------------------
//
// Trigger on document opening
//
function onOpen()
{
// Add a search menu.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{name: "Nach Matrikelnummer", functionName: "searchMatrNr"},
{name: "Nach Twikiname", functionName: "searchTwikiName"}
];
ss.addMenu("Suche", menuEntries);
}