-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1451 lines (1326 loc) · 43.9 KB
/
index.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
/**
* @class
*/
function F_token(value, type, subtype) {
this.value = value;
this.type = type;
this.subtype = subtype;
}
/**
* @class
*/
function F_tokens() {
this.TOK_SUBTYPE_STOP = "stop";
this.items = [];
this.add = function (value, type, subtype) {
if (!subtype) {
subtype = "";
}
let token = new F_token(value, type, subtype);
this.addRef(token);
return token;
};
this.addRef = function (token) {
this.items.push(token);
};
this.index = -1;
this.reset = function () {
this.index = -1;
};
this.BOF = function () {
return this.index <= 0;
};
this.EOF = function () {
return this.index >= this.items.length - 1;
};
this.moveNext = function () {
if (this.EOF()) {
return false;
}
this.index += 1;
return true;
};
this.current = function () {
if (this.index === -1) {
return null;
}
return this.items[this.index];
};
this.next = function () {
if (this.EOF()) {
return null;
}
return this.items[this.index + 1];
};
this.previous = function () {
if (this.index < 1) {
return null;
}
return this.items[this.index - 1];
};
}
function F_tokenStack() {
this.TOK_SUBTYPE_STOP = "stop";
this.items = [];
this.push = function (token) {
this.items.push(token);
};
this.pop = function (name) {
let token = this.items.pop();
return new F_token(name || "", token.type, this.TOK_SUBTYPE_STOP);
};
this.token = function () {
return this.items.length > 0 ? this.items[this.items.length - 1] : null;
};
this.value = function () {
return this.token() ? this.token().value.toString() : "";
};
this.type = function () {
return this.token() ? this.token().type.toString() : "";
};
this.subtype = function () {
return this.token() ? this.token().subtype.toString() : "";
};
}
class ExcelFormula {
static excelFormulaUtilities = {
isEu: false,
};
static types = {};
static TOK_TYPE_NOOP = (this.types.TOK_TYPE_NOOP = "noop");
static TOK_TYPE_OPERAND = (this.types.TOK_TYPE_OPERAND = "operand");
static TOK_TYPE_FUNCTION = (this.types.TOK_TYPE_FUNCTION = "function");
static TOK_TYPE_SUBEXPR = (this.types.TOK_TYPE_SUBEXPR = "subexpression");
static TOK_TYPE_ARGUMENT = (this.types.TOK_TYPE_ARGUMENT = "argument");
static TOK_TYPE_OP_PRE = (this.types.TOK_TYPE_OP_PRE = "operator-prefix");
static TOK_TYPE_OP_IN = (this.types.TOK_TYPE_OP_IN = "operator-infix");
static TOK_TYPE_OP_POST = (this.types.TOK_TYPE_OP_POST = "operator-postfix");
static TOK_TYPE_WHITE_SPACE = (this.types.TOK_TYPE_WHITE_SPACE =
"white-space");
static TOK_TYPE_UNKNOWN = (this.types.TOK_TYPE_UNKNOWN = "unknown");
static TOK_SUBTYPE_START = (this.types.TOK_SUBTYPE_START = "start");
static TOK_SUBTYPE_STOP = (this.types.TOK_SUBTYPE_STOP = "stop");
static TOK_SUBTYPE_TEXT = (this.types.TOK_SUBTYPE_TEXT = "text");
static TOK_SUBTYPE_NUMBER = (this.types.TOK_SUBTYPE_NUMBER = "number");
static TOK_SUBTYPE_LOGICAL = (this.types.TOK_SUBTYPE_LOGICAL = "logical");
static TOK_SUBTYPE_ERROR = (this.types.TOK_SUBTYPE_ERROR = "error");
static TOK_SUBTYPE_RANGE = (this.types.TOK_SUBTYPE_RANGE = "range");
static TOK_SUBTYPE_MATH = (this.types.TOK_SUBTYPE_MATH = "math");
static TOK_SUBTYPE_CONCAT = (this.types.TOK_SUBTYPE_CONCAT = "concatenate");
static TOK_SUBTYPE_INTERSECT = (this.types.TOK_SUBTYPE_INTERSECT =
"intersect");
static TOK_SUBTYPE_UNION = (this.types.TOK_SUBTYPE_UNION = "union");
/**
* Simple/quick string formater. This will take an input string and apply n number of arguments to it.
*
* <b>example:</b><br />
* <code>
* <pre>
* var foo = excelFormulaUtilities.core.formatStr("{0}", "foo"); // foo will be set to "foo"
* var fooBar = excelFormulaUtilities.core.formatStr("{0} {1}", "foo", "bar"); // fooBar will be set to "fooBar"
* var error = excelFormulaUtilities.core.formatStr("{1}", "error"); // will throw an index out of range error since only 1 extra argument was passed, which would be index 0.
* </pre>
* </code>
*
* @memberOf global.excelFormulaUtilities.core
* @function
* @param {String} inStr
**/
static formatStr(inStr) {
var formattedStr = inStr;
var argIndex = 1;
for (; argIndex < arguments.length; argIndex++) {
var replaceIndex = argIndex - 1;
var replaceRegex = new RegExp(
"\\{{1}" + replaceIndex.toString() + "{1}\\}{1}",
"g"
);
formattedStr = formattedStr.replace(replaceRegex, arguments[argIndex]);
}
return formattedStr;
}
static trim(inStr) {
return inStr.replace(/^\s|\s$/, "");
}
//Quick and dirty type checks
/**
* @param {object} obj
* @returns {boolean}
* @memberOf global.excelFormulaUtilities.core
*/
static isFunction(obj) {
return typeof obj === "function";
}
/**
* @param {object} obj
* @returns {boolean}
* @memberOf global.excelFormulaUtilities.core
*/
static isArray(obj) {
return typeof obj === "object" && obj.length;
}
/**
* @param {object} obj
* @returns {boolean}
* @memberOf global.excelFormulaUtilities.core
*/
static isWindow() {
return obj && typeof obj === "object" && "setInterval" in obj;
} /*----The functionality below has based off of the jQuery core library----*/
/**
* Check if the object is a plain object or not. This has been pulled from the jQuery core and modified slightly.
* @param {object} obj
* @returns {boolean} returns weather the object is a plain object or not.
* @memberOf global.excelFormulaUtilities.core
*/
static isPlainObject(obj) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || typeof obj !== "object" || obj.nodeType || isWindow(obj)) {
return false;
}
// Not own constructor property must be Object
if (
obj.constructor &&
!hasOwnProperty.call(obj, "constructor") &&
!hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")
) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var lastKey;
for (key in obj) {
lastKey = key;
}
return lastKey === undefined || hasOwnProperty.call(obj, lastKey);
}
/**
* This has been pulled from the jQuery core and modified slightly. see http://api.jquery.com/jQuery.extend/
* @param {object} target
* @param {object} object add one or more object to extend the target.
* @returns {object} returns the extended object.
* @memberOf global.excelFormulaUtilities.core
*/
static extend() {
var options,
name,
src,
copy,
copyIsArray,
clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !isFunction(target)) {
target = {};
}
// extend jQuery itself if only one argument is passed
if (length === i) {
target = this;
--i;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (
deep &&
copy &&
(this.isPlainObject(copy) || (copyIsArray = this.isArray(copy)))
) {
if (copyIsArray) {
copyIsArray = false;
clone = src && this.isArray(src) ? src : [];
} else {
clone = src && this.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = core.extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
}
static getTokens(formula) {
let tokens = new F_tokens(),
tokenStack = new F_tokenStack(),
offset = 0,
currentChar = function () {
return formula.substr(offset, 1);
},
doubleChar = function () {
return formula.substr(offset, 2);
},
nextChar = function () {
return formula.substr(offset + 1, 1);
},
EOF = function () {
return offset >= formula.length;
},
token = "",
inString = false,
inPath = false,
inRange = false,
inError = false,
regexSN = /^[1-9]{1}(\.[0-9]+)?E{1}$/;
while (formula.length > 0) {
if (formula.substr(0, 1) === " ") {
formula = formula.substr(1);
} else {
if (formula.substr(0, 1) === "=") {
formula = formula.substr(1);
}
break;
}
}
while (!EOF()) {
// state-dependent character evaluation (order is important)
// double-quoted strings
// embeds are doubled
// end marks token
if (inString) {
if (currentChar() === '"') {
if (nextChar() === '"') {
token += '"';
offset += 1;
} else {
inString = false;
tokens.add(token, this.TOK_TYPE_OPERAND, this.TOK_SUBTYPE_TEXT);
token = "";
}
} else {
token += currentChar();
}
offset += 1;
continue;
}
// single-quoted strings (links)
// embeds are double
// end does not mark a token
if (inPath) {
if (currentChar() === "'") {
if (nextChar() === "'") {
token += "'";
offset += 1;
} else {
inPath = false;
token += "'";
}
} else {
token += currentChar();
}
offset += 1;
continue;
}
// bracketed strings (range offset or linked workbook name)
// no embeds (changed to "()" by Excel)
// end does not mark a token
if (inRange) {
if (currentChar() === "]") {
inRange = false;
}
token += currentChar();
offset += 1;
continue;
}
// error values
// end marks a token, determined from absolute list of values
if (inError) {
token += currentChar();
offset += 1;
if (
",#NULL!,#DIV/0!,#VALUE!,#REF!,#NAME?,#NUM!,#N/A,".indexOf(
"," + token + ","
) !== -1
) {
inError = false;
tokens.add(token, this.TOK_TYPE_OPERAND, this.TOK_SUBTYPE_ERROR);
token = "";
}
continue;
}
// scientific notation check
if ("+-".indexOf(currentChar()) !== -1) {
if (token.length > 1) {
if (token.match(regexSN)) {
token += currentChar();
offset += 1;
continue;
}
}
}
// independent character evaluation (order not important)
// establish state-dependent character evaluations
if (currentChar() === '"') {
if (token.length > 0) {
// not expected
tokens.add(token, this.TOK_TYPE_UNKNOWN);
token = "";
}
inString = true;
offset += 1;
continue;
}
if (currentChar() === "'") {
if (token.length > 0) {
// not expected
tokens.add(token, this.TOK_TYPE_UNKNOWN);
token = "";
}
token = "'";
inPath = true;
offset += 1;
continue;
}
if (currentChar() === "[") {
inRange = true;
token += currentChar();
offset += 1;
continue;
}
if (currentChar() === "#") {
if (token.length > 0) {
// not expected
tokens.add(token, this.TOK_TYPE_UNKNOWN);
token = "";
}
inError = true;
token += currentChar();
offset += 1;
continue;
}
// mark start and end of arrays and array rows
if (currentChar() === "{") {
if (token.length > 0) {
// not expected
tokens.add(token, this.TOK_TYPE_UNKNOWN);
token = "";
}
tokenStack.push(
tokens.add("ARRAY", this.TOK_TYPE_FUNCTION, this.TOK_SUBTYPE_START)
);
tokenStack.push(
tokens.add("ARRAYROW", this.TOK_TYPE_FUNCTION, this.TOK_SUBTYPE_START)
);
offset += 1;
continue;
}
if (currentChar() === ";") {
if (excelFormulaUtilities.isEu) {
// If is EU then handle ; as list separators
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
if (tokenStack.type() !== this.TOK_TYPE_FUNCTION) {
tokens.add(
currentChar(),
this.TOK_TYPE_OP_IN,
this.TOK_SUBTYPE_UNION
);
} else {
tokens.add(currentChar(), this.TOK_TYPE_ARGUMENT);
}
offset += 1;
continue;
} else {
// Else if not Eu handle ; as array row separator
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.addRef(tokenStack.pop());
tokens.add(",", this.TOK_TYPE_ARGUMENT);
tokenStack.push(
tokens.add(
"ARRAYROW",
this.TOK_TYPE_FUNCTION,
this.TOK_SUBTYPE_START
)
);
offset += 1;
continue;
}
}
if (currentChar() === "}") {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.addRef(tokenStack.pop("ARRAYROWSTOP"));
tokens.addRef(tokenStack.pop("ARRAYSTOP"));
offset += 1;
continue;
}
// trim white-space
if (currentChar() === " ") {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.add("", this.TOK_TYPE_WHITE_SPACE);
offset += 1;
while (currentChar() === " " && !EOF()) {
offset += 1;
}
continue;
}
// multi-character comparators
if (",>=,<=,<>,".indexOf("," + doubleChar() + ",") !== -1) {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.add(doubleChar(), this.TOK_TYPE_OP_IN, this.TOK_SUBTYPE_LOGICAL);
offset += 2;
continue;
}
// standard infix operators
if ("+-*/^&=><".indexOf(currentChar()) !== -1) {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.add(currentChar(), this.TOK_TYPE_OP_IN);
offset += 1;
continue;
}
// standard postfix operators
if ("%".indexOf(currentChar()) !== -1) {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.add(currentChar(), this.TOK_TYPE_OP_POST);
offset += 1;
continue;
}
// start subexpression or function
if (currentChar() === "(") {
if (token.length > 0) {
tokenStack.push(
tokens.add(token, this.TOK_TYPE_FUNCTION, this.TOK_SUBTYPE_START)
);
token = "";
} else {
tokenStack.push(
tokens.add("", this.TOK_TYPE_SUBEXPR, this.TOK_SUBTYPE_START)
);
}
offset += 1;
continue;
}
// function, subexpression, array parameters
// if (currentChar() === "," && !this.excelFormulaUtilities.isEu) {
if (currentChar() === ",") {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
if (tokenStack.type() !== this.TOK_TYPE_FUNCTION) {
tokens.add(
currentChar(),
this.TOK_TYPE_OP_IN,
this.TOK_SUBTYPE_UNION
);
} else {
tokens.add(currentChar(), this.TOK_TYPE_ARGUMENT);
}
offset += 1;
continue;
}
// stop subexpression
if (currentChar() === ")") {
if (token.length > 0) {
tokens.add(token, this.TOK_TYPE_OPERAND);
token = "";
}
tokens.addRef(tokenStack.pop());
offset += 1;
continue;
}
// token accumulation
token += currentChar();
offset += 1;
}
// dump remaining accumulation
if (token.length > 0 || inString || inPath || inRange || inError) {
if (inString || inPath || inRange || inError) {
if (inString) {
token = '"' + token;
} else if (inPath) {
token = "'" + token;
} else if (inRange) {
token = "[" + token;
} else if (inError) {
token = "#" + token;
}
tokens.add(token, this.TOK_TYPE_UNKNOWN);
} else {
tokens.add(token, this.TOK_TYPE_OPERAND);
}
}
// move all tokens to a new collection, excluding all unnecessary white-space tokens
let tokens2 = new F_tokens();
while (tokens.moveNext()) {
token = tokens.current();
if (token.type.toString() === this.TOK_TYPE_WHITE_SPACE) {
let doAddToken = tokens.BOF() || tokens.EOF();
//if ((tokens.BOF()) || (tokens.EOF())) {}
doAddToken =
doAddToken &&
((tokens.previous().type.toString() === this.TOK_TYPE_FUNCTION &&
tokens.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
(tokens.previous().type.toString() === this.TOK_TYPE_SUBEXPR &&
tokens.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
tokens.previous().type.toString() === this.TOK_TYPE_OPERAND);
//else if (!(
// ((tokens.previous().type === this.TOK_TYPE_FUNCTION) && (tokens.previous().subtype == this.TOK_SUBTYPE_STOP))
// || ((tokens.previous().type == this.TOK_TYPE_SUBEXPR) && (tokens.previous().subtype == this.TOK_SUBTYPE_STOP))
// || (tokens.previous().type == this.TOK_TYPE_OPERAND)))
// {}
doAddToken =
doAddToken &&
((tokens.next().type.toString() === this.TOK_TYPE_FUNCTION &&
tokens.next().subtype.toString() === this.TOK_SUBTYPE_START) ||
(tokens.next().type.toString() === this.TOK_TYPE_SUBEXPR &&
tokens.next().subtype.toString() === this.TOK_SUBTYPE_START) ||
tokens.next().type.toString() === this.TOK_TYPE_OPERAND);
//else if (!(
// ((tokens.next().type == this.TOK_TYPE_FUNCTION) && (tokens.next().subtype == this.TOK_SUBTYPE_START))
// || ((tokens.next().type == this.TOK_TYPE_SUBEXPR) && (tokens.next().subtype == this.TOK_SUBTYPE_START))
// || (tokens.next().type == this.TOK_TYPE_OPERAND)))
// {}
//else { tokens2.add(token.value, this.TOK_TYPE_OP_IN, this.TOK_SUBTYPE_INTERSECT)};
if (doAddToken) {
tokens2.add(
token.value.toString(),
this.TOK_TYPE_OP_IN,
this.TOK_SUBTYPE_INTERSECT
);
}
continue;
}
tokens2.addRef(token);
}
// switch infix "-" operator to prefix when appropriate, switch infix "+" operator to noop when appropriate, identify operand
// and infix-operator subtypes, pull "@" from in front of function names
while (tokens2.moveNext()) {
token = tokens2.current();
if (
token.type.toString() === this.TOK_TYPE_OP_IN &&
token.value.toString() === "-"
) {
if (tokens2.BOF()) {
token.type = this.TOK_TYPE_OP_PRE.toString();
} else if (
(tokens2.previous().type.toString() === this.TOK_TYPE_FUNCTION &&
tokens2.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
(tokens2.previous().type.toString() === this.TOK_TYPE_SUBEXPR &&
tokens2.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
tokens2.previous().type.toString() === this.TOK_TYPE_OP_POST ||
tokens2.previous().type.toString() === this.TOK_TYPE_OPERAND
) {
token.subtype = this.TOK_SUBTYPE_MATH.toString();
} else {
token.type = this.TOK_TYPE_OP_PRE.toString();
}
continue;
}
if (
token.type.toString() === this.TOK_TYPE_OP_IN &&
token.value.toString() === "+"
) {
if (tokens2.BOF()) {
token.type = this.TOK_TYPE_NOOP.toString();
} else if (
(tokens2.previous().type.toString() === this.TOK_TYPE_FUNCTION &&
tokens2.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
(tokens2.previous().type.toString() === this.TOK_TYPE_SUBEXPR &&
tokens2.previous().subtype.toString() === this.TOK_SUBTYPE_STOP) ||
tokens2.previous().type.toString() === this.TOK_TYPE_OP_POST ||
tokens2.previous().type.toString() === this.TOK_TYPE_OPERAND
) {
token.subtype = this.TOK_SUBTYPE_MATH.toString();
} else {
token.type = this.TOK_TYPE_NOOP.toString();
}
continue;
}
if (
token.type.toString() === this.TOK_TYPE_OP_IN &&
token.subtype.length === 0
) {
if ("<>=".indexOf(token.value.substr(0, 1)) !== -1) {
token.subtype = this.TOK_SUBTYPE_LOGICAL.toString();
} else if (token.value.toString() === "&") {
token.subtype = this.TOK_SUBTYPE_CONCAT.toString();
} else {
token.subtype = this.TOK_SUBTYPE_MATH.toString();
}
continue;
}
if (
token.type.toString() === this.TOK_TYPE_OPERAND &&
token.subtype.length === 0
) {
if (isNaN(parseFloat(token.value))) {
if (
token.value.toString() === "TRUE" ||
token.value.toString() === "FALSE"
) {
token.subtype = this.TOK_SUBTYPE_LOGICAL.toString();
} else {
token.subtype = this.TOK_SUBTYPE_RANGE.toString();
}
} else {
token.subtype = this.TOK_SUBTYPE_NUMBER.toString();
}
continue;
}
if (token.type.toString() === this.TOK_TYPE_FUNCTION) {
if (token.value.substr(0, 1) === "@") {
token.value = token.value.substr(1).toString();
}
continue;
}
}
tokens2.reset();
// move all tokens to a new collection, excluding all no-ops
tokens = new F_tokens();
while (tokens2.moveNext()) {
if (tokens2.current().type.toString() !== this.TOK_TYPE_NOOP) {
tokens.addRef(tokens2.current());
}
}
tokens.reset();
return tokens;
}
// Pass a range such as A1:B2 along with a
// delimiter to get back a full list of ranges.
//
// Example:
// breakOutRanges("A1:B2", "+"); //Returns A1+A2+B1+B2
static breakOutRanges(rangeStr, delimStr) {
//Quick Check to see if if rangeStr is a valid range
if (!RegExp("[a-z]+[0-9]+:[a-z]+[0-9]+", "gi").test(rangeStr)) {
throw "This is not a valid range: " + rangeStr;
}
//Make the rangeStr lowercase to deal with looping.
let range = rangeStr.split(":"),
startRow = parseInt(range[0].match(/[0-9]+/gi)[0]),
startCol = range[0].match(/[A-Z]+/gi)[0],
startColDec = fromBase26(startCol);
(endRow = parseInt(range[1].match(/[0-9]+/gi)[0])),
(endCol = range[1].match(/[A-Z]+/gi)[0]),
(endColDec = fromBase26(endCol)),
// Total rows and cols
(totalRows = endRow - startRow + 1),
(totalCols = fromBase26(endCol) - fromBase26(startCol) + 1),
// Loop vars
(curCol = 0),
(curRow = 1),
(curCell = ""),
//Return String
(retStr = "");
for (; curRow <= totalRows; curRow += 1) {
for (; curCol < totalCols; curCol += 1) {
// Get the current cell id
curCell = toBase26(startColDec + curCol) + "" + (startRow + curRow - 1);
retStr +=
curCell +
(curRow === totalRows && curCol === totalCols - 1 ? "" : delimStr);
}
curCol = 0;
}
return retStr;
}
//Modified from function at http://en.wikipedia.org/wiki/Hexavigesimal
static toBase26(value) {
value = Math.abs(value);
let converted = "",
iteration = false,
remainder;
// Repeatedly divide the number by 26 and convert the
// remainder into the appropriate letter.
do {
remainder = value % 26;
// Compensate for the last letter of the series being corrected on 2 or more iterations.
if (iteration && value < 25) {
remainder--;
}
converted =
String.fromCharCode(remainder + "A".charCodeAt(0)) + converted;
value = Math.floor((value - remainder) / 26);
iteration = true;
} while (value > 0);
return converted;
}
// This was Modified from a function at http://en.wikipedia.org/wiki/Hexavigesimal
// Pass in the base 26 string, get back integer
static fromBase26(number) {
number = number.toUpperCase();
let s = 0,
i = 0,
dec = 0;
if (number !== null && typeof number !== "undefined" && number.length > 0) {
for (; i < number.length; i++) {
s = number.charCodeAt(number.length - i - 1) - "A".charCodeAt(0);
dec += Math.pow(26, i) * (s + 1);
}
}
return dec - 1;
}
static applyTokenTemplate(token, options, indent, lineBreak, override) {
let lastToken =
typeof arguments[5] === undefined || arguments[5] === null
? null
: arguments[5];
let replaceTokenTmpl = function (inStr) {
return inStr
.replace(/\{\{token\}\}/gi, "{0}")
.replace(/\{\{autoindent\}\}/gi, "{1}")
.replace(/\{\{autolinebreak\}\}/gi, "{2}");
};
let tokenString = "";
if (token.subtype === "text" || token.type === "text") {
tokenString = token.value.toString();
} else if (token.type === "operand" && token.subtype === "range") {
tokenString = token.value.toString();
} else {
tokenString = (token.value.length === 0 ? " " : token.value.toString())
.split(" ")
.join("")
.toString();
}
if (typeof override === "function") {
let returnVal = override(tokenString, token, indent, lineBreak);
tokenString = returnVal.tokenString;
if (!returnVal.useTemplate) {
return tokenString;
}
}
switch (token.type) {
case "function":
//-----------------FUNCTION------------------
switch (token.value) {
case "ARRAY":
tokenString = formatStr(
replaceTokenTmpl(options.tmplFunctionStartArray),
tokenString,
indent,
lineBreak
);
break;
case "ARRAYROW":
tokenString = formatStr(
replaceTokenTmpl(options.tmplFunctionStartArrayRow),
tokenString,
indent,
lineBreak
);
break;
case "ARRAYSTOP":
tokenString = formatStr(
replaceTokenTmpl(options.tmplFunctionStopArray),
tokenString,
indent,
lineBreak
);
break;
case "ARRAYROWSTOP":
tokenString = formatStr(
replaceTokenTmpl(options.tmplFunctionStopArrayRow),
tokenString,
indent,
lineBreak
);
break;
default:
if (token.subtype.toString() === "start") {
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplFunctionStart),
tokenString,
indent,
lineBreak
);
} else {
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplFunctionStop),
tokenString,
indent,
lineBreak
);
}
break;
}
break;
case "operand":
//-----------------OPERAND------------------
switch (token.subtype.toString()) {
case "error":
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplOperandError),
tokenString,
indent,
lineBreak
);
break;
case "range":
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplOperandRange),
tokenString,
indent,
lineBreak
);
break;
case "logical":
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplOperandLogical),
tokenString,
indent,
lineBreak
);
break;
case "number":
tokenString = ExcelFormula.formatStr(
replaceTokenTmpl(options.tmplOperandNumber),
tokenString,
indent,
lineBreak
);
break;
case "text":