forked from gurubenka/handsontable-excel
/
jquery.handsontable-excel.js
1261 lines (1104 loc) · 37 KB
/
jquery.handsontable-excel.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Extension to jquery.handsontable to implement formula evaluation
as well as dynamic types depending on the value you input.
To get the translation, add the following code to your 'cell' function:
cells: function (row, col, prop) {
var cellProperties = {};
cellProperties.type = 'excel';
return cellProperties;
},
*/
;(function ( $ ) {
var Token = function (t,c) {
var _d = { "type": null, t: t, c: c };
};
Token.parse = function () {
var c;
if (self._d.type != null)
return self._d; // Already parsed
if (typeof self._d.t === 'undefined' || self._d.t == null || self._d.t == '')
return self._d = { "type": '', token: null, next: null };
if (typeof self._d.t == 'string') {
c = self._d.t[0].toUpperCase();
if (c == "'")
return self._d = { "type": 'text', token: self._d.t.substring(1), next: self._d.c };
if (c == '=')
return self._d = { "type": 'formula', token: self._d.t.substring(1), next: self._d.c };
if (c == '(' || c == ')')
return self._d = { "type": 'paranthes', token: c, next: self._d.t.substring(1) };
}
else if (typeof self._d.t == 'number' && typeof self._d.c == 'number') {
return self._d = { "type": 'coord', row: self._d.t, col: self._d.c };
}
};
Token.get = function () {
if (self._d.type != null)
return self._d;
else
return self.parse();
}
Token.set = function(t,c) {
self._d = { "type": null, t: t, c: c };
}
Token.error = function () {
if (self._d.type == 'error')
return self._d;
if (self._d.type == null && typeof self._d.t == 'string')
return self._d = { "type": 'error', error: self._d.t };
return self._d = { "type": 'error', error: 'Bad error' };
}
Token.boolean = function () {
if (self._d.type == 'boolean')
return self._d;
if (self._d.type == null && typeof self._d.t == 'number')
return self._d = { "type": 'boolean', token: !!self._d.t, next: self._d.c };
if (self._d.type == null && typeof self._d.t == 'string' &&
self._d.t.length > 0 && self._d.t[0] >= '0' && self._d.t[0] <= '9')
return self._d = { "type": 'boolean', token: !!(self._d.t * 1.0), next: self._d.c };
return self._d = { "type": 'boolean', token: null, next: self._d.c };
}
Token.number = function () {
if (self._d.type == 'number')
return self._d;
if (self._d.type == null && typeof self._d.t == 'number')
return self._d = { "type": 'number', token: !!self._d.t, next: self._d.c };
if (self._d.type == null && typeof self._d.t == 'string' && self._d.t.length > 0 &&
(self._d.t[0] == '-' || (self._d.t[0] >= '0' && self._d.t[0] <= '9')))
return self._d = { "type": 'number', token: !!(self._d.t * 1.0), next: self._d.c };
return self._d = { "type": 'number', token: null, next: self._d.c };
}
//
// Take the formula as a string
// and make an array of tokens
// Take the array of tokens and evaluate it
// Return the result of that evaluation
var evaluateFormula = function (instance,formula) {
/*
var evalFormula = function (instance, formula) {
var instance = instance;
var orgformula;
var tokens = [];
}
*/
//
// Give us a the coordinated from a string reference.
// The coordinates are 0-based!!!
// A1 gives the row=0 (from the '1') and col=0 (from the 'A')
//
var getRC = function (cellRef) {
var i;
var row = 0;
var col = 0;
var radix = "Z".charCodeAt(0) - "A".charCodeAt(0) + 1;
var moff = "A".charCodeAt(0);
// var res = {};
var errres = { "type": 'error', error: 'Bad reference', next: null };
var state = 0;
for (i = 0; i < cellRef.length; i++) {
var c = cellRef[i];
if (c == '$')
continue;
if (state < 2 && c >= 'A' && c <= 'Z') {
state = 1;
col = col * radix + c.charCodeAt(0) - moff;
}
else if (state >= 1 && state <= 2 && c >= '0' && c <= '9') {
state = 2;
row = row * 10 + c.charCodeAt(0) - "0".charCodeAt(0);
}
else
return errres;
}
if (state < 2 || row < 1 || col < 0)
return errres;
return { "type": 'coord', row: row-1, col: col };
};
//
// Get the value from row and col variables
// Row and col is 0-based
//
var getData = function (row, col) {
var c;
var val;
var res = { "type": '', token: null, next: null };
try {
// val = container.handsontable('getData')[row][col];
val = instance.getData()[row][col];
}
catch(err) {
return { "type": 'error', error: 'Bad reference', next: null };
}
if (val == null || val == '')
return res;
c = val[0];
if (c == "'") {
res.type = 'text';
res.token = val.substring(1) + '';
return res;
}
if (c == "=") {
return evaluateFormula(instance,val.substring(1));
}
if (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9')) { // Numeric constant
res.type = 'number';
res.token = (val * 1.0);
return res;
}
// Else, we treat it as text anyway
res.type = 'text';
res.token = val + '';
return res;
};
//
// Get the value of a cell, referenced by a string as A1 or ABD3092
// Returns a token with the result, or an error if we cannot comply
//
var getCell = function (cellRef) {
var thiserr;
var coord = getRC(cellRef);
if (coord.type == 'error') {
thiserr = coord.error != null && coord.error != '' ? coord.error : 'Bad reference';
return { "type": 'error', error: thiserr, next: null };
}
return getData(coord.row, coord.col);
};
//
// Take the partial formula in the string
// and give us the next token from it
// swallowing the charcters of that token
// in the process
//
var nextToken = function (formulaString) { // split the textstring into tokens one at a time
var result = { "type": '', "token": null, "next": null};
var i;
var c;
var l;
if (formulaString == null || formulaString == '')
return result;
while (formulaString.length > 0 && formulaString[0] == ' ')
formulaString = formulaString.substring(1);
if (formulaString == null || formulaString == '')
return result;
l = formulaString.length;
c = formulaString[0].toUpperCase();
if (c == '-' || c == '+' || c == '*' || c == '/') {
result = { "type": "operator", token: c, next: formulaString.substring(1) };
}
else if (c == '(' || c == ')') {
result = { "type": "paranthes", token: c, next: formulaString.substring(1) };
}
else if (c == ':') {
result = { "type": "range", token: c, next: formulaString.substring(1) };
}
else if (c == ';') {
result = { "type": "param", token: c, next: formulaString.substring(1) };
}
else if ((c >= '0' && c <= '9') || c == '.') {
var val = 0.0;
var part = 0.0;
var state = 0;
var div = 1.0;
for (i = 0; i < formulaString.length && ((formulaString[i] >= '0' && formulaString[i] <= '9') || formulaString[i] == '.'); i++) {
c = formulaString[i];
if (state == 0 && c == '.')
state = 1;
else if (state == 0)
val = val * 10 + formulaString.charCodeAt(i) - "0".charCodeAt(0);
else if (state == 1 && c == '.')
return { "type": 'error', error: 'Bad munber', next: null };
else if (state == 1) {
div /= 10.0;
part = part + (formulaString.charCodeAt(i) - "0".charCodeAt(0)) * div;
}
}
result = { "type": "number", token: val + part, next: formulaString.substring(i) };
}
else if (c == '$' || (c >= 'A' && c <= 'Z')) { // See if it is a cell reference
var ref = '';
for (i = 0; i < formulaString.length && (formulaString[i] == '$' || (formulaString[i] >= '0' && formulaString[i] <= '9') || (formulaString[i].toUpperCase() >= 'A' && formulaString[i].toUpperCase() <= 'Z')); i++) {
c = formulaString[i].toUpperCase();
ref = ref + c;
}
if (i < formulaString.length && formulaString[i] == '(') {
ref = ref.toLowerCase(); // Reformat the function names to lower case!!!!!
result.type = 'func';
}
else
result.type = 'cell';
result.token = ref; // NOTE: Keep the upper case for cell references!!!!!
result.next = formulaString.substring(i);
}
else if (c == '=' || c == '<' || c == '>') { // See if it is a relation that produces a boolean
var c2 = null;
if (l == 1) {
result = { "type": "relation", token: c, next: formulaString.substring(1) };
}
else {
c2 = formulaString[1];
if (c2 == '=' || c2 == '<' || c2 == '>') {
result = { "type": "relation", token: c+c2, next: formulaString.substring(2) };
}
else
result = { "type": "relation", token: c, next: formulaString.substring(1) };
}
}
else if (c == '"') {
var str = ''
var i;
for (i = 1; i < formulaString.length && formulaString[i] != '"'; i++) {
c = formulaString[i];
if (c == '\\') {
i++;
c = formulaString[i];
}
str = str + c;
}
if (i == formulaString.length || formulaString[i] != '"')
return { "type": 'error', error: 'Unbalanced string', next: null };
result = { "type": "text", token: str, next: formulaString.substring(i+1) };
}
else { // Error or unknown token
result.type = 'error';
result.error = 'Bad token ' + c;
}
return result;
};
//
// When all parameters that uses the cell name are done in a function,
// We comes here and evaluates all the rest of the parameters,
// Giving all the values after each other in the array.
// This makes it possible to handle the array as a collection of values
// as all cells, functions and operators have been dealt with.
//
var fixParams = function (tokens) {
var i;
var s = 0;
var res = {};
for (i = 0; i < tokens.length; i++) {
if (tokens[i].type == 'param') {
if (s == i) { // A null parameter.
tokens[i].type = '';
tokens[i].token = null;
s++;
continue;
}
res = evaluateTokens(tokens.slice(s,i));
tokens.splice(s,i-s+1,res);
s++;
i = s - 1;
}
}
if (s < tokens.length) {
res = evaluateTokens(tokens.slice(s,i));
tokens.splice(s,i-s+1,res);
}
return tokens;
}
//
//
// The function block
// Here we have all the functions that can be used within the formulas.
// They need to be in lowercase as the formula give us the names that way.
//
//
// Sums all the parameters as well as all the ranges within
//
//
var funcSum = function (tokens) {
var i;
var sum = 0.0;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
sum = 0.0;
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean')
sum += (res.token * 1.0);
}
tokens.splice(i-1,3,{ "type": 'number', token: sum, next: null});
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
sum = 0.0;
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean')
sum += (tokens[i].token * 1.0);
return { "type": 'number', token: sum, next: null };
};
//
// Count all parameters that have a value as well as all ranges
//
var funcCount = function (tokens) {
var i;
var count = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (count == null)
count = 0;
count++;
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (count == null)
count = 0;
count++;
}
return { "type": count == null ? '' : 'number', token: count, next: null };
}
//
// Find the average of all parameters that have a value as well as all ranges
// If we have no values, this function gives 'NULL', ie blank box
//
var funcAvg = function (tokens) {
var i;
var count = null;
var sum = null;
var myavg = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (count == null) {
count = 0;
sum = 0.0;
}
count++;
sum += (res.token * 1.0);
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (count == null) {
count = 0;
sum = 0.0;
}
count++;
sum += (tokens[i].token * 1.0);
}
if (count > 0)
myavg = (sum * 1.0) / count;
return { "type": myavg == null ? '' : 'number', token: myavg, next: null };
}
//
// Find Minimum among all parameters that have a value as well as all ranges
//
var funcMin = function (tokens) {
var i;
var mymin = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (mymin == null)
mymin = res.token;
if (mymin > res.token)
mymin = res.token;
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (mymin == null)
mymin = tokens[i].token;
if (mymin > tokens[i].token)
mymin = tokens[i].token;
}
return { "type": mymin == null ? '' : 'number', token: mymin, next: null };
}
//
// Find Maximum among all parameters that have a value as well as all ranges
//
var funcMax = function (tokens) {
var i;
var mymax = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (mymax == null)
mymax = res.token;
if (mymax < res.token)
mymax = res.token;
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (mymax == null)
mymax = tokens[i].token;
if (mymax < tokens[i].token)
mymax = tokens[i].token;
}
return { "type": mymax == null ? '' : 'number', token: mymax, next: null };
}
//
// Logical AND among all parameters that have a value as well as all ranges
//
var funcAnd = function (tokens) {
var i;
var mycond = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (mycond == null)
mycond = !!res.token;
mycond = (mycond && !!res.token);
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (mycond == null)
mycond = !!tokens[i].token;
mycond = (mycond && !!tokens[i].token);
}
return { "type": mycond == null ? '' : 'boolean', token: mycond, next: null };
}
//
// Logical OR among all parameters that have a value as well as all ranges
//
var funcOr = function (tokens) {
var i;
var mycond = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (mycond == null)
mycond = !!res.token;
mycond = (mycond || !!res.token);
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (mycond == null)
mycond = !!tokens[i].token;
mycond = (mycond || !!tokens[i].token);
}
return { "type": mycond == null ? '' : 'boolean', token: mycond, next: null };
}
//
// Logical XOR among all parameters that have a value as well as all ranges
//
var funcXor = function (tokens) {
var i;
var mycond = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
if (mycond == null)
mycond = 0;
mycond = !!(mycond ^ !!res.token);
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
if (mycond == null)
mycond = 0;
mycond = !!(mycond ^ !!tokens[i].token);
}
return { "type": mycond == null ? '' : 'boolean', token: mycond, next: null };
}
//
// Logical NOT among all parameters that have a value as well as all ranges
//
var funcNot = function (tokens) {
var i;
var mycond = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == 'number' || res.type == 'boolean') {
mycond = !res.token;
}
}
tokens.splice(i-1,3); // Remove range itself as we have just counted all within
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'number' || tokens[i].type == 'boolean') {
mycond = !tokens[i].token;
}
return { "type": mycond == null ? '' : 'boolean', token: mycond, next: null };
}
//
// IF have two or three parameters. First must evaluate to a boolean,
// The second is evaluated if the first is true.
// If the third exists, it is evaluated if the first is false.
//
var funcIf = function (tokens) {
var res = {};
tokens = fixParams(tokens); // evaluate all parameters
if (tokens.length < 2)
return { "type": 'error', error: 'To few parameters for IF', next: null };
res = tokens[0];
if (!(res.type == 'boolean' || res.type == 'number')) {
return { "type": 'error', error: 'First parameter for IF must be boolean', next: null };
}
if (!!res.token)
return tokens[1];
else
if (tokens.length < 3)
return { "type": '', token: null, next: null };
else
return tokens[2];
}
//
// Concatenate all parameters that have a value as well as all ranges
// The result is a text or null.
//
var funcConcat = function (tokens) {
var i;
var tempstr = null;
var res = {};
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'range') {
var topleft, bottomright;
var r, c;
var tempstr = null;
if (i == 0 || i == tokens.length - 1) { // Range error
return { "type": 'error', error: 'Bad range', next: null };
}
topleft = getRC(tokens[i-1].token);
bottomright = getRC(tokens[i+1].token);
for (r = topleft.row; r <= bottomright.row; r++)
for (c = topleft.col; c <= bottomright.col; c++) {
res = getData(r,c);
if (res.type == 'error')
return res;
if (res.type == '' || res.token == null)
continue;
if (tempstr == null)
tempstr = '';
tempstr = tempstr + res.token + '';
}
res = { "type": tempstr == null ? '' : 'text', token: tempstr, next: null };
tokens.splice(i-1,3,res); // Remove range itself and place the concated string there
i = i - 2;
continue;
}
tokens = fixParams(tokens); // evaluate all parameters
for (i = 0; i < tokens.length; i++) {
if (tokens[i].type == '' || tokens[i].token == null)
continue;
if (tempstr == null)
tempstr = '';
tempstr = tempstr + tokens[i].token + '';
}
return { "type": tempstr == null ? '' : 'text', token: tempstr, next: null };
}
//
// NOTE: All function names are in lower case!!!!!
//
var funcs = [
{ func: 'sum', cb: funcSum }
,{ func: 'count', cb: funcCount }
,{ func: 'if', cb: funcIf }
,{ func: 'avg', cb: funcAvg }
,{ func: 'min', cb: funcMin }
,{ func: 'max', cb: funcMax }
,{ func: 'and', cb: funcAnd }
,{ func: 'or', cb: funcOr }
,{ func: 'xor', cb: funcXor }
,{ func: 'not', cb: funcNot }
,{ func: 'concat', cb: funcConcat }
];
var evaluateFunc = function (tokens) {
var i;
for (i = 0; i < funcs.length; i++)
if (funcs[i].func == tokens[0].token)
return funcs[i].cb(tokens.slice(2, tokens.length));
return { "type": 'error', error: 'No such function ' + tokens[0].token, next: null };
};
//
// Some support functions to evalTokens:
// fixBoolean: makes boolean value forcefully either numeric 0 or 1.
// A null value is treated as a 0
//
// fixText: if the first token is a text and the other a number,
// see if the text has digits as first chars. If so,
// make the text a number, else make the number a text
//
var fixBoolean = function(tokens, col) {
if (tokens[col].type == '' || tokens[col].type == 'boolean') {
var term = tokens[col].token;
tokens[col].type = 'number';
if (typeof term === 'undefined' || term == null || term == '')
tokens[col].token = 0.0;
}
};
var fixOneCol = function (tokens, col) {
var term = tokens[col].token;
if (typeof term === 'undefined' || term == null || term == '') {
tokens[col].type = 'number';
tokens[col].token = 0.0;
}
else {
var c = tokens[col].token[0];
if (c == '-' || c == '+' || (c >= '0' && c <= '9')) {
tokens[col].type = 'number';
tokens[col].token = (1.0 * tokens[col].token);
}
}
};
var fixText = function (tokens, col1, col2) {
if (tokens[col1].type == 'text' && tokens[col2].type == 'number')
fixOneCol(tokens,col1);
else if (tokens[col2].type == 'text' && tokens[col1].type == 'number')
fixOneCol(tokens,col2);
};
//
// Make a recursive loop through the array of tokens
// to evaluate it by collapse the array when we find something to evaluate.
// The result is at the first index of the collapsed array
//
// () and functions have the highest priority.
// that mean that the parameters to the function isn't neseccary evaluated fully
// we need to have it that way so that we can evaluate 'range' and functions
// that use the cell references not the values they point to. For instance
// COLUMN(A3) should give '3' as result and ROW(A3) should give '1'.
// but SUM(A1:B2) should iterate all indicated cells to sum the values of them.
// The above gives A1, A2, B1, B2 as cells to sum the values from.
// If any of them contails a formula, it is reevaluated on the spot, returning
// the result of that formula before the 'SUM' continues.
//
// A2 When all paranthesis and functions have been evaluated,
// the rest of the cell references are evaluated and exchanged for their values.
// If any value is a formula, it is reevaluated here to give the current value
// of that formula.
//
// Note that we can have a deadlock!!!!!! formulas are not marked as evaluated
// this sequence, so be careful.
//
// * / comes next in priority
// - Unary minus is evaluated next. Also unary '+' goes here.
// + - Have the lowest priority
//
var evaluateTokens = function (tokens) {
var i, j;
var found = 0;
for (i = tokens.length - 1; i >= 0; i--)
if (tokens[i].type == 'paranthes' && tokens[i].token == '(') {
for (j = i; j < tokens.length; j++)
if (tokens[j].type == 'paranthes' && tokens[j].token == ')') {
var res;
if (i > 0 && tokens[i-1].type == 'func') {
i--;
res = evaluateFunc(tokens.slice(i,j)); // Evaluate function
}
else
res = evaluateTokens(tokens.slice(i+1,j)); // Evaluate formula within ()
tokens.splice(i,j-i+1,res);
found = 1;
break;
}
if (!found) {
return { "type": 'error', error: 'Unbalanced paranthesis', next: null };
}
i = tokens.length; // Restart search for paranthesis
found = 0;
continue;
}
for (i = 0; i < tokens.length; i++)
if (tokens[i].type == 'cell') {
var res = getCell(tokens[i].token); // Replace reference with the value
tokens.splice(i,1,res);
}
for (i = 1; i < tokens.length - 1; i++) // Evaluate * /
if (tokens[i].type == 'operator' && (tokens[i].token == '*' || tokens[i].token == '/')) {
var res = { "type": 'number', "token": 0.0, "next": null };
fixBoolean(tokens, i-1);
fixBoolean(tokens, i+1);
fixText(tokens, i-1, i+1);
if (tokens[i-1].type != 'number' || tokens[i+1].type != 'number')
return { "type": 'error', "error": 'Bad formula', "next": null };
if (tokens[i].token == '*')
res.token = tokens[i-1].token * tokens[i+1].token;
else
res.token = tokens[i-1].token / tokens[i+1].token;
tokens.splice(i-1,3,res);
i = i - 2;
continue;
}
// Unary + and -
if (tokens[0].type == 'operator' && (tokens[0].token == '-' || tokens[0].token == '+')) {
var res = { "type": 'number', "token": 0.0, "next": null };
if (tokens.length > 1) {
fixBoolean(tokens, 1);