@syncfusion/ej2-calculate 35.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/.eslintrc.json +263 -0
  2. package/README.md +132 -0
  3. package/base.d.ts +4 -0
  4. package/base.js +4 -0
  5. package/common.d.ts +4 -0
  6. package/common.js +4 -0
  7. package/dist/ej2-calculate.umd.min.js +11 -0
  8. package/dist/ej2-calculate.umd.min.js.map +1 -0
  9. package/dist/es6/ej2-calculate.es2015.js +24801 -0
  10. package/dist/es6/ej2-calculate.es2015.js.map +1 -0
  11. package/dist/es6/ej2-calculate.es5.js +27229 -0
  12. package/dist/es6/ej2-calculate.es5.js.map +1 -0
  13. package/dist/global/ej2-calculate.min.js +11 -0
  14. package/dist/global/ej2-calculate.min.js.map +1 -0
  15. package/dist/global/index.d.ts +14 -0
  16. package/formulas.d.ts +4 -0
  17. package/formulas.js +4 -0
  18. package/index.d.ts +4 -0
  19. package/index.js +4 -0
  20. package/license +10 -0
  21. package/package.json +30 -0
  22. package/src/base/calculate-model.d.ts +4 -0
  23. package/src/base/calculate-model.js +0 -0
  24. package/src/base/calculate.d.ts +1105 -0
  25. package/src/base/calculate.js +4720 -0
  26. package/src/base/index.d.ts +8 -0
  27. package/src/base/index.js +7 -0
  28. package/src/base/parser.d.ts +208 -0
  29. package/src/base/parser.js +1512 -0
  30. package/src/common/address.d.ts +59 -0
  31. package/src/common/address.js +96 -0
  32. package/src/common/cell-ref.d.ts +32 -0
  33. package/src/common/cell-ref.js +112 -0
  34. package/src/common/common.d.ts +44 -0
  35. package/src/common/common.js +134 -0
  36. package/src/common/enum.d.ts +94 -0
  37. package/src/common/enum.js +97 -0
  38. package/src/common/index.d.ts +9 -0
  39. package/src/common/index.js +8 -0
  40. package/src/common/interface.d.ts +122 -0
  41. package/src/common/interface.js +0 -0
  42. package/src/common/l10n.d.ts +24 -0
  43. package/src/common/l10n.js +54 -0
  44. package/src/common/number-date.d.ts +26 -0
  45. package/src/common/number-date.js +38 -0
  46. package/src/common/sheet-info.d.ts +10 -0
  47. package/src/common/sheet-info.js +17 -0
  48. package/src/common/sort.d.ts +17 -0
  49. package/src/common/sort.js +44 -0
  50. package/src/formulas/array.d.ts +47 -0
  51. package/src/formulas/array.js +718 -0
  52. package/src/formulas/basic.d.ts +47 -0
  53. package/src/formulas/basic.js +181 -0
  54. package/src/formulas/complex-numbers.d.ts +207 -0
  55. package/src/formulas/complex-numbers.js +549 -0
  56. package/src/formulas/database.d.ts +152 -0
  57. package/src/formulas/database.js +579 -0
  58. package/src/formulas/date-time-extra.d.ts +97 -0
  59. package/src/formulas/date-time-extra.js +415 -0
  60. package/src/formulas/date-time-intl.d.ts +57 -0
  61. package/src/formulas/date-time-intl.js +309 -0
  62. package/src/formulas/date-time.d.ts +164 -0
  63. package/src/formulas/date-time.js +1021 -0
  64. package/src/formulas/engineering-base.d.ts +150 -0
  65. package/src/formulas/engineering-base.js +404 -0
  66. package/src/formulas/engineering-bessel.d.ts +65 -0
  67. package/src/formulas/engineering-bessel.js +167 -0
  68. package/src/formulas/engineering-convert.d.ts +35 -0
  69. package/src/formulas/engineering-convert.js +120 -0
  70. package/src/formulas/engineering-misc.d.ts +64 -0
  71. package/src/formulas/engineering-misc.js +168 -0
  72. package/src/formulas/financial-amortization-ecma.d.ts +87 -0
  73. package/src/formulas/financial-amortization-ecma.js +288 -0
  74. package/src/formulas/financial-annuity.d.ts +104 -0
  75. package/src/formulas/financial-annuity.js +419 -0
  76. package/src/formulas/financial-bond-helpers.d.ts +106 -0
  77. package/src/formulas/financial-bond-helpers.js +190 -0
  78. package/src/formulas/financial-cashflow.d.ts +47 -0
  79. package/src/formulas/financial-cashflow.js +203 -0
  80. package/src/formulas/financial-coupons.d.ts +227 -0
  81. package/src/formulas/financial-coupons.js +810 -0
  82. package/src/formulas/financial-depreciation.d.ts +64 -0
  83. package/src/formulas/financial-depreciation.js +247 -0
  84. package/src/formulas/financial-rates.d.ts +54 -0
  85. package/src/formulas/financial-rates.js +140 -0
  86. package/src/formulas/financial-securities.d.ts +126 -0
  87. package/src/formulas/financial-securities.js +488 -0
  88. package/src/formulas/formula-math-helpers.d.ts +212 -0
  89. package/src/formulas/formula-math-helpers.js +599 -0
  90. package/src/formulas/formula-module-base.d.ts +54 -0
  91. package/src/formulas/formula-module-base.js +125 -0
  92. package/src/formulas/formula-owner.d.ts +17 -0
  93. package/src/formulas/formula-owner.js +0 -0
  94. package/src/formulas/index.d.ts +68 -0
  95. package/src/formulas/index.js +67 -0
  96. package/src/formulas/information-advanced.d.ts +44 -0
  97. package/src/formulas/information-advanced.js +157 -0
  98. package/src/formulas/information-ecma.d.ts +33 -0
  99. package/src/formulas/information-ecma.js +88 -0
  100. package/src/formulas/information-extra.d.ts +146 -0
  101. package/src/formulas/information-extra.js +351 -0
  102. package/src/formulas/information.d.ts +34 -0
  103. package/src/formulas/information.js +78 -0
  104. package/src/formulas/logical-extra.d.ts +75 -0
  105. package/src/formulas/logical-extra.js +217 -0
  106. package/src/formulas/logical.d.ts +83 -0
  107. package/src/formulas/logical.js +296 -0
  108. package/src/formulas/lookup-array.d.ts +98 -0
  109. package/src/formulas/lookup-array.js +364 -0
  110. package/src/formulas/lookup-ref-extra.d.ts +64 -0
  111. package/src/formulas/lookup-ref-extra.js +180 -0
  112. package/src/formulas/lookup-ref.d.ts +84 -0
  113. package/src/formulas/lookup-ref.js +263 -0
  114. package/src/formulas/lookup.d.ts +114 -0
  115. package/src/formulas/lookup.js +881 -0
  116. package/src/formulas/math-aggregate.d.ts +74 -0
  117. package/src/formulas/math-aggregate.js +376 -0
  118. package/src/formulas/math-ceiling-ecma.d.ts +42 -0
  119. package/src/formulas/math-ceiling-ecma.js +123 -0
  120. package/src/formulas/math-combinatorics.d.ts +75 -0
  121. package/src/formulas/math-combinatorics.js +290 -0
  122. package/src/formulas/math-misc.d.ts +164 -0
  123. package/src/formulas/math-misc.js +889 -0
  124. package/src/formulas/math-number.d.ts +76 -0
  125. package/src/formulas/math-number.js +261 -0
  126. package/src/formulas/math-rounding.d.ts +115 -0
  127. package/src/formulas/math-rounding.js +728 -0
  128. package/src/formulas/math-trig-hyperbolic.d.ts +55 -0
  129. package/src/formulas/math-trig-hyperbolic.js +113 -0
  130. package/src/formulas/math-trig.d.ts +165 -0
  131. package/src/formulas/math-trig.js +334 -0
  132. package/src/formulas/matrix-base.d.ts +13 -0
  133. package/src/formulas/matrix-base.js +169 -0
  134. package/src/formulas/matrix.d.ts +64 -0
  135. package/src/formulas/matrix.js +161 -0
  136. package/src/formulas/regression-ecma.d.ts +66 -0
  137. package/src/formulas/regression-ecma.js +234 -0
  138. package/src/formulas/statistical-advanced.d.ts +125 -0
  139. package/src/formulas/statistical-advanced.js +756 -0
  140. package/src/formulas/statistical-correlation.d.ts +75 -0
  141. package/src/formulas/statistical-correlation.js +198 -0
  142. package/src/formulas/statistical-dispersion.d.ts +108 -0
  143. package/src/formulas/statistical-dispersion.js +337 -0
  144. package/src/formulas/statistical-distribution-extended.d.ts +176 -0
  145. package/src/formulas/statistical-distribution-extended.js +668 -0
  146. package/src/formulas/statistical-distribution.d.ts +104 -0
  147. package/src/formulas/statistical-distribution.js +368 -0
  148. package/src/formulas/statistical-ecma.d.ts +95 -0
  149. package/src/formulas/statistical-ecma.js +392 -0
  150. package/src/formulas/statistical-extended.d.ts +136 -0
  151. package/src/formulas/statistical-extended.js +340 -0
  152. package/src/formulas/statistical-ifs.d.ts +46 -0
  153. package/src/formulas/statistical-ifs.js +180 -0
  154. package/src/formulas/statistical-moments.d.ts +84 -0
  155. package/src/formulas/statistical-moments.js +269 -0
  156. package/src/formulas/statistical-percentile.d.ts +56 -0
  157. package/src/formulas/statistical-percentile.js +195 -0
  158. package/src/formulas/statistical-sample-base.d.ts +14 -0
  159. package/src/formulas/statistical-sample-base.js +124 -0
  160. package/src/formulas/statistical.d.ts +135 -0
  161. package/src/formulas/statistical.js +542 -0
  162. package/src/formulas/text-advanced.d.ts +64 -0
  163. package/src/formulas/text-advanced.js +222 -0
  164. package/src/formulas/text-ecma.d.ts +54 -0
  165. package/src/formulas/text-ecma.js +106 -0
  166. package/src/formulas/text-extra.d.ts +216 -0
  167. package/src/formulas/text-extra.js +657 -0
  168. package/src/formulas/text.d.ts +125 -0
  169. package/src/formulas/text.js +628 -0
  170. package/src/global.js +1 -0
  171. package/src/index.d.ts +14 -0
  172. package/src/index.js +14 -0
  173. package/tslint.json +111 -0
@@ -0,0 +1,1512 @@
1
+ /**
2
+ * Formula tokenizer and parser for the Calculate engine.
3
+ *
4
+ * Converts Excel-style formula text into an internal token string consumed by
5
+ * {@link Calculate.computeFormula}. Handles operators, library function markers,
6
+ * named ranges, and sheet references.
7
+ *
8
+ * @remarks
9
+ * Prefer {@link Calculate.computeFormula} or {@link Calculate.computeExpression}
10
+ * for evaluation. Use {@link Parser.parseFormula} when you need the parsed token
11
+ * form without computing a result.
12
+ *
13
+ * @hidden
14
+ */
15
+ import { FormulaError } from './index';
16
+ import { CommonErrors, FormulasErrorsStrings, isExternalFileLink } from '../common/index';
17
+ import { isNullOrUndefined } from '@syncfusion/ej2-base';
18
+ var Parser = /** @class */ (function () {
19
+ /**
20
+ * @param {Object} parent - Owning {@link Calculate} engine instance.
21
+ */
22
+ function Parser(parent) {
23
+ this.emptyStr = '';
24
+ this.storedStringText = this.emptyStr;
25
+ this.sheetToken = '!';
26
+ /** @hidden */
27
+ this.tokenAdd = 'a';
28
+ /** @hidden */
29
+ this.tokenSubtract = 's';
30
+ /** @hidden */
31
+ this.tokenMultiply = 'm';
32
+ /** @hidden */
33
+ this.tokenDivide = 'd';
34
+ /** @hidden */
35
+ this.tokenLess = 'l';
36
+ this.charEm = 'r';
37
+ this.charEp = 'x';
38
+ /** @hidden */
39
+ this.tokenGreater = 'g';
40
+ /** @hidden */
41
+ this.tokenEqual = 'e';
42
+ /** @hidden */
43
+ this.tokenLessEq = 'k';
44
+ /** @hidden */
45
+ this.tokenGreaterEq = 'j';
46
+ /** @hidden */
47
+ this.tokenNotEqual = 'o';
48
+ /** @hidden */
49
+ this.tokenAnd = 'c';
50
+ this.tokenEm = 'v';
51
+ this.tokenEp = 't';
52
+ /** @hidden */
53
+ this.tokenOr = String.fromCharCode(126);
54
+ this.charAnd = 'i';
55
+ this.charLess = '<';
56
+ this.charGreater = '>';
57
+ this.charEqual = '=';
58
+ this.charLessEq = 'f';
59
+ this.charGreaterEq = 'h';
60
+ this.charNoEqual = 'z';
61
+ this.stringGreaterEq = '>=';
62
+ this.stringLessEq = '<=';
63
+ this.stringNoEqual = '<>';
64
+ this.stringAnd = '&';
65
+ this.stringOr = '^';
66
+ this.charOr = 'w';
67
+ this.charAdd = '+';
68
+ this.charSubtract = '-';
69
+ this.charMultiply = '*';
70
+ this.charDivide = '/';
71
+ this.fixedReference = '$';
72
+ this.spaceString = ' ';
73
+ this.ignoreBracet = false;
74
+ /**
75
+ * When `true`, the last parse attempt failed.
76
+ *
77
+ * @internal
78
+ */
79
+ this.isError = false;
80
+ /**
81
+ * When `true`, formula text passed to compute is already a parsed token string.
82
+ *
83
+ * @internal
84
+ */
85
+ this.isFormulaParsed = false;
86
+ this.findNamedRange = false;
87
+ this.stringsColl = new Map();
88
+ this.tokens = [
89
+ this.tokenAdd, this.tokenSubtract, this.tokenMultiply, this.tokenDivide, this.tokenLess,
90
+ this.tokenGreater, this.tokenEqual, this.tokenLessEq, this.tokenGreaterEq, this.tokenNotEqual, this.tokenAnd, this.tokenOr
91
+ ];
92
+ this.charNOTop = String.fromCharCode(167);
93
+ this.specialSym = ['~', '@', '#', '?'];
94
+ this.isFailureTriggered = false;
95
+ this.parent = parent;
96
+ }
97
+ /**
98
+ * Parses formula text into the engine's internal token representation.
99
+ *
100
+ * Performs named-range substitution, string literal extraction, operator
101
+ * collapsing, and library-formula marking.
102
+ *
103
+ * @param {string} text - Formula text (leading `=` is optional).
104
+ * @param {string} fkey - Optional formula cell key for named-range / failure context.
105
+ * @returns {string} - Parsed token string, or an error token when parsing fails.
106
+ */
107
+ Parser.prototype.parse = function (text, fkey) {
108
+ var _this = this;
109
+ if (this.parent.isTextEmpty(text)) {
110
+ return text;
111
+ }
112
+ if (isExternalFileLink(text)) {
113
+ return this.parent.getErrorStrings()[CommonErrors.Ref];
114
+ }
115
+ if (this.parent.getFormulaCharacter() !== String.fromCharCode(0) && this.parent.getFormulaCharacter() === text[0]) {
116
+ text = text.substring(1);
117
+ }
118
+ if (this.parent.namedRanges.size > 0 || this.parent.storedData.size > 0) {
119
+ text = this.checkForNamedRangeAndKeyValue(text);
120
+ this.findNamedRange = false;
121
+ }
122
+ // Extract string literals first so sequences like "//" in URLs are not treated as operators.
123
+ var formulaString = this.storeStrings(text);
124
+ text = this.storedStringText;
125
+ text = text.replace(/[-+*/&^]+/g, function (operators) {
126
+ var firstOp = '';
127
+ while (1 < operators.length) {
128
+ switch (operators.substring(0, 2)) {
129
+ case '++':
130
+ operators = '+' + operators.substring(2);
131
+ break;
132
+ case '--':
133
+ operators = '+' + operators.substring(2);
134
+ break;
135
+ case '+-':
136
+ operators = '-' + operators.substring(2);
137
+ break;
138
+ case '-+':
139
+ operators = '-' + operators.substring(2);
140
+ break;
141
+ case '*+':
142
+ operators = '*' + operators.substring(2);
143
+ break;
144
+ case '/+':
145
+ operators = '/' + operators.substring(2);
146
+ break;
147
+ case '^+':
148
+ operators = '^' + operators.substring(2);
149
+ break;
150
+ case '&+':
151
+ operators = '&' + operators.substring(2);
152
+ break;
153
+ case '*-':
154
+ case '/-':
155
+ case '^-':
156
+ case '&-':
157
+ firstOp = operators.substring(0, 1);
158
+ operators = operators.substring(1);
159
+ break;
160
+ default:
161
+ throw new FormulaError(_this.parent.formulaErrorStrings[FormulasErrorsStrings.InvalidExpression], true);
162
+ }
163
+ }
164
+ return firstOp + operators;
165
+ });
166
+ text = text.split('-' + '(' + '-').join('(');
167
+ var i = 0;
168
+ if (isNullOrUndefined(formulaString)) {
169
+ text = text.split(' ').join('');
170
+ }
171
+ text = text.split('=>').join('>=');
172
+ text = text.split('=<').join('<=');
173
+ if (text[text.length - 1] !== this.parent.arithMarker || this.indexOfAny(text, this.tokens) !== (text.length - 2)) {
174
+ text = text.toUpperCase();
175
+ }
176
+ if (text.indexOf(this.sheetToken) > -1) {
177
+ var family = this.parent.getSheetFamilyItem(this.parent.grid);
178
+ if (family.sheetNameToParentObject != null && family.sheetNameToParentObject.size > 0) {
179
+ if (text[0] !== this.sheetToken.toString()) {
180
+ text = this.parent.setTokensForSheets(text);
181
+ }
182
+ var sheetToken = this.parent.getSheetToken(text.split(this.parent.tic).join(this.emptyStr));
183
+ var scopedRange = this.checkScopedRange(text.split('"').join(this.emptyStr).split(this.sheetToken).join(''));
184
+ if (isNullOrUndefined(sheetToken) && sheetToken !== '' && this.parent.namedRanges.size > 0 && scopedRange !== '') {
185
+ text = scopedRange;
186
+ }
187
+ }
188
+ }
189
+ text = this.processIndexArg(text);
190
+ text = this.markLibraryFormulas(text);
191
+ try {
192
+ text = this.formulaAutoCorrection(text);
193
+ }
194
+ catch (ex) {
195
+ var args = {
196
+ message: ex.message, exception: ex, isForceCalculable: ex.formulaCorrection,
197
+ computeForceCalculate: false
198
+ };
199
+ if (!args.isForceCalculable) {
200
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.InvalidExpression];
201
+ }
202
+ if (!this.isFailureTriggered) {
203
+ this.parent.trigger('onFailure', args);
204
+ this.isFailureTriggered = true;
205
+ }
206
+ if (args.isForceCalculable && args.computeForceCalculate) {
207
+ text = this.formulaAutoCorrection(text, args);
208
+ this.parent.storedData.get(fkey).formulaText = '=' + text;
209
+ }
210
+ else {
211
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.InvalidExpression];
212
+ }
213
+ }
214
+ if (!this.ignoreBracet) {
215
+ i = text.indexOf(')');
216
+ while (i > -1) {
217
+ var k = text.substring(0, i).lastIndexOf('(');
218
+ if (k === -1) {
219
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.MismatchedParentheses]);
220
+ }
221
+ if (k === i - 1) {
222
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.EmptyExpression]);
223
+ }
224
+ var s = this.emptyStr;
225
+ if (this.ignoreBracet) {
226
+ s = this.parent.substring(text, k, i - k + 1);
227
+ }
228
+ else {
229
+ s = this.parent.substring(text, k + 1, i - k - 1);
230
+ }
231
+ try {
232
+ text = text.substring(0, k) + this.parseSimple(s) + text.substring(i + 1);
233
+ }
234
+ catch (ex) {
235
+ if (ex === this.parent.formulaErrorStrings[FormulasErrorsStrings.CircularReference]) {
236
+ throw ex;
237
+ }
238
+ var args = this.exceptionArgs(ex);
239
+ if (!this.isFailureTriggered) {
240
+ this.parent.trigger('onFailure', args);
241
+ this.isFailureTriggered = true;
242
+ }
243
+ var errorMessage = (typeof args.exception === 'string') ? args.exception : args.message;
244
+ return (this.parent.getErrorLine(ex) ? '' : '#' + this.parent.getErrorLine(ex) + ': ') + errorMessage;
245
+ }
246
+ i = text.indexOf(')');
247
+ }
248
+ }
249
+ if (!this.ignoreBracet && text.indexOf('(') > -1) {
250
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.MismatchedParentheses]);
251
+ }
252
+ text = this.parseSimple(text);
253
+ if (formulaString !== null && formulaString.size > 0) {
254
+ text = this.setStrings(text, formulaString);
255
+ }
256
+ return text;
257
+ };
258
+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
259
+ Parser.prototype.exceptionArgs = function (ex) {
260
+ return {
261
+ message: ex.message, exception: ex, isForceCalculable: ex.formulaCorrection,
262
+ computeForceCalculate: false
263
+ };
264
+ };
265
+ Parser.prototype.formulaAutoCorrection = function (formula, args, isSubString) {
266
+ var arithemeticArr = ['*', '+', '-', '/', '^', '&'];
267
+ var logicalSym = ['>', '=', '<'];
268
+ var i = 0;
269
+ var form = '';
270
+ var op = '';
271
+ var firstOp = '';
272
+ var secondprevOp = '';
273
+ var secondnextOp = '';
274
+ var firstDigit = '';
275
+ var secondDigit = '';
276
+ var countDigit = 0;
277
+ if (this.parent.formulaErrorStrings.indexOf(formula) > -1) {
278
+ return formula;
279
+ }
280
+ else {
281
+ if (this.indexOfAny(formula, this.specialSym) > -1) {
282
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.InvalidExpression], false);
283
+ }
284
+ // Normalize invalid operator pairs once before the character walk (avoids O(n²) split/join).
285
+ formula = formula.split('-*').join('-').split('/*').join('/').split('*/').join('*').split('-/').join('-').
286
+ split('*+').join('*').split('+*').join('+');
287
+ while (i < formula.length) {
288
+ if ((this.parent.isDigit(formula[i]) && ((formula.length > i + 1)
289
+ && (this.indexOfAny(formula[i + 1], arithemeticArr) > -1)) && ((formula.length > i + 2)
290
+ && (!isNullOrUndefined(formula[i + 2]) && this.indexOfAny(formula[i + 2], arithemeticArr) > -1))) &&
291
+ (formula[i + 2] !== '-' || (formula[i + 1] !== '*' && formula[i + 1] !== '/' && formula[i + 1] !== '^' && formula[i + 1] !== '&'))) {
292
+ if (args && args.computeForceCalculate) {
293
+ if (this.parent.isDigit(formula[i])) {
294
+ if (countDigit < 1) {
295
+ firstDigit = formula[i];
296
+ firstOp = formula[i + 1];
297
+ if (isNullOrUndefined(firstOp)) {
298
+ firstOp = this.emptyStr;
299
+ }
300
+ firstOp = firstOp === '&' ? '' : firstOp;
301
+ countDigit = countDigit + 1;
302
+ form = form + firstDigit + firstOp;
303
+ }
304
+ else if (countDigit < 2) {
305
+ secondDigit = formula[i];
306
+ secondprevOp = formula[i - 1];
307
+ secondnextOp = formula[i + 1];
308
+ countDigit = 0;
309
+ if (secondprevOp === '-') {
310
+ secondnextOp = isNullOrUndefined(secondnextOp) ? this.emptyStr : secondnextOp;
311
+ secondnextOp = secondnextOp === '&' ? '' : secondnextOp;
312
+ form = form + secondprevOp + secondDigit + secondnextOp;
313
+ }
314
+ else {
315
+ secondnextOp = isNullOrUndefined(secondnextOp) ? this.emptyStr : secondnextOp;
316
+ form = form + secondDigit + secondnextOp;
317
+ }
318
+ }
319
+ i = i + 2;
320
+ }
321
+ else {
322
+ form = (formula[i] === '-') ? form + formula[i] : form;
323
+ i = i + 1;
324
+ }
325
+ }
326
+ else {
327
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.ImproperFormula];
328
+ }
329
+ }
330
+ else if ((this.parent.isDigit(formula[i]) || formula[i] === this.parent.rightBracket ||
331
+ this.parent.storedData.has(formula[i].toUpperCase())) && (isNullOrUndefined(formula[i + 1]) ||
332
+ (this.indexOfAny(formula[i + 1], arithemeticArr) > -1 && formula[i + 1] !== '&'))) {
333
+ op = isNullOrUndefined(formula[i + 1]) ? this.emptyStr : formula[i + 1];
334
+ op = op === '&' && formula[i + 2] !== '-' ? '' : op; // for the cases 5&3=>53 and 5&-3=>5-3.
335
+ form = formula[i - 1] === '-' ? form + formula[i - 1] + formula[i] + op : form + formula[i] + op;
336
+ i = i + 2;
337
+ }
338
+ else if (this.indexOfAny(formula[i], logicalSym) > -1 && !isNullOrUndefined(formula[i - 1]) &&
339
+ !isNullOrUndefined(formula[i + 1])) {
340
+ form = form + formula[i];
341
+ i = i + 1;
342
+ }
343
+ else if (formula[i] === 'q') {
344
+ while (formula[i] !== this.parent.leftBracket) {
345
+ form = form + formula[i];
346
+ i = i + 1;
347
+ }
348
+ }
349
+ else if (formula[i] === this.parent.leftBracket || formula[i] === this.parent.rightBracket ||
350
+ formula[i] === '{' || formula[i] === '}' || formula[i] === '(' ||
351
+ formula[i] === ')') {
352
+ form = form + formula[i];
353
+ i = i + 1;
354
+ }
355
+ else if (this.parent.isUpperChar(formula[i]) || formula[i].indexOf(':') > -1 ||
356
+ formula[i] === this.parent.getParseArgumentSeparator() || (formula[i] === '%' &&
357
+ this.parent.isDigit(formula[i - 1])) || this.isOtherLangChar(formula[i])) {
358
+ form = form + formula[i];
359
+ i = i + 1;
360
+ }
361
+ else if (formula[i] === this.parent.tic || formula[i] === ' ' || formula[i] ===
362
+ this.parent.getParseDecimalSeparator() || formula[i] === this.sheetToken || formula[i] === '$' ||
363
+ formula[i] === '_') {
364
+ form = form + formula[i];
365
+ i = i + 1;
366
+ }
367
+ else {
368
+ if (this.parent.isDigit(formula[i])) {
369
+ form = formula[i - 1] === '-' ? form + formula[i - 1] + formula[i] : form + formula[i];
370
+ }
371
+ if (formula[i] === '-' || formula[i] === '+') {
372
+ form = form + formula[i];
373
+ form = form.split('++').join('+').split('+-').join('-').split('-+').join('-');
374
+ }
375
+ if (formula[i] === '/' || formula[i] === '*' || formula[i] === '^') {
376
+ form = form + formula[i];
377
+ }
378
+ if (formula[i] === '&' && (isSubString || formula.substring(i + 1).trim()[0] !== 'q')) {
379
+ form = form + formula[i];
380
+ }
381
+ i = i + 1;
382
+ }
383
+ }
384
+ }
385
+ form = form === this.emptyStr ? formula : form;
386
+ if (this.indexOfAny(form[form.length - 1], arithemeticArr) > -1) {
387
+ form = form.substring(0, form.length - 1);
388
+ }
389
+ form = form.split('--').join('-').split('-+').join('-').split('+-').join('-');
390
+ return form;
391
+ };
392
+ Parser.prototype.checkScopedRange = function (text) {
393
+ var _this = this;
394
+ var scopedRange = this.emptyStr;
395
+ var b = 'NaN';
396
+ var id = this.parent.getSheetID(this.parent.grid);
397
+ var sheet = this.parent.getSheetFamilyItem(this.parent.grid);
398
+ if (text[0] === this.sheetToken.toString()) {
399
+ var i = text.indexOf(this.sheetToken, 1);
400
+ var v = parseInt(text.substr(1, i - 1), 10);
401
+ if (i > 1 && !this.parent.isNaN(v)) {
402
+ text = text.substring(i + 1);
403
+ id = v;
404
+ }
405
+ }
406
+ var token = '!' + id.toString();
407
+ if (sheet === null || sheet.sheetNameToToken == null) {
408
+ return b;
409
+ }
410
+ sheet.sheetNameToToken.forEach(function (value, key) {
411
+ if (sheet.sheetNameToToken.get(key).toString() === token + '!') {
412
+ var s_1 = _this.emptyStr;
413
+ _this.parent.namedRanges.forEach(function (value, key) {
414
+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
415
+ if (!isNullOrUndefined(_this.parent.parentObject)) {
416
+ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
417
+ s_1 = _this.parent.parentObject.getActiveSheet().name + _this.sheetToken + text.toUpperCase();
418
+ }
419
+ else {
420
+ s_1 = sheet.sheetNameToToken.get(key).toUpperCase();
421
+ }
422
+ if (_this.parent.getNamedRanges().has(s_1)) {
423
+ scopedRange = (_this.parent.getNamedRanges().get(s_1)).toUpperCase();
424
+ b = scopedRange;
425
+ }
426
+ });
427
+ }
428
+ });
429
+ return b;
430
+ };
431
+ Parser.prototype.storeStrings = function (tempString) {
432
+ var i = 0;
433
+ var j = 0;
434
+ var id = 0;
435
+ var key = '';
436
+ var storedString = null;
437
+ var condition;
438
+ var ticLoc = tempString.indexOf(this.parent.tic);
439
+ if (ticLoc > -1) {
440
+ i = tempString.indexOf(this.parent.tic);
441
+ while (i > -1 && tempString.length > 0) {
442
+ if (storedString === null) {
443
+ storedString = this.stringsColl;
444
+ }
445
+ j = i + 1 < tempString.length ? tempString.indexOf(this.parent.tic, i + 1) : -1;
446
+ if (j === -1) {
447
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.MismatchedTics]);
448
+ }
449
+ condition = this.parent.substring(tempString, i, j - i + 1);
450
+ key = this.parent.tic + this.spaceString + id.toString() + this.parent.tic;
451
+ storedString = storedString.set(key, condition);
452
+ tempString = tempString.substring(0, i) + key + tempString.substring(j + 1);
453
+ i = i + key.length;
454
+ if (i <= tempString.length) {
455
+ i = tempString.indexOf(this.parent.tic, i);
456
+ }
457
+ id++;
458
+ }
459
+ }
460
+ this.storedStringText = tempString;
461
+ return storedString;
462
+ };
463
+ Parser.prototype.setStrings = function (text, formulaString) {
464
+ formulaString.forEach(function (value, key) {
465
+ text = text.split(key).join(value);
466
+ });
467
+ return text;
468
+ };
469
+ /**
470
+ * Parses a simple (non-library) expression after library formulas are marked.
471
+ *
472
+ * @param {string} formulaText - Partially processed formula text.
473
+ * @returns {string} - Token string with arithmetic markers applied.
474
+ */
475
+ Parser.prototype.parseSimple = function (formulaText) {
476
+ var needToContinue = true;
477
+ var text = formulaText;
478
+ if (text.length > 0 && text[0] === '+') {
479
+ text = text.substring(1);
480
+ }
481
+ if (text === '#DIV/0!') {
482
+ return '#DIV/0!';
483
+ }
484
+ if (text === '#NAME?') {
485
+ return '#NAME?';
486
+ }
487
+ if (text === '') {
488
+ return text;
489
+ }
490
+ if (this.parent.formulaErrorStrings.indexOf(text) > -1) {
491
+ return text;
492
+ }
493
+ text = text.split(this.stringLessEq).join(this.charLessEq);
494
+ text = text.split(this.stringGreaterEq).join(this.charGreaterEq);
495
+ text = text.split(this.stringNoEqual).join(this.charNoEqual);
496
+ text = text.split(this.stringAnd).join(this.charAnd);
497
+ text = text.split(this.stringOr).join(this.charOr);
498
+ text = text.split(this.fixedReference).join(this.emptyStr);
499
+ needToContinue = true;
500
+ var expTokenArray = [this.tokenEp, this.tokenEm];
501
+ var mulTokenArray = [this.tokenMultiply, this.tokenDivide];
502
+ var addTokenArray = [this.tokenAdd, this.tokenSubtract];
503
+ var mulCharArray = [this.charMultiply, this.charDivide];
504
+ var addCharArray = [this.charAdd, this.charSubtract];
505
+ var compareTokenArray = [this.tokenLess, this.tokenGreater, this.tokenEqual, this.tokenLessEq,
506
+ this.tokenGreaterEq, this.tokenNotEqual];
507
+ var compareCharArray = [this.charLess, this.charGreater, this.charEqual, this.charLessEq,
508
+ this.charGreaterEq, this.charNoEqual];
509
+ var expCharArray = [this.charEp, this.charEm];
510
+ var andTokenArray = [this.tokenAnd];
511
+ var andCharArray = [this.charAnd];
512
+ var orCharArray = [this.charOr];
513
+ var orTokenArray = [this.tokenOr];
514
+ text = this.parseSimpleOperators(text, expTokenArray, expCharArray);
515
+ text = this.parseSimpleOperators(text, orTokenArray, orCharArray);
516
+ if (needToContinue) {
517
+ text = this.parseSimpleOperators(text, mulTokenArray, mulCharArray);
518
+ }
519
+ if (needToContinue) {
520
+ text = this.parseSimpleOperators(text, addTokenArray, addCharArray);
521
+ }
522
+ if (needToContinue) {
523
+ text = this.parseSimpleOperators(text, compareTokenArray, compareCharArray);
524
+ }
525
+ if (needToContinue) {
526
+ text = this.parseSimpleOperators(text, andTokenArray, andCharArray);
527
+ }
528
+ return text;
529
+ };
530
+ /**
531
+ * Replaces operator characters with single-character tokens for evaluation.
532
+ *
533
+ * @param {string} formulaText - Formula text containing operator markers.
534
+ * @param {string | number | Object} markers - Replacement token characters.
535
+ * @param {string} operators - Operator strings to replace.
536
+ * @returns {string} - Formula text with operators tokenized.
537
+ */
538
+ Parser.prototype.parseSimpleOperators = function (formulaText, markers, operators) {
539
+ if (this.parent.getErrorStrings().indexOf(formulaText) > -1) {
540
+ return formulaText;
541
+ }
542
+ var text = formulaText;
543
+ var i = 0;
544
+ var op = '';
545
+ for (var c = 0; c < operators.length; c++) {
546
+ op = op + operators[c];
547
+ }
548
+ text = text.split('---').join('-').split('--').join('+').split(this.parent.getParseArgumentSeparator() + '-').join(this.parent.getParseArgumentSeparator() + 'u').split(this.parent.leftBracket + '-').join(this.parent.leftBracket + 'u').split('=-').join('=u');
549
+ text = text.split(',+').join(',').split(this.parent.leftBracket + '+').join(this.parent.leftBracket).split('=+').join('=').split('>+').join('>').split('<+').join('<').split('/+').join('/').split('*+').join('*').split('++').join('+').split('*-').join('*u').split('/-').join('/u').split('w-').join('wu').split('i-').join('iu').toString();
550
+ text = text.split('>-').join('>u').split('<-').join('<u').split('h-').join('hu').split('f-').join('fu').split('z-').join('zu');
551
+ if (text.length > 0 && text[0] === '-') {
552
+ var tokenOrOp = String.fromCharCode(132);
553
+ text = text.substring(1).split('-').join(tokenOrOp);
554
+ text = '0-' + text;
555
+ text = this.parseSimpleOperators(text, [this.tokenSubtract], [this.charSubtract]);
556
+ text = text.split(tokenOrOp).join('-');
557
+ }
558
+ else if (text.length > 0 && text[0] === '+') {
559
+ text = text.substring(1);
560
+ }
561
+ else if (text.length > 0 && text[text.length - 1] === '+') {
562
+ text = text.substring(0, text.length - 1);
563
+ }
564
+ try {
565
+ if (this.indexOfAny(text, operators) > -1) {
566
+ if (text.includes(' ')) {
567
+ var newText = '';
568
+ for (var index = 0; index < text.length; index++) {
569
+ var currChar = text[index];
570
+ if (operators.indexOf(currChar) >= 0) {
571
+ newText = newText.trim() + currChar;
572
+ }
573
+ else if (currChar === ' ' && operators.indexOf(newText[newText.length - 1]) >= 0) {
574
+ continue;
575
+ }
576
+ else {
577
+ newText += currChar;
578
+ }
579
+ }
580
+ text = newText;
581
+ }
582
+ i = this.indexOfAny(text, operators);
583
+ var decimalSep = this.parent.getParseDecimalSeparator();
584
+ while (i > -1) {
585
+ var left = '';
586
+ var right = '';
587
+ var leftIndex = 0;
588
+ var rightIndex = 0;
589
+ var isLeftBool = false;
590
+ var arithOp = ['*', '+', '-', '/', 'w', '=', '<', '>'];
591
+ var isNotOperator = text[i] === this.charNOTop;
592
+ var j = 0;
593
+ if (!isNotOperator) {
594
+ j = i - 1;
595
+ if (text[j] === this.parent.arithMarker) {
596
+ var k = this.findLeftMarker(text.substring(0, j - 1));
597
+ if (k < 0) {
598
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse]);
599
+ }
600
+ left = this.parent.substring(text, k + 1, j - k - 1);
601
+ leftIndex = k + 1;
602
+ }
603
+ else if (text[j] === this.parent.rightBracket) {
604
+ var bracketCount = 0;
605
+ var k = j - 1;
606
+ while (k > 0 && (text[k] !== 'q' || bracketCount !== 0)) {
607
+ if (text[k] === 'q') {
608
+ bracketCount--;
609
+ }
610
+ else if (text[k] === this.parent.rightBracket) {
611
+ bracketCount++;
612
+ }
613
+ k--;
614
+ }
615
+ if (k < 0) {
616
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse]);
617
+ }
618
+ left = this.parent.substring(text, k, j - k + 1);
619
+ leftIndex = k;
620
+ }
621
+ else if (text[j] === this.parent.tic[0]) {
622
+ var l = text.substring(0, j - 1).lastIndexOf(this.parent.tic);
623
+ if (l < 0) {
624
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse]);
625
+ }
626
+ left = this.parent.substring(text, l, j - l + 1);
627
+ leftIndex = l;
628
+ }
629
+ else {
630
+ var period = false;
631
+ while (j > -1 && (this.parent.isDigit(text[j]) ||
632
+ (!period && (text[j] === decimalSep || text[j] === '%')))) {
633
+ if (!this.parent.isDigit(text[j]) && text[j] !== '%') {
634
+ period = true;
635
+ }
636
+ j = j - 1;
637
+ }
638
+ if (j > -1 && period && text[j] === decimalSep) {
639
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.NumberContains2DecimalPoints]);
640
+ }
641
+ j = j + 1;
642
+ if (j === 0 || (j > 0 && !this.parent.isUpperChar(text[j - 1]))) {
643
+ left = 'n' + this.parent.substring(text, j, i - j);
644
+ leftIndex = j;
645
+ }
646
+ else if (j > 0 && text[j - 1] === 'E' && ((text.substring(j - 4, j) === 'TRUE' && (!isNullOrUndefined(text[j - 5]) ? arithOp.indexOf(text[j - 5]) > -1 : true)) ||
647
+ (text.substring(j - 5, j) === 'FALSE' && (!isNullOrUndefined(text[j - 6]) ? arithOp.indexOf(text[j - 6]) > -1 : true))) && (text.substring(j + 1, j + 5) === 'TRUE' || text.substring(j + 1, j + 6) === 'FALSE')) {
648
+ j = text.substring(j - 4, j) === 'TRUE' ? j - 4 : j - 5;
649
+ left = text.substring(j, i) === 'TRUE' ? 'n1' : (text.substring(j, i) === 'FALSE' ? 'n0' : left);
650
+ leftIndex = j;
651
+ isLeftBool = true;
652
+ }
653
+ else {
654
+ j = j - 1;
655
+ while (j > -1 && (this.parent.isUpperChar(text[j]) || // Check if character is uppercase alphabets.
656
+ this.parent.isDigit(text[j]) || // Check if character is a digit.
657
+ text[j] === '_')) { // Check if character is an underscore ('_'), for defined names cases.
658
+ j = j - 1;
659
+ }
660
+ if (j > -1 && text[j] === this.sheetToken) {
661
+ j = j - 1;
662
+ while (j > -1 && text[j] !== this.sheetToken) {
663
+ j = j - 1;
664
+ }
665
+ if (j > -1 && text[j] === this.sheetToken) {
666
+ j = j - 1;
667
+ }
668
+ }
669
+ if (j > -1 && text[j] === ':') {
670
+ //// handle range operands
671
+ j = j - 1;
672
+ while (j > -1 && this.parent.isDigit(text[j])) {
673
+ j = j - 1;
674
+ }
675
+ while (j > -1 && this.parent.isUpperChar(text[j])) {
676
+ j = j - 1;
677
+ }
678
+ if (j > -1 && text[j] === this.sheetToken) {
679
+ j--;
680
+ while (j > -1 && text[j] !== this.sheetToken) {
681
+ j--;
682
+ }
683
+ if (j > -1 && text[j] === this.sheetToken) {
684
+ j--;
685
+ }
686
+ }
687
+ j = j + 1;
688
+ left = this.parent.substring(text, j, i - j);
689
+ left = this.parent.getCellFrom(left);
690
+ }
691
+ else {
692
+ var uFound = false;
693
+ if (j > 0 && !this.parent.isUpperChar(text[j])) {
694
+ uFound = text[j] === 'u' && text[j - 1] === this.parent.getParseArgumentSeparator();
695
+ }
696
+ if (!uFound) {
697
+ j = j + 1;
698
+ }
699
+ left = this.parent.substring(text, j, i - j);
700
+ }
701
+ this.parent.updateDependentCell(left);
702
+ leftIndex = j;
703
+ }
704
+ if ((this.parent.namedRanges.size > 0 && this.parent.namedRanges.has(left.toUpperCase())) ||
705
+ (this.parent.storedData.has(left.toUpperCase()))) {
706
+ left = 'n' + this.checkForNamedRangeAndKeyValue(left);
707
+ }
708
+ }
709
+ }
710
+ else {
711
+ leftIndex = i;
712
+ }
713
+ if (i === text.length - 1) {
714
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.ExpressionCannotEndWithAnOperator]);
715
+ }
716
+ else {
717
+ j = i + 1;
718
+ var uFound = text[j] === 'u'; // for 3*-2
719
+ if (uFound) {
720
+ j = j + 1;
721
+ }
722
+ if (text[j] === this.parent.tic[0]) {
723
+ var k = text.substring(j + 1).indexOf(this.parent.tic);
724
+ if (k < 0) {
725
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse];
726
+ }
727
+ right = this.parent.substring(text, j, k + 2);
728
+ rightIndex = k + j + 2;
729
+ }
730
+ else if (text[j] === this.parent.arithMarker) {
731
+ var k = this.findRightMarker(text.substring(j + 1));
732
+ if (k < 0) {
733
+ throw new FormulaError(this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse]);
734
+ }
735
+ right = this.parent.substring(text, j + 1, k);
736
+ rightIndex = k + j + 2;
737
+ }
738
+ else if (text[j] === 'q') {
739
+ var bracketCount = 0;
740
+ var k = j + 1;
741
+ while (k < text.length && (text[k] !== this.parent.rightBracket || bracketCount !== 0)) {
742
+ if (text[k] === this.parent.rightBracket) {
743
+ bracketCount++;
744
+ }
745
+ else if (text[k] === 'q') {
746
+ bracketCount--;
747
+ }
748
+ k++;
749
+ }
750
+ if (k === text.length) {
751
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.CannotParse];
752
+ }
753
+ right = this.parent.substring(text, j, k - j + 1);
754
+ if (uFound) {
755
+ right = 'u' + right;
756
+ }
757
+ rightIndex = k + 1;
758
+ }
759
+ else if (this.parent.isDigit(text[j]) || text[j] === decimalSep) {
760
+ var period = (text[j] === decimalSep);
761
+ j = j + 1;
762
+ while (j < text.length && (this.parent.isDigit(text[j]) ||
763
+ (!period && text[j] === decimalSep))) {
764
+ if (text[j] === decimalSep) {
765
+ period = true;
766
+ }
767
+ j = j + 1;
768
+ }
769
+ if (j < text.length && text[j] === '%') {
770
+ j += 1;
771
+ }
772
+ if (period && j < text.length && text[j] === decimalSep) {
773
+ throw this.parent.formulaErrorStrings[FormulasErrorsStrings.NumberContains2DecimalPoints];
774
+ }
775
+ right = 'n' + this.parent.substring(text, i + 1, j - i - 1);
776
+ rightIndex = j;
777
+ }
778
+ else if (this.parent.isUpperChar(text[j]) || text[j] === this.sheetToken ||
779
+ text[j] === 'u') {
780
+ if (text[j] === this.sheetToken) {
781
+ j = j + 1;
782
+ while (j < text.length && text[j] !== this.sheetToken) {
783
+ j = j + 1;
784
+ }
785
+ }
786
+ j = j + 1;
787
+ var jTemp = 0;
788
+ var inbracket = false;
789
+ while (j < text.length && (this.parent.isUpperChar(text[j]) || text[j] === '_'
790
+ || text[j] === decimalSep || text[j] === '[' || text[j] === ']' ||
791
+ text[j] === '#' || text[j] === ' ' || text[j] === '%' || text[j] ===
792
+ decimalSep && inbracket)) {
793
+ if (j !== text.length - 1 && text[j] === '[' && text[j + 1] === '[') {
794
+ inbracket = true;
795
+ }
796
+ if (j !== text.length - 1 && text[j] === ']' && text[j + 1] === ']') {
797
+ inbracket = false;
798
+ }
799
+ j++;
800
+ jTemp++;
801
+ }
802
+ var noCellReference = (j === text.length) || !this.parent.isDigit(text[j]);
803
+ if (jTemp > 1) {
804
+ while (j < text.length && (this.parent.isUpperChar(text[j]) ||
805
+ this.parent.isDigit(text[j]) || text[j] === ' ' || text[j] === '_')) {
806
+ j++;
807
+ }
808
+ noCellReference = true;
809
+ }
810
+ while (j < text.length && this.parent.isDigit(text[j])) {
811
+ j = j + 1;
812
+ }
813
+ if (j < text.length && text[j] === ':') {
814
+ j = j + 1;
815
+ if (j < text.length && text[j] === this.sheetToken) {
816
+ j++;
817
+ while (j < text.length && text[j] !== this.sheetToken) {
818
+ j = j + 1;
819
+ }
820
+ if (j < text.length && text[j] === this.sheetToken) {
821
+ j++;
822
+ }
823
+ }
824
+ while (j < text.length && this.parent.isUpperChar(text[j])) {
825
+ j = j + 1;
826
+ }
827
+ while (j < text.length && this.parent.isDigit(text[j])) {
828
+ j = j + 1;
829
+ }
830
+ j = j - 1;
831
+ right = this.parent.substring(text, i + 1, j - i);
832
+ right = this.parent.getCellFrom(right);
833
+ }
834
+ else {
835
+ j = j - 1;
836
+ right = this.parent.substring(text, i + 1, j - i);
837
+ uFound = text[j] === 'u';
838
+ if (uFound) {
839
+ right = 'u' + right;
840
+ }
841
+ }
842
+ if (noCellReference && right.startsWith(this.sheetToken)) {
843
+ noCellReference = !this.parent.isCellReference(right);
844
+ }
845
+ if (!noCellReference) {
846
+ this.parent.updateDependentCell(right);
847
+ }
848
+ if ((this.parent.namedRanges.size > 0 && this.parent.namedRanges.has(right.toUpperCase()))
849
+ || (this.parent.storedData.has(right.toUpperCase()))) {
850
+ right = 'n' + this.checkForNamedRangeAndKeyValue(right);
851
+ }
852
+ var isPrevArithOp = ['*', '+', '-', '/', 'w'].indexOf(text[j - right.length]) > -1;
853
+ right = right === 'TRUE' && (isLeftBool || isPrevArithOp) ? 'n1' : (right === 'FALSE' && (isLeftBool || isPrevArithOp) ? 'n0' : right);
854
+ rightIndex = j + 1;
855
+ }
856
+ }
857
+ var p = op.indexOf(text[i]);
858
+ var s = this.parent.arithMarker + left + right + markers[p] + this.parent.arithMarker;
859
+ if (leftIndex > 0) {
860
+ s = text.substring(0, leftIndex) + s;
861
+ }
862
+ if (rightIndex < text.length) {
863
+ s = s + text.substring(rightIndex);
864
+ }
865
+ s = s.split(this.parent.arithMarker2).join(this.parent.arithMarker.toString());
866
+ text = s;
867
+ i = this.indexOfAny(text, operators);
868
+ }
869
+ }
870
+ else {
871
+ if (text.length > 0 && (this.parent.isUpperChar(text[0]) || text[0] === this.sheetToken)) {
872
+ var isCharacter = true;
873
+ var checkLetter = true;
874
+ var oneTokenFound = false;
875
+ var textLen = text.length;
876
+ for (var k = 0; k < textLen; ++k) {
877
+ if (text[k] === this.sheetToken) {
878
+ if (k > 0 && !oneTokenFound) {
879
+ throw this.parent.getErrorStrings()[CommonErrors.Ref];
880
+ }
881
+ oneTokenFound = true;
882
+ k++;
883
+ while (k < textLen && this.parent.isDigit(text[k])) {
884
+ k++;
885
+ }
886
+ if (k === textLen || text[k] !== this.sheetToken) {
887
+ isCharacter = false;
888
+ break;
889
+ }
890
+ }
891
+ else {
892
+ if (!checkLetter && this.parent.isChar(text[k])) {
893
+ isCharacter = false;
894
+ break;
895
+ }
896
+ if (this.parent.isChar(text[k]) || this.parent.isDigit(text[k]) || text[k] ===
897
+ this.sheetToken) {
898
+ checkLetter = this.parent.isUpperChar(text[k]);
899
+ }
900
+ else {
901
+ isCharacter = false;
902
+ break;
903
+ }
904
+ }
905
+ }
906
+ if (isCharacter) {
907
+ this.parent.updateDependentCell(text);
908
+ }
909
+ }
910
+ }
911
+ return text;
912
+ }
913
+ catch (ex) {
914
+ if (ex === this.parent.formulaErrorStrings[FormulasErrorsStrings.CircularReference]) {
915
+ throw ex;
916
+ }
917
+ throw ex;
918
+ }
919
+ };
920
+ /**
921
+ * Returns the index of the first character in `text` that appears in `operators`.
922
+ *
923
+ * @param {string} text - Text to search.
924
+ * @param {string | number | Object} operators - Characters to find.
925
+ * @returns {string | number} - Index of the first match, or `-1`.
926
+ */
927
+ Parser.prototype.indexOfAny = function (text, operators) {
928
+ for (var i = 0; i < text.length; i++) {
929
+ if (operators.indexOf(text[i]) > -1) {
930
+ return i;
931
+ }
932
+ }
933
+ return -1;
934
+ };
935
+ /**
936
+ * Finds the matching left arithmetic marker for a right-marker position.
937
+ *
938
+ * @param {string} text - Tokenized formula text.
939
+ * @returns {string | number} - Index of the left marker.
940
+ */
941
+ Parser.prototype.findLeftMarker = function (text) {
942
+ var ret = -1;
943
+ if (text.indexOf(this.parent.arithMarker) > -1) {
944
+ var bracketLevel = 0;
945
+ for (var i = text.length - 1; i >= 0; --i) {
946
+ if (text[i] === this.parent.rightBracket) {
947
+ bracketLevel--;
948
+ }
949
+ else if (text[i] === this.parent.leftBracket) {
950
+ bracketLevel++;
951
+ }
952
+ else if (text[i] === this.parent.arithMarker && bracketLevel === 0) {
953
+ ret = i;
954
+ break;
955
+ }
956
+ }
957
+ }
958
+ return ret;
959
+ };
960
+ /**
961
+ * Finds the matching right arithmetic marker for a left-marker position.
962
+ *
963
+ * @param {string} text - Tokenized formula text.
964
+ * @returns {string | number} - Index of the right marker.
965
+ */
966
+ Parser.prototype.findRightMarker = function (text) {
967
+ var ret = -1;
968
+ if (text.indexOf(this.parent.arithMarker) > -1) {
969
+ var bracketLevel = 0;
970
+ for (var j = 0; j < text.length; ++j) {
971
+ if (text[j] === this.parent.rightBracket) {
972
+ bracketLevel--;
973
+ }
974
+ else if (text[j] === this.parent.leftBracket) {
975
+ bracketLevel++;
976
+ }
977
+ else if (text[j] === this.parent.arithMarker && bracketLevel === 0) {
978
+ ret = j;
979
+ break;
980
+ }
981
+ }
982
+ }
983
+ return ret;
984
+ };
985
+ /**
986
+ * Entry point that resets parser state and returns {@link parse} output.
987
+ *
988
+ * @param {string} formula - Formula text to parse.
989
+ * @param {string} fKey - Optional formula cell key.
990
+ * @returns {string} - Parsed token string.
991
+ */
992
+ Parser.prototype.parseFormula = function (formula, fKey) {
993
+ if (formula.length > 0 && formula[0] === this.parent.getFormulaCharacter()) {
994
+ formula = formula.substring(1);
995
+ }
996
+ if (formula.indexOf('#REF!') > -1) {
997
+ return this.parent.getErrorStrings()[CommonErrors.Ref];
998
+ }
999
+ if (formula.length > 0 && formula[0] === '+') {
1000
+ formula = formula.substring(1);
1001
+ }
1002
+ try {
1003
+ this.isFailureTriggered = false;
1004
+ this.isError = false;
1005
+ formula = this.parse(formula.trim(), fKey);
1006
+ this.isFormulaParsed = true;
1007
+ }
1008
+ catch (ex) {
1009
+ var args = this.exceptionArgs(ex);
1010
+ if (!this.isFailureTriggered) {
1011
+ this.parent.trigger('onFailure', args);
1012
+ this.isFailureTriggered = true;
1013
+ }
1014
+ var errorMessage = (typeof args.exception === 'string') ? args.exception : args.message;
1015
+ formula = (isNullOrUndefined(this.parent.getErrorLine(ex)) ? '' : '#' + this.parent.getErrorLine(ex) + ': ') + errorMessage;
1016
+ this.isError = true;
1017
+ }
1018
+ return formula;
1019
+ };
1020
+ /**
1021
+ * Wraps recognized library function calls with `q` markers and bracket tokens.
1022
+ *
1023
+ * @param {string} formula - Formula text.
1024
+ * @returns {string} - Formula text with library calls marked for evaluation.
1025
+ */
1026
+ Parser.prototype.markLibraryFormulas = function (formula) {
1027
+ var bracCount = 0;
1028
+ var rightParens = formula.indexOf(')');
1029
+ if (rightParens === -1) {
1030
+ formula = this.markNamedRanges(formula);
1031
+ }
1032
+ else {
1033
+ var _loop_1 = function () {
1034
+ var parenCount = 0;
1035
+ var leftParens = rightParens - 1;
1036
+ while (leftParens > -1 && (formula[leftParens] !== '(' || parenCount !== 0)) {
1037
+ if (formula[leftParens] === ')') {
1038
+ parenCount++;
1039
+ }
1040
+ // else if (formula[leftParens] === ')') {
1041
+ // parenCount--;
1042
+ // }
1043
+ leftParens--;
1044
+ }
1045
+ if (leftParens === -1) {
1046
+ throw new FormulaError(this_1.parent.formulaErrorStrings[FormulasErrorsStrings.MismatchedParentheses]);
1047
+ }
1048
+ var i = leftParens - 1;
1049
+ // Include digits so names like LOG10, BIN2DEC, and HEX2OCT are recognized.
1050
+ while (i > -1 && (this_1.parent.isChar(formula[i]) || this_1.parent.isDigit(formula[i]) ||
1051
+ formula[i] === '.')) {
1052
+ i--;
1053
+ }
1054
+ var len = leftParens - i - 1;
1055
+ var libFormula = this_1.parent.substring(formula, i + 1, len);
1056
+ if (len > 0 && !isNullOrUndefined(this_1.parent.getFunction(libFormula)) && (!libFormula.includes('.') ||
1057
+ this_1.isValidDotFormulaName(libFormula))) {
1058
+ var substr = this_1.parent.substring(formula, leftParens, rightParens - leftParens + 1);
1059
+ var argsSep = this_1.parent.getParseArgumentSeparator();
1060
+ if (libFormula === 'AREAS') {
1061
+ this_1.ignoreBracet = true;
1062
+ }
1063
+ else {
1064
+ this_1.ignoreBracet = false;
1065
+ if (((libFormula.includes('IFS') && libFormula !== 'COUNTIFS') || libFormula === 'MATCH') && substr.includes('{')) {
1066
+ var leftBraceIdx = substr.indexOf('{');
1067
+ var criteriaStr = this_1.parent.substring(substr, leftBraceIdx, substr.indexOf('}') - leftBraceIdx + 1);
1068
+ substr = substr.split(criteriaStr).join(criteriaStr.split(argsSep).join(this_1.parent.tic + this_1.parent.tic));
1069
+ }
1070
+ }
1071
+ try {
1072
+ var args = void 0;
1073
+ substr = substr.split('(').join('').split(')').join('');
1074
+ substr = '(' + this_1.formulaAutoCorrection(substr, args, true) + ')';
1075
+ }
1076
+ catch (ex) {
1077
+ var args = {
1078
+ message: ex.message, exception: ex,
1079
+ isForceCalculable: ex.formulaCorrection, computeForceCalculate: false
1080
+ };
1081
+ if (!args.isForceCalculable) {
1082
+ throw this_1.parent.formulaErrorStrings[FormulasErrorsStrings.ImproperFormula];
1083
+ }
1084
+ if (!this_1.isFailureTriggered) {
1085
+ this_1.parent.trigger('onFailure', args);
1086
+ this_1.isFailureTriggered = true;
1087
+ bracCount = bracCount + 1;
1088
+ }
1089
+ args.computeForceCalculate = bracCount > 0 ? true : args.computeForceCalculate;
1090
+ if (args.isForceCalculable) {
1091
+ if (args.computeForceCalculate) {
1092
+ substr = substr.split('(').join('').split(')').join('');
1093
+ substr = '(' + this_1.formulaAutoCorrection(substr, args) + ')';
1094
+ }
1095
+ else {
1096
+ throw this_1.parent.formulaErrorStrings[FormulasErrorsStrings.ImproperFormula];
1097
+ }
1098
+ }
1099
+ else {
1100
+ throw this_1.parent.formulaErrorStrings[FormulasErrorsStrings.ImproperFormula];
1101
+ }
1102
+ }
1103
+ substr = this_1.markNamedRanges(substr);
1104
+ substr = this_1.swapInnerParens(substr);
1105
+ substr = this_1.addParensToArgs(substr);
1106
+ var id = substr.lastIndexOf(argsSep);
1107
+ if (id === -1) {
1108
+ if (substr.length > 2 && substr[0] === '(' && substr[substr.length - 1] === ')') {
1109
+ if (substr[1] !== '{' && substr[1] !== '(') {
1110
+ substr = substr.substring(0, substr.length - 1) + '}' + substr.substring(substr.length - 1);
1111
+ substr = substr[0] + '{' + substr.substring(1);
1112
+ }
1113
+ }
1114
+ }
1115
+ formula = formula.substring(0, i + 1) + 'q' + this_1.parent.substring(formula, i + 1, len) +
1116
+ (substr.split('(').join(this_1.parent.leftBracket))
1117
+ .split(')').join(this_1.parent.rightBracket) + formula.substring(rightParens + 1);
1118
+ }
1119
+ else {
1120
+ var isIfErrorArg_1 = function (beforeFormula) {
1121
+ if (beforeFormula.includes('IFERROR(')) {
1122
+ var idx = beforeFormula.lastIndexOf('(');
1123
+ return beforeFormula.substring(idx - 7, idx) === 'IFERROR' || isIfErrorArg_1(beforeFormula.substring(0, idx));
1124
+ }
1125
+ return false;
1126
+ };
1127
+ if (len > 0 && !isIfErrorArg_1(formula.substring(0, i + 1))) {
1128
+ return { value: this_1.parent.getErrorStrings()[CommonErrors.Name] };
1129
+ }
1130
+ else {
1131
+ var s = this_1.emptyStr;
1132
+ if (leftParens > 0) {
1133
+ s = formula.substring(0, leftParens);
1134
+ }
1135
+ s = s + '{' + this_1.parent.substring(formula, leftParens + 1, rightParens - leftParens - 1) + '}';
1136
+ if (rightParens < formula.length) {
1137
+ s = s + formula.substring(rightParens + 1);
1138
+ }
1139
+ s = this_1.markNamedRanges(s);
1140
+ formula = s;
1141
+ }
1142
+ }
1143
+ rightParens = formula.indexOf(')');
1144
+ };
1145
+ var this_1 = this;
1146
+ while (rightParens > -1) {
1147
+ var state_1 = _loop_1();
1148
+ if (typeof state_1 === "object")
1149
+ return state_1.value;
1150
+ }
1151
+ }
1152
+ formula = (formula.split('{').join('(')).split('}').join(')');
1153
+ return formula;
1154
+ };
1155
+ /**
1156
+ * Swaps nested parentheses inside a library-formula substring for token safety.
1157
+ *
1158
+ * @hidden
1159
+ * @param {string} fSubstr - Library formula substring.
1160
+ * @returns {string} - Substring with inner parentheses rewritten.
1161
+ */
1162
+ Parser.prototype.swapInnerParens = function (fSubstr) {
1163
+ if (fSubstr.length > 2) {
1164
+ fSubstr = fSubstr[0] + fSubstr.substr(1, fSubstr.length - 2).split('(').join('{').split(')').join('}') + fSubstr[fSubstr.length - 1];
1165
+ }
1166
+ return fSubstr;
1167
+ };
1168
+ /**
1169
+ * Ensures library-formula arguments are wrapped for nested parsing.
1170
+ *
1171
+ * @hidden
1172
+ * @param {string} fSubstr - Library formula substring.
1173
+ * @returns {string} - Substring with argument parentheses normalized.
1174
+ */
1175
+ Parser.prototype.addParensToArgs = function (fSubstr) {
1176
+ if (fSubstr.length === 0) {
1177
+ return this.emptyStr;
1178
+ }
1179
+ var rightSides = [];
1180
+ rightSides.push(this.parent.getParseArgumentSeparator());
1181
+ rightSides.push(this.parent.rightBracket);
1182
+ var id = fSubstr.lastIndexOf(this.parent.getParseArgumentSeparator());
1183
+ var k = 0;
1184
+ if (id === -1) {
1185
+ if (fSubstr.length > 2 && fSubstr[0] === '(' && fSubstr[fSubstr.length - 1] === ')') {
1186
+ if (fSubstr[1] !== '{' && fSubstr[1] !== '(') {
1187
+ fSubstr = fSubstr.substring(0, fSubstr.length - 1) + '}' + fSubstr.substring(fSubstr.length - 1);
1188
+ fSubstr = fSubstr[0] + '{' + fSubstr.substring(1);
1189
+ }
1190
+ else {
1191
+ var marker = ['+', '-', '*', '/'];
1192
+ id = this.lastIndexOfAny(fSubstr, marker);
1193
+ if (k === 0 && fSubstr[fSubstr.length - 1] === ')') {
1194
+ k = fSubstr.length - 1;
1195
+ }
1196
+ if (k > 0) {
1197
+ if (fSubstr[id + 1] !== '{' && fSubstr[id - 1] === '}') {
1198
+ fSubstr = fSubstr.substr(0, k) + '}' + fSubstr.substr(k);
1199
+ fSubstr = fSubstr.substr(0, id + 1) + '{' + fSubstr.substr(id + 1);
1200
+ }
1201
+ }
1202
+ }
1203
+ }
1204
+ }
1205
+ else {
1206
+ var oneTimeOnly = true;
1207
+ while (id > -1) {
1208
+ var j = this.indexOfAny(fSubstr.substring(id + 1, fSubstr.length), rightSides);
1209
+ if (j >= 0) {
1210
+ j = id + j + 1;
1211
+ }
1212
+ else if (j === -1 && fSubstr[fSubstr.length - 1] === ')') {
1213
+ j = fSubstr.length - 1;
1214
+ }
1215
+ if (j > 0) {
1216
+ if (fSubstr[id + 1] !== '{' && fSubstr[j - 1] !== '}' && fSubstr[j - 1] !== '¢') {
1217
+ fSubstr = fSubstr.substr(0, j).trim() + '}' + fSubstr.substr(j);
1218
+ fSubstr = fSubstr.substr(0, id + 1) + '{' + fSubstr.substr(id + 1).trim();
1219
+ }
1220
+ }
1221
+ id = fSubstr.substr(0, id).lastIndexOf(this.parent.getParseArgumentSeparator());
1222
+ if (oneTimeOnly && id === -1 && fSubstr[0] === '(') {
1223
+ id = 0;
1224
+ oneTimeOnly = false;
1225
+ }
1226
+ }
1227
+ }
1228
+ fSubstr = fSubstr.split('{}').join(this.emptyStr);
1229
+ return fSubstr;
1230
+ };
1231
+ /**
1232
+ * @hidden
1233
+ * @param {string} text - specify the text
1234
+ * @param {string[]} operators - specify the operators
1235
+ * @returns {number} - returns last Index Of Any.
1236
+ */
1237
+ Parser.prototype.lastIndexOfAny = function (text, operators) {
1238
+ for (var i = text.length - 1; i > -1; i--) {
1239
+ if (operators.indexOf(text[i]) > -1) {
1240
+ return i;
1241
+ }
1242
+ }
1243
+ return -1;
1244
+ };
1245
+ /**
1246
+ * Substitutes named ranges in formula text with their defined addresses.
1247
+ *
1248
+ * @param {string} formula - Formula text that may contain named ranges.
1249
+ * @returns {string} - Formula text with names expanded.
1250
+ */
1251
+ Parser.prototype.markNamedRanges = function (formula) {
1252
+ var markers = [')', this.parent.getParseArgumentSeparator(), '}', '+', '-', '*', '/', '<', '>', '=', '&', ':'];
1253
+ var i = (formula.length > 0 && (formula[0] === '(' || formula[0] === '{')) ? 1 : 0;
1254
+ if (formula.indexOf('#N/A') > -1) {
1255
+ formula = formula.split('#N/A').join('#N~A');
1256
+ }
1257
+ if (formula.indexOf('#DIV/0!') > -1) {
1258
+ formula = formula.split('#DIV/0!').join('#DIV~0!');
1259
+ }
1260
+ var end = this.indexOfAny(formula.substring(i), markers);
1261
+ while (end > -1 && end + i < formula.length) {
1262
+ var scopedRange = this.emptyStr;
1263
+ var s = null;
1264
+ if ((this.parent.substring(formula, i, end)).indexOf('[') > -1) {
1265
+ s = this.getTableRange(this.parent.substring(formula, i, end));
1266
+ }
1267
+ else if (this.parent.storedData.has(this.parent.substring(formula, i, end))) {
1268
+ s = this.checkForNamedRangeAndKeyValue(this.parent.substring(formula, i, end));
1269
+ }
1270
+ else if (this.parent.namedRanges.has(this.parent.substring(formula, i, end))) {
1271
+ s = this.checkForNamedRangeAndKeyValue(this.parent.substring(formula, i, end));
1272
+ }
1273
+ if (isNullOrUndefined(s)) {
1274
+ scopedRange = this.checkScopedRange(this.parent.substring(formula, i, end));
1275
+ if (scopedRange !== 'NaN') {
1276
+ this.findNamedRange = true;
1277
+ s = scopedRange;
1278
+ }
1279
+ else if (this.parent.substring(formula, i, end).startsWith(this.sheetToken.toString())) {
1280
+ //let formulaStr: number = this.parent.substring(formula, i, end).indexOf(this.sheetToken, 1);
1281
+ // if (formulaStr > 1) {
1282
+ // s = this.parent.namedRanges.get(this.parent.substring
1283
+ // (formula.substring(i), formulaStr + 1, end - formulaStr - 1));
1284
+ // }
1285
+ }
1286
+ if (!isNullOrUndefined(s) && this.findNamedRange) {
1287
+ if (s.indexOf(this.fixedReference) > -1) {
1288
+ s = s.split(this.fixedReference).join(this.emptyStr);
1289
+ }
1290
+ }
1291
+ }
1292
+ if (!isNullOrUndefined(s)) {
1293
+ s = s.toUpperCase();
1294
+ s = this.parent.setTokensForSheets(s);
1295
+ s = this.markLibraryFormulas(s);
1296
+ }
1297
+ if (!isNullOrUndefined(s) && s !== this.emptyStr) {
1298
+ formula = formula.substring(0, i) + s + formula.substring(i + end);
1299
+ i += s.length + 1;
1300
+ }
1301
+ else {
1302
+ i += end + 1;
1303
+ while (i < formula.length && !this.parent.isUpperChar(formula[i]) && formula[i] !== this.sheetToken) {
1304
+ i++;
1305
+ }
1306
+ }
1307
+ end = i;
1308
+ if (i < formula.length - 1 && formula[i] === '{') {
1309
+ i = i + 1;
1310
+ }
1311
+ end = this.indexOfAny(formula.substring(i), markers);
1312
+ while (end === 0 && i < formula.length - 1) {
1313
+ i++;
1314
+ end = this.indexOfAny(formula.substring(i), markers);
1315
+ }
1316
+ if ((end === -1 || formula.substring(i).indexOf('[') > -1) && i < formula.length) {
1317
+ if (formula.substring(i).indexOf('[') > -1) {
1318
+ s = this.getTableRange(formula.substring(i));
1319
+ }
1320
+ else {
1321
+ if (this.parent.storedData.has(formula.substring(i))) {
1322
+ s = this.parent.storedData.size > 0 ? this.checkForNamedRangeAndKeyValue(formula.substring(i)) : s;
1323
+ }
1324
+ else {
1325
+ s = this.parent.namedRanges.size > 0 ? this.checkForNamedRangeAndKeyValue(formula.substring(i)) : s;
1326
+ }
1327
+ }
1328
+ if (isNullOrUndefined(s)) {
1329
+ scopedRange = this.checkScopedRange(formula.substring(i));
1330
+ if (scopedRange !== 'NaN') {
1331
+ s = scopedRange;
1332
+ }
1333
+ }
1334
+ if (!isNullOrUndefined(s) && s !== this.emptyStr) {
1335
+ s = s.toUpperCase();
1336
+ s = this.parent.setTokensForSheets(s);
1337
+ s = this.markLibraryFormulas(s);
1338
+ if (s != null) {
1339
+ var val = formula.substring(i);
1340
+ if (val[val.length - 1] === ')') {
1341
+ formula = formula.substring(0, i) + s + ')';
1342
+ }
1343
+ else {
1344
+ formula = formula.substring(0, i) + s;
1345
+ }
1346
+ i += s.toString().length + 1;
1347
+ }
1348
+ }
1349
+ end = (i < formula.length) ? this.indexOfAny(formula.substring(i), markers) : -1;
1350
+ }
1351
+ }
1352
+ if (formula.indexOf('#N~A') > -1) {
1353
+ formula = formula.split('#N~A').join('#N/A');
1354
+ }
1355
+ if (formula.indexOf('#DIV~0!') > -1) {
1356
+ formula = formula.split('#DIV~0!').join('#DIV/0!');
1357
+ }
1358
+ return formula;
1359
+ };
1360
+ /**
1361
+ * Resolves named ranges and standalone key values embedded in formula text.
1362
+ *
1363
+ * @param {string} text - Formula text.
1364
+ * @returns {string} - Text with names/keys replaced by their values or addresses.
1365
+ */
1366
+ Parser.prototype.checkForNamedRangeAndKeyValue = function (text) {
1367
+ var scopedRange = this.emptyStr;
1368
+ if (text.indexOf('[') > -1) {
1369
+ var namerangeValue = this.getTableRange(text);
1370
+ if (!isNullOrUndefined(namerangeValue)) {
1371
+ this.findNamedRange = true;
1372
+ text = namerangeValue;
1373
+ }
1374
+ }
1375
+ scopedRange = this.checkScopedRange(text);
1376
+ if (scopedRange !== 'NaN') {
1377
+ this.findNamedRange = true;
1378
+ text = scopedRange;
1379
+ }
1380
+ else {
1381
+ if (text.indexOf(this.sheetToken) > -1) {
1382
+ var sheet = this.parent.getSheetFamilyItem(this.parent.grid);
1383
+ var value = text.split('"').join(this.emptyStr);
1384
+ value = value.substr(0, value.indexOf(this.sheetToken));
1385
+ if (sheet.sheetNameToToken.has(value.toUpperCase())) {
1386
+ /* eslint-disable */
1387
+ var sheetIndex = parseInt(sheet.sheetNameToToken.get(value.toUpperCase()).split(this.sheetToken).join(this.emptyStr));
1388
+ // if (!ej.isNullOrUndefined(this.parentObject) && this.parentObject.pluginName == "ejSpreadsheet") {
1389
+ // var name = text.replace(value, this.parentObject.model.sheets[(sheetIndex + 1)].sheetInfo.text.toUpperCase()).split("'").join(this._string_empty);
1390
+ // if (this.getNamedRanges().length > 0 && this.getNamedRanges().contains(name.toUpperCase())) {
1391
+ // text = name;
1392
+ // }
1393
+ // }
1394
+ /* tslint-enable */
1395
+ }
1396
+ }
1397
+ if (this.parent.storedData.size > 0 && this.parent.storedData.has(text)) {
1398
+ text = 'A' + this.parent.colIndex(text);
1399
+ }
1400
+ if (this.parent.namedRanges.size > 0 && this.parent.namedRanges.has(text.toUpperCase())) {
1401
+ if (!isNullOrUndefined(this.parent.parentObject)) {
1402
+ text = this.parse(this.parent.namedRanges.get(text.toUpperCase()));
1403
+ }
1404
+ else {
1405
+ text = this.parse(this.parent.namedRanges.get(text.toUpperCase()));
1406
+ text = this.parent.setTokensForSheets(text);
1407
+ if (text.indexOf(this.fixedReference) > -1) {
1408
+ text.split(this.fixedReference).join(this.emptyStr);
1409
+ }
1410
+ this.findNamedRange = true;
1411
+ }
1412
+ }
1413
+ if (this.findNamedRange) {
1414
+ if (text[0] !== '!' && text[0] !== 'q' && text[0] !== 'bq') {
1415
+ text = this.parent.setTokensForSheets(text);
1416
+ if (text.indexOf(this.fixedReference) > -1) {
1417
+ text = text.split(this.fixedReference).join(this.emptyStr);
1418
+ }
1419
+ }
1420
+ }
1421
+ }
1422
+ return text;
1423
+ };
1424
+ Parser.prototype.getTableRange = function (text) {
1425
+ text = text.replace(' ', this.emptyStr).toUpperCase();
1426
+ var name = text.replace(']', this.emptyStr).replace('#DATA', this.emptyStr);
1427
+ var tableName = name;
1428
+ if (name.indexOf(this.parent.getParseArgumentSeparator()) > -1) {
1429
+ tableName = name.substring(0, name.indexOf(this.parent.getParseArgumentSeparator())).replace('[', this.emptyStr);
1430
+ name = name.replace('[', this.emptyStr).replace(this.parent.getParseArgumentSeparator(), '_');
1431
+ }
1432
+ var range = this.emptyStr;
1433
+ return name.toUpperCase();
1434
+ };
1435
+ Parser.prototype.findNextEndIndex = function (formula, loc) {
1436
+ var count = 0;
1437
+ var l = loc;
1438
+ var found = false;
1439
+ while (!found && loc < formula.length) {
1440
+ if (formula[l] === '[') {
1441
+ count++;
1442
+ }
1443
+ else if (formula[l] === ']') {
1444
+ count--;
1445
+ if (count === 0) {
1446
+ found = true;
1447
+ }
1448
+ }
1449
+ loc++;
1450
+ }
1451
+ loc = loc - l;
1452
+ return loc;
1453
+ };
1454
+ ;
1455
+ Parser.prototype.processIndexArg = function (text) {
1456
+ var search = 'INDEX((';
1457
+ var idx = text.indexOf(search);
1458
+ if (idx !== -1) {
1459
+ var start = idx + (search.length - 1);
1460
+ if (start !== -1) {
1461
+ var braces = 0;
1462
+ var end = -1;
1463
+ for (var j = start; j < text.length; j++) {
1464
+ if (text[j] === '(') {
1465
+ braces++;
1466
+ }
1467
+ else if (text[j] === ')') {
1468
+ braces--;
1469
+ }
1470
+ if (braces === 0) {
1471
+ end = j;
1472
+ break;
1473
+ }
1474
+ }
1475
+ if (end > start) {
1476
+ text = text.slice(0, start) +
1477
+ this.parent.tic +
1478
+ text.slice(start + 1, end) +
1479
+ this.parent.tic +
1480
+ text.slice(end + 1);
1481
+ }
1482
+ }
1483
+ }
1484
+ return text;
1485
+ };
1486
+ Parser.prototype.isValidDotFormulaName = function (formulaName) {
1487
+ formulaName = formulaName.toUpperCase();
1488
+ var dotFormulaNames = ['STDEV.P', 'STDEV.S', 'VAR.S', 'VAR.P', 'T.TEST', 'F.TEST', 'Z.TEST', 'CHISQ.TEST',
1489
+ 'COVARIANCE.S', 'COVARIANCE.P', 'CHISQ.DIST', 'F.DIST', 'T.DIST', 'NETWORKDAYS.INTL', 'WORKDAY.INTL',
1490
+ 'ERROR.TYPE', 'ECMA.CEILING', 'ISO.CEILING'];
1491
+ return dotFormulaNames.some(function (formula) { return formulaName === formula; });
1492
+ };
1493
+ Parser.prototype.isOtherLangChar = function (char) {
1494
+ var code = char.charCodeAt(0);
1495
+ // checking for Latin characters.
1496
+ return (code >= 192 && code <= 214) || (code >= 216 && code <= 246) || (code >= 248 && code <= 255);
1497
+ };
1498
+ /**
1499
+ * Clears parser scratch state and detaches from the owning Calculate engine.
1500
+ * @internal
1501
+ * @returns {void}
1502
+ */
1503
+ Parser.prototype.destroy = function () {
1504
+ this.stringsColl.clear();
1505
+ this.storedStringText = this.emptyStr;
1506
+ this.isError = false;
1507
+ this.isFormulaParsed = false;
1508
+ this.parent = null;
1509
+ };
1510
+ return Parser;
1511
+ }());
1512
+ export { Parser };