@borgar/fx 4.13.0 → 5.0.0

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 (141) hide show
  1. package/dist/index-BMr6cTgc.d.cts +1444 -0
  2. package/dist/index-BMr6cTgc.d.ts +1444 -0
  3. package/dist/index.cjs +3054 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +1 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +2984 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/xlsx/index.cjs +3120 -0
  10. package/dist/xlsx/index.cjs.map +1 -0
  11. package/dist/xlsx/index.d.cts +55 -0
  12. package/dist/xlsx/index.d.ts +55 -0
  13. package/dist/xlsx/index.js +3049 -0
  14. package/dist/xlsx/index.js.map +1 -0
  15. package/docs/API.md +2959 -718
  16. package/docs/AST_format.md +2 -2
  17. package/eslint.config.mjs +40 -0
  18. package/lib/a1.spec.ts +32 -0
  19. package/lib/a1.ts +26 -0
  20. package/lib/addA1RangeBounds.ts +50 -0
  21. package/lib/addTokenMeta.spec.ts +166 -0
  22. package/lib/{addTokenMeta.js → addTokenMeta.ts} +53 -33
  23. package/lib/astTypes.ts +211 -0
  24. package/lib/cloneToken.ts +29 -0
  25. package/lib/{constants.js → constants.ts} +6 -3
  26. package/lib/fixRanges.spec.ts +220 -0
  27. package/lib/fixRanges.ts +260 -0
  28. package/lib/fromCol.spec.ts +15 -0
  29. package/lib/{fromCol.js → fromCol.ts} +1 -1
  30. package/lib/index.spec.ts +119 -0
  31. package/lib/index.ts +76 -0
  32. package/lib/isNodeType.ts +151 -0
  33. package/lib/isType.spec.ts +208 -0
  34. package/lib/{isType.js → isType.ts} +26 -25
  35. package/lib/lexers/{advRangeOp.js → advRangeOp.ts} +1 -1
  36. package/lib/lexers/{canEndRange.js → canEndRange.ts} +2 -2
  37. package/lib/lexers/{lexBoolean.js → lexBoolean.ts} +25 -6
  38. package/lib/lexers/{lexContext.js → lexContext.ts} +14 -6
  39. package/lib/lexers/{lexError.js → lexError.ts} +3 -3
  40. package/lib/lexers/{lexFunction.js → lexFunction.ts} +3 -2
  41. package/lib/lexers/lexNameFuncCntx.ts +112 -0
  42. package/lib/lexers/{lexNamed.js → lexNamed.ts} +4 -4
  43. package/lib/lexers/{lexNewLine.js → lexNewLine.ts} +3 -2
  44. package/lib/lexers/{lexNumber.js → lexNumber.ts} +4 -3
  45. package/lib/lexers/{lexOperator.js → lexOperator.ts} +5 -4
  46. package/lib/lexers/lexRange.ts +15 -0
  47. package/lib/lexers/{lexRangeA1.js → lexRangeA1.ts} +11 -7
  48. package/lib/lexers/{lexRangeR1C1.js → lexRangeR1C1.ts} +10 -6
  49. package/lib/lexers/{lexRangeTrim.js → lexRangeTrim.ts} +3 -2
  50. package/lib/lexers/{lexRefOp.js → lexRefOp.ts} +4 -3
  51. package/lib/lexers/{lexString.js → lexString.ts} +3 -3
  52. package/lib/lexers/{lexStructured.js → lexStructured.ts} +5 -5
  53. package/lib/lexers/{lexWhitespace.js → lexWhitespace.ts} +3 -2
  54. package/lib/lexers/sets.ts +51 -0
  55. package/lib/mergeRefTokens.spec.ts +141 -0
  56. package/lib/{mergeRefTokens.js → mergeRefTokens.ts} +14 -9
  57. package/lib/nodeTypes.ts +54 -0
  58. package/lib/parse.spec.ts +1410 -0
  59. package/lib/{parser.js → parse.ts} +81 -63
  60. package/lib/parseA1Range.spec.ts +233 -0
  61. package/lib/parseA1Range.ts +206 -0
  62. package/lib/parseA1Ref.spec.ts +337 -0
  63. package/lib/parseA1Ref.ts +115 -0
  64. package/lib/parseR1C1Range.ts +191 -0
  65. package/lib/parseR1C1Ref.spec.ts +323 -0
  66. package/lib/parseR1C1Ref.ts +127 -0
  67. package/lib/parseRef.spec.ts +90 -0
  68. package/lib/parseRef.ts +240 -0
  69. package/lib/{parseSRange.js → parseSRange.ts} +15 -10
  70. package/lib/parseStructRef.spec.ts +168 -0
  71. package/lib/parseStructRef.ts +76 -0
  72. package/lib/stringifyA1Range.spec.ts +72 -0
  73. package/lib/stringifyA1Range.ts +72 -0
  74. package/lib/stringifyA1Ref.spec.ts +64 -0
  75. package/lib/stringifyA1Ref.ts +59 -0
  76. package/lib/{stringifyPrefix.js → stringifyPrefix.ts} +17 -2
  77. package/lib/stringifyR1C1Range.spec.ts +92 -0
  78. package/lib/stringifyR1C1Range.ts +73 -0
  79. package/lib/stringifyR1C1Ref.spec.ts +63 -0
  80. package/lib/stringifyR1C1Ref.ts +67 -0
  81. package/lib/stringifyStructRef.spec.ts +124 -0
  82. package/lib/stringifyStructRef.ts +113 -0
  83. package/lib/stringifyTokens.ts +15 -0
  84. package/lib/toCol.spec.ts +11 -0
  85. package/lib/{toCol.js → toCol.ts} +4 -4
  86. package/lib/tokenTypes.ts +76 -0
  87. package/lib/tokenize-srefs.spec.ts +429 -0
  88. package/lib/tokenize.spec.ts +2103 -0
  89. package/lib/tokenize.ts +346 -0
  90. package/lib/translate.spec.ts +35 -0
  91. package/lib/translateToA1.spec.ts +247 -0
  92. package/lib/translateToA1.ts +231 -0
  93. package/lib/translateToR1C1.spec.ts +227 -0
  94. package/lib/translateToR1C1.ts +145 -0
  95. package/lib/types.ts +179 -0
  96. package/lib/xlsx/index.spec.ts +27 -0
  97. package/lib/xlsx/index.ts +32 -0
  98. package/package.json +45 -31
  99. package/tsconfig.json +28 -0
  100. package/typedoc-ignore-links.ts +17 -0
  101. package/typedoc.json +41 -0
  102. package/.eslintrc +0 -22
  103. package/benchmark/benchmark.js +0 -48
  104. package/benchmark/formulas.json +0 -15677
  105. package/dist/fx.d.ts +0 -823
  106. package/dist/fx.js +0 -2
  107. package/dist/package.json +0 -1
  108. package/lib/a1.js +0 -348
  109. package/lib/a1.spec.js +0 -458
  110. package/lib/addTokenMeta.spec.js +0 -153
  111. package/lib/astTypes.js +0 -96
  112. package/lib/extraTypes.js +0 -74
  113. package/lib/fixRanges.js +0 -104
  114. package/lib/fixRanges.spec.js +0 -171
  115. package/lib/fromCol.spec.js +0 -11
  116. package/lib/index.js +0 -134
  117. package/lib/index.spec.js +0 -67
  118. package/lib/isType.spec.js +0 -168
  119. package/lib/lexer-srefs.spec.js +0 -324
  120. package/lib/lexer.js +0 -264
  121. package/lib/lexer.spec.js +0 -1953
  122. package/lib/lexers/lexRange.js +0 -8
  123. package/lib/lexers/sets.js +0 -38
  124. package/lib/mergeRefTokens.spec.js +0 -121
  125. package/lib/package.json +0 -1
  126. package/lib/parseRef.js +0 -157
  127. package/lib/parseRef.spec.js +0 -71
  128. package/lib/parseStructRef.js +0 -48
  129. package/lib/parseStructRef.spec.js +0 -164
  130. package/lib/parser.spec.js +0 -1208
  131. package/lib/rc.js +0 -341
  132. package/lib/rc.spec.js +0 -403
  133. package/lib/stringifyStructRef.js +0 -80
  134. package/lib/stringifyStructRef.spec.js +0 -182
  135. package/lib/toCol.spec.js +0 -11
  136. package/lib/translate-toA1.spec.js +0 -214
  137. package/lib/translate-toRC.spec.js +0 -197
  138. package/lib/translate.js +0 -239
  139. package/lib/translate.spec.js +0 -21
  140. package/rollup.config.mjs +0 -22
  141. package/tsd.json +0 -12
package/dist/fx.d.ts DELETED
@@ -1,823 +0,0 @@
1
- /**
2
- * Fill the any missing bounds in range objects. Top will be set to 0, bottom to
3
- * 1048575, left to 0, and right to 16383, if they are `null` or `undefined`.
4
- * ```js
5
- * addA1RangeBounds({
6
- * context: [ 'Sheet1' ],
7
- * range: {
8
- * top: 0,
9
- * left: 0,
10
- * bottom: 1,
11
- * $top: true,
12
- * $left: false,
13
- * $bottom: false,
14
- * }
15
- * });
16
- * // => {
17
- * // context: [ 'Sheet1' ],
18
- * // range: {
19
- * // top: 0,
20
- * // left: 0,
21
- * // bottom: 1,
22
- * // right: 16383,
23
- * // $top: true,
24
- * // $left: false,
25
- * // $bottom: false,
26
- * // $right: false
27
- * // }
28
- * // }
29
- * ```
30
- *
31
- * @param range The range part of a reference object.
32
- * @returns same range with missing bounds filled in.
33
- */
34
- export declare function addA1RangeBounds(range: RangeA1): RangeA1;
35
-
36
- /**
37
- * Runs through a list of tokens and adds extra attributes such as matching
38
- * parens and ranges.
39
- * The `context` parameter defines default reference attributes:
40
- * `{ workbookName: 'report.xlsx', sheetName: 'Sheet1' }`.
41
- * If supplied, these are used to match `A1` to `Sheet1!A1`.
42
- * All tokens will be tagged with a `.depth` number value to indicating the
43
- * level of nesting in parentheses as well as an `.index` number indicating
44
- * their zero based position in the list.
45
- * The returned output will be the same array of tokens but the following
46
- * properties will added to tokens (as applicable):
47
- * #### Parentheses ( )
48
- * Matching parens will be tagged with `.groupId` string identifier as well as
49
- * a `.depth` number value (indicating the level of nesting).
50
- * Closing parens without a counterpart will be tagged with `.error`
51
- * (boolean true).
52
- * #### Curly brackets { }
53
- * Matching curly brackets will be tagged with `.groupId` string identifier.
54
- * These may not be nested in Excel.
55
- * Closing curly brackets without a counterpart will be tagged with `.error`
56
- * (boolean `true`).
57
- * #### Ranges (`REF_RANGE` or `REF_BEAM` type tokens)
58
- * All ranges will be tagged with `.groupId` string identifier regardless of
59
- * the number of times they occur.
60
- * #### Tokens of type `UNKNOWN`
61
- * All will be tagged with `.error` (boolean `true`).
62
- *
63
- * @param tokenlist An array of tokens (from `tokenize()`)
64
- * @param [context={}] A contest used to match `A1` to `Sheet1!A1`.
65
- * @param [context.sheetName=''] An implied sheet name ('Sheet1')
66
- * @param [context.workbookName=''] An implied workbook name ('report.xlsx')
67
- * @returns The input array with the enchanced tokens
68
- */
69
- export declare function addTokenMeta(tokenlist: Array<Token>, context?: {
70
- /** An implied sheet name ('Sheet1') */
71
- sheetName?: string;
72
- /** An implied workbook name ('report.xlsx') */
73
- workbookName?: string;
74
- }): Array<TokenEnhanced>;
75
-
76
- /**
77
- * Normalizes A1 style ranges and structured references in a formula or list of
78
- * tokens.
79
- * It ensures that that the top and left coordinates of an A1 range are on the
80
- * left-hand side of a colon operator:
81
- * `B2:A1` → `A1:B2`
82
- * `1:A1` → `A1:1`
83
- * `A:A1` → `A1:A`
84
- * `B:A` → `A:B`
85
- * `2:1` → `1:2`
86
- * `A1:A1` → `A1`
87
- * When `{ addBounds: true }` is passed as an option, the missing bounds are
88
- * also added. This can be done to ensure Excel compatible ranges. The fixes
89
- * then additionally include:
90
- * `1:A1` → `A1:1` → `1:1`
91
- * `A:A1` → `A1:A` → `A:A`
92
- * `A1:A` → `A:A`
93
- * `A1:1` → `A:1`
94
- * `B2:B` → `B2:1048576`
95
- * `B2:2` → `B2:XFD2`
96
- * Structured ranges are normalized cleaned up to have consistent order and
97
- * capitalization of sections as well as removing redundant ones.
98
- * Returns the same formula with the ranges updated. If an array of tokens was
99
- * supplied, then a new array is returned.
100
- *
101
- * @param formula A string (an Excel formula) or a token list that should be adjusted.
102
- * @param [options={}] Options
103
- * @param [options.addBounds=false] Fill in any undefined bounds of range objects. Top to 0, bottom to 1048575, left to 0, and right to 16383.
104
- * @param [options.thisRow=false] Enforces using the `[#This Row]` instead of the `@` shorthand when serializing structured ranges.
105
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
106
- * @returns A formula string or token list (depending on which was input)
107
- */
108
- export declare function fixRanges(formula: (string | Array<Token>), options?: {
109
- /** Fill in any undefined bounds of range objects. Top to 0, bottom to 1048575, left to 0, and right to 16383. */
110
- addBounds?: boolean;
111
- /** Enforces using the `[#This Row]` instead of the `@` shorthand when serializing structured ranges. */
112
- thisRow?: boolean;
113
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
114
- xlsx?: boolean;
115
- }): (string | Array<Token>);
116
-
117
- /**
118
- * Convert a column string representation to a 0 based
119
- * offset number (`"C"` = `2`).
120
- * The method expects a valid column identifier made up of _only_
121
- * A-Z letters, which may be either upper or lower case. Other input will
122
- * return garbage.
123
- *
124
- * @param columnString The column string identifier
125
- * @returns Zero based column index number
126
- */
127
- export declare function fromCol(columnString: string): number;
128
-
129
- /**
130
- * Determines whether the specified token is an error.
131
- * Returns `true` if the input is a token of type ERROR (`#VALUE!`). In all
132
- * other cases `false` is returned.
133
- *
134
- * @param token The token
135
- * @returns True if the specified token is error, False otherwise.
136
- */
137
- export declare function isError(token: any): boolean;
138
-
139
- /**
140
- * Determines whether the specified token is a function.
141
- * Returns `true` if the input is a token of type FUNCTION.
142
- * In all other cases `false` is returned.
143
- *
144
- * @param token The token
145
- * @returns True if the specified token is function, False otherwise.
146
- */
147
- export declare function isFunction(token: any): boolean;
148
-
149
- /**
150
- * Returns `true` if the input is a token of type FX_PREFIX (leading `=` in
151
- * formula). In all other cases `false` is returned.
152
- *
153
- * @param token The token
154
- * @returns True if the specified token is effects prefix, False otherwise.
155
- */
156
- export declare function isFxPrefix(token: any): boolean;
157
-
158
- /**
159
- * Determines whether the specified token is a literal.
160
- * Returns `true` if the input is a token of type BOOLEAN (`TRUE` or `FALSE`),
161
- * ERROR (`#VALUE!`), NUMBER (123.4), or STRING (`"lorem ipsum"`). In all other
162
- * cases `false` is returned.
163
- *
164
- * @param token The token
165
- * @returns True if the specified token is literal, False otherwise.
166
- */
167
- export declare function isLiteral(token: any): boolean;
168
-
169
- /**
170
- * Determines whether the specified token is an operator.
171
- * Returns `true` if the input is a token of type OPERATOR (`+` or `:`). In all
172
- * other cases `false` is returned.
173
- *
174
- * @param token The token
175
- * @returns True if the specified token is operator, False otherwise.
176
- */
177
- export declare function isOperator(token: any): boolean;
178
-
179
- /**
180
- * Determines whether the specified token is a range.
181
- * Returns `true` if the input is a token that has a type of either REF_RANGE
182
- * (`A1` or `A1:B2`), REF_TERNARY (`A1:A`, `A1:1`, `1:A1`, or `A:A1`), or
183
- * REF_BEAM (`A:A` or `1:1`). In all other cases `false` is returned.
184
- *
185
- * @param token A token
186
- * @returns True if the specified token is range, False otherwise.
187
- */
188
- export declare function isRange(token: any): boolean;
189
-
190
- /**
191
- * Determines whether the specified token is a reference.
192
- * Returns `true` if the input is a token of type REF_RANGE (`A1` or `A1:B2`),
193
- * REF_TERNARY (`A1:A`, `A1:1`, `1:A1`, or `A:A1`), REF_BEAM (`A:A` or `1:1`),
194
- * or REF_NAMED (`myrange`). In all other cases `false` is returned.
195
- *
196
- * @param token The token
197
- * @returns True if the specified token is reference, False otherwise.
198
- */
199
- export declare function isReference(token: any): boolean;
200
-
201
- /**
202
- * Determines whether the specified token is whitespace.
203
- * Returns `true` if the input is a token of type WHITESPACE (` `) or
204
- * NEWLINE (`\n`). In all other cases `false` is returned.
205
- *
206
- * @param token The token
207
- * @returns True if the specified token is whitespace, False otherwise.
208
- */
209
- export declare function isWhitespace(token: any): boolean;
210
-
211
- /**
212
- * Merges context with reference tokens as possible in a list of tokens.
213
- * When given a tokenlist, this function returns a new list with ranges returned
214
- * as whole references (`Sheet1!A1:B2`) rather than separate tokens for each
215
- * part: (`Sheet1`,`!`,`A1`,`:`,`B2`).
216
- *
217
- * @param tokenlist An array of tokens (from `tokenize()`)
218
- * @returns A new list of tokens with range parts merged.
219
- */
220
- export declare function mergeRefTokens(tokenlist: Array<Token>): Array<Token>;
221
-
222
- /**
223
- * Parses a string formula or list of tokens into an AST.
224
- * The parser requires `mergeRefs` to have been `true` in tokenlist options,
225
- * because it does not recognize reference context tokens.
226
- * The AST Abstract Syntax Tree's format is documented in
227
- * [AST_format.md](./AST_format.md)
228
- *
229
- * @param formula An Excel formula string (an Excel expression) or an array of tokens.
230
- * @param [options={}] Options
231
- * @param [options.allowNamed=true] Enable parsing names as well as ranges.
232
- * @param [options.allowTernary=false] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
233
- * @param [options.looseRefCalls=false] Permits any function call where otherwise only functions that return references would be permitted.
234
- * @param [options.negativeNumbers=true] Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree).
235
- * @param [options.permitArrayCalls=false] Function calls are allowed as elements of arrays. This is a feature in Google Sheets while Excel does not allow it.
236
- * @param [options.permitArrayRanges=false] Ranges are allowed as elements of arrays. This is a feature in Google Sheets while Excel does not allow it.
237
- * @param [options.r1c1=false] Ranges are expected to be in the R1C1 style format rather than the more popular A1 style.
238
- * @param [options.withLocation=false] Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }`
239
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
240
- * @returns An AST of nodes
241
- */
242
- export declare function parse(formula: (string | Array<Token>), options?: {
243
- /** Enable parsing names as well as ranges. */
244
- allowNamed?: boolean;
245
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
246
- allowTernary?: boolean;
247
- /** Permits any function call where otherwise only functions that return references would be permitted. */
248
- looseRefCalls?: boolean;
249
- /** Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree). */
250
- negativeNumbers?: boolean;
251
- /** Function calls are allowed as elements of arrays. This is a feature in Google Sheets while Excel does not allow it. */
252
- permitArrayCalls?: boolean;
253
- /** Ranges are allowed as elements of arrays. This is a feature in Google Sheets while Excel does not allow it. */
254
- permitArrayRanges?: boolean;
255
- /** Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. */
256
- r1c1?: boolean;
257
- /** Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` */
258
- withLocation?: boolean;
259
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
260
- xlsx?: boolean;
261
- }): AstExpression;
262
-
263
- /**
264
- * Parse a string reference into an object representing it.
265
- * ```js
266
- * parseA1Ref('Sheet1!A$1:$B2');
267
- * // => {
268
- * // context: [ 'Sheet1' ],
269
- * // range: {
270
- * // top: 0,
271
- * // left: 0,
272
- * // bottom: 1,
273
- * // right: 1
274
- * // $top: true,
275
- * // $left: false,
276
- * // $bottom: false,
277
- * // $right: true
278
- * // }
279
- * // }
280
- * ```
281
- * For A:A or A1:A style ranges, `null` will be used for any dimensions that the
282
- * syntax does not specify:
283
- *
284
- * @param refString An A1-style reference string
285
- * @param [options={}] Options
286
- * @param [options.allowNamed=true] Enable parsing names as well as ranges.
287
- * @param [options.allowTernary=false] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
288
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
289
- * @returns An object representing a valid reference or null if it is invalid.
290
- */
291
- export declare function parseA1Ref(refString: string, options?: {
292
- /** Enable parsing names as well as ranges. */
293
- allowNamed?: boolean;
294
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
295
- allowTernary?: boolean;
296
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
297
- xlsx?: boolean;
298
- }): (ReferenceA1 | null);
299
-
300
- /**
301
- * Parse a string reference into an object representing it.
302
- * ```js
303
- * parseR1C1Ref('Sheet1!R[9]C9:R[9]C9');
304
- * // => {
305
- * // context: [ 'Sheet1' ],
306
- * // range: {
307
- * // r0: 9,
308
- * // c0: 8,
309
- * // r1: 9,
310
- * // c1: 8,
311
- * // $c0: true,
312
- * // $c1: true
313
- * // $r0: false,
314
- * // $r1: false
315
- * // }
316
- * // }
317
- * ```
318
- *
319
- * @param refString An R1C1-style reference string
320
- * @param [options={}] Options
321
- * @param [options.allowNamed=true] Enable parsing names as well as ranges.
322
- * @param [options.allowTernary=false] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
323
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
324
- * @returns An object representing a valid reference or null if it is invalid.
325
- */
326
- export declare function parseR1C1Ref(refString: string, options?: {
327
- /** Enable parsing names as well as ranges. */
328
- allowNamed?: boolean;
329
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
330
- allowTernary?: boolean;
331
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
332
- xlsx?: boolean;
333
- }): (ReferenceR1C1 | null);
334
-
335
- /**
336
- * Parse a structured reference string into an object representing it.
337
- * ```js
338
- * parseStructRef('workbook.xlsx!tableName[[#Data],[Column1]:[Column2]]');
339
- * // => {
340
- * // context: [ 'workbook.xlsx' ],
341
- * // sections: [ 'data' ],
342
- * // columns: [ 'my column', '@foo' ],
343
- * // table: 'tableName',
344
- * // }
345
- * ```
346
- * For A:A or A1:A style ranges, `null` will be used for any dimensions that the
347
- * syntax does not specify:
348
- *
349
- * @param ref A structured reference string
350
- * @param [options={}] Options
351
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
352
- * @returns An object representing a valid reference or null if it is invalid.
353
- */
354
- export declare function parseStructRef(ref: string, options?: {
355
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
356
- xlsx?: boolean;
357
- }): (ReferenceStruct | null);
358
-
359
- /**
360
- * Get an A1-style string representation of a reference object.
361
- * ```js
362
- * stringifyA1Ref({
363
- * context: [ 'Sheet1' ],
364
- * range: {
365
- * top: 0,
366
- * left: 0,
367
- * bottom: 1,
368
- * right: 1,
369
- * $top: true,
370
- * $left: false,
371
- * $bottom: false,
372
- * $right: true
373
- * }
374
- * });
375
- * // => 'Sheet1!A$1:$B2'
376
- * ```
377
- *
378
- * @param refObject A reference object
379
- * @param [options={}] Options
380
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
381
- * @returns The reference in A1-style string format
382
- */
383
- export declare function stringifyA1Ref(refObject: ReferenceA1, options?: {
384
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
385
- xlsx?: boolean;
386
- }): string;
387
-
388
- /**
389
- * Get an R1C1-style string representation of a reference object.
390
- * ```js
391
- * stringifyR1C1Ref({
392
- * context: [ 'Sheet1' ],
393
- * range: {
394
- * r0: 9,
395
- * c0: 8,
396
- * r1: 9,
397
- * c1: 8,
398
- * $c0: true,
399
- * $c1: true
400
- * $r0: false,
401
- * $r1: false
402
- * }
403
- * });
404
- * // => 'Sheet1!R[9]C9:R[9]C9'
405
- * ```
406
- *
407
- * @param refObject A reference object
408
- * @param [options={}] Options
409
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
410
- * @returns The reference in R1C1-style string format
411
- */
412
- export declare function stringifyR1C1Ref(refObject: ReferenceR1C1, options?: {
413
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
414
- xlsx?: boolean;
415
- }): string;
416
-
417
- /**
418
- * Get a string representation of a structured reference object.
419
- * ```js
420
- * stringifyStructRef({
421
- * context: [ 'workbook.xlsx' ],
422
- * sections: [ 'data' ],
423
- * columns: [ 'my column', '@foo' ],
424
- * table: 'tableName',
425
- * });
426
- * // => 'workbook.xlsx!tableName[[#Data],[Column1]:[Column2]]'
427
- * ```
428
- *
429
- * @param refObject A structured reference object
430
- * @param [options={}] Options
431
- * @param [options.thisRow=false] Enforces using the `[#This Row]` instead of the `@` shorthand when serializing structured ranges.
432
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
433
- * @returns The structured reference in string format
434
- */
435
- export declare function stringifyStructRef(refObject: ReferenceStruct, options?: {
436
- /** Enforces using the `[#This Row]` instead of the `@` shorthand when serializing structured ranges. */
437
- thisRow?: boolean;
438
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
439
- xlsx?: boolean;
440
- }): string;
441
-
442
- /**
443
- * Convert a 0 based offset number to a column string
444
- * representation (`2` = `"C"`).
445
- * The method expects a number between 0 and 16383. Other input will
446
- * return garbage.
447
- *
448
- * @param columnIndex Zero based column index number
449
- * @returns The column string identifier
450
- */
451
- export declare function toCol(columnIndex: number): string;
452
-
453
- /**
454
- * Breaks a string formula into a list of tokens.
455
- * The returned output will be an array of objects representing the tokens:
456
- * ```js
457
- * [
458
- * { type: FX_PREFIX, value: '=' },
459
- * { type: FUNCTION, value: 'SUM' },
460
- * { type: OPERATOR, value: '(' },
461
- * { type: REF_RANGE, value: 'A1:B2' },
462
- * { type: OPERATOR, value: ')' }
463
- * ]
464
- * ```
465
- * Token types may be found as an Object as the
466
- * [`tokenTypes` export]{@link tokenTypes} on the package
467
- * (`import {tokenTypes} from '@borgar/fx';`).
468
- * To support syntax highlighting as you type, `STRING` tokens are allowed to be
469
- * "unterminated". For example, the incomplete formula `="Hello world` would be
470
- * tokenized as:
471
- * ```js
472
- * [
473
- * { type: FX_PREFIX, value: '=' },
474
- * { type: STRING, value: '"Hello world', unterminated: true },
475
- * ]
476
- * ```
477
- *
478
- * @param formula An Excel formula string (an Excel expression) or an array of tokens.
479
- * @param [options={}] Options
480
- * @param [options.allowTernary=false] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
481
- * @param [options.mergeRefs=true] Should ranges be returned as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`). This is the same as calling [`mergeRefTokens`](#mergeRefTokens)
482
- * @param [options.negativeNumbers=true] Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree).
483
- * @param [options.r1c1=false] Ranges are expected to be in the R1C1 style format rather than the more popular A1 style.
484
- * @param [options.withLocation=true] Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }`
485
- * @param [options.xlsx=false] Enables a `[1]Sheet1!A1` or `[1]!name` syntax form for external workbooks found only in XLSX files.
486
- * @returns An AST of nodes
487
- */
488
- export declare function tokenize(formula: string, options?: {
489
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
490
- allowTernary?: boolean;
491
- /** Should ranges be returned as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`). This is the same as calling [`mergeRefTokens`](#mergeRefTokens) */
492
- mergeRefs?: boolean;
493
- /** Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree). */
494
- negativeNumbers?: boolean;
495
- /** Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. */
496
- r1c1?: boolean;
497
- /** Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` */
498
- withLocation?: boolean;
499
- /** Enables a `[1]Sheet1!A1` or `[1]!name` syntax form for external workbooks found only in XLSX files. */
500
- xlsx?: boolean;
501
- }): Array<Token>;
502
-
503
- /**
504
- * Translates ranges in a formula or list of tokens from relative R1C1 syntax to
505
- * absolute A1 syntax.
506
- * Returns the same formula with the ranges translated. If an array of tokens
507
- * was supplied, then the same array is returned.
508
- * ```js
509
- * translateToA1("=SUM(RC[1],R2C5,Sheet!R3C5)", "D10");
510
- * // => "=SUM(E10,$E$2,Sheet!$E$3)");
511
- * ```
512
- * If an input range is -1,-1 relative rows/columns and the anchor is A1, the
513
- * resulting range will (by default) wrap around to the bottom of the sheet
514
- * resulting in the range XFD1048576. This may not be what you want so may set
515
- * `wrapEdges` to false which will instead turn the range into a `#REF!` error.
516
- * ```js
517
- * translateToA1("=R[-1]C[-1]", "A1");
518
- * // => "=XFD1048576");
519
- * translateToA1("=R[-1]C[-1]", "A1", { wrapEdges: false });
520
- * // => "=#REF!");
521
- * ```
522
- * Note that if you are passing in a list of tokens that was not created using
523
- * `mergeRefs` and you disable edge wrapping (or you simply set both options
524
- * to false), you can end up with a formula such as `=#REF!:B2` or
525
- * `=Sheet3!#REF!:F3`. These are valid formulas in the Excel formula language
526
- * and Excel will accept them, but they are not supported in Google Sheets.
527
- *
528
- * @param formula A string (an Excel formula) or a token list that should be adjusted.
529
- * @param anchorCell A simple string reference to an A1 cell ID (`AF123` or`$C$5`).
530
- * @param [options={}] The options
531
- * @param [options.allowTernary=true] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
532
- * @param [options.mergeRefs=true] Should ranges be treated as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`).
533
- * @param [options.wrapEdges=true] Wrap out-of-bounds ranges around sheet edges rather than turning them to #REF! errors
534
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
535
- * @returns A formula string or token list (depending on which was input)
536
- */
537
- export declare function translateToA1(formula: (string | Array<Token>), anchorCell: string, options?: {
538
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
539
- allowTernary?: boolean;
540
- /** Should ranges be treated as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`). */
541
- mergeRefs?: boolean;
542
- /** Wrap out-of-bounds ranges around sheet edges rather than turning them to #REF! errors */
543
- wrapEdges?: boolean;
544
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
545
- xlsx?: boolean;
546
- }): (string | Array<Token>);
547
-
548
- /**
549
- * Translates ranges in a formula or list of tokens from absolute A1 syntax to
550
- * relative R1C1 syntax.
551
- * Returns the same formula with the ranges translated. If an array of tokens
552
- * was supplied, then the same array is returned.
553
- * ```js
554
- * translateToR1C1("=SUM(E10,$E$2,Sheet!$E$3)", "D10");
555
- * // => "=SUM(RC[1],R2C5,Sheet!R3C5)");
556
- * ```
557
- *
558
- * @param formula A string (an Excel formula) or a token list that should be adjusted.
559
- * @param anchorCell A simple string reference to an A1 cell ID (`AF123` or`$C$5`).
560
- * @param [options={}] The options
561
- * @param [options.allowTernary=true] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
562
- * @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
563
- * @returns A formula string or token list (depending on which was input)
564
- */
565
- export declare function translateToR1C1(formula: (string | Array<Token>), anchorCell: string, options?: {
566
- /** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
567
- allowTernary?: boolean;
568
- /** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
569
- xlsx?: boolean;
570
- }): (string | Array<Token>);
571
-
572
- /** A dictionary of the types used to identify AST node variants. */
573
- export declare const nodeTypes: Readonly<{
574
- /** An array expression (`{1,2;3,4}`) */
575
- ARRAY: string;
576
- /** A binary operation (`10+10`) */
577
- BINARY: string;
578
- /** A function call expression (`SUM(1,2)`) */
579
- CALL: string;
580
- /** An error literal (`#VALUE!`) */
581
- ERROR: string;
582
- /** A function name identifier (`SUM`) */
583
- IDENTIFIER: string;
584
- /** A literal (number, string, or boolean) (`123`, `"foo"`, `false`) */
585
- LITERAL: string;
586
- /** A range identifier (`A1`) */
587
- REFERENCE: string;
588
- /** A unary operation (`10%`) */
589
- UNARY: string;
590
- }>;
591
-
592
- /** A dictionary of the types used to identify token variants. */
593
- export declare const tokenTypes: Readonly<{
594
- /** Boolean literal (`TRUE`) */
595
- BOOLEAN: string;
596
- /** Reference context ([Workbook.xlsx]Sheet1) */
597
- CONTEXT: string;
598
- /** Quoted reference context (`'[My workbook.xlsx]Sheet1'`) */
599
- CONTEXT_QUOTE: string;
600
- /** Error literal (`#VALUE!`) */
601
- ERROR: string;
602
- /** Function name (`SUM`) */
603
- FUNCTION: string;
604
- /** A leading equals sign at the start of a formula (`=`) */
605
- FX_PREFIX: string;
606
- /** Newline character (`\n`) */
607
- NEWLINE: string;
608
- /** Number literal (`123.4`, `-1.5e+2`) */
609
- NUMBER: string;
610
- /** Newline (`\n`) */
611
- OPERATOR: string;
612
- /** A range "beam" identifier (`A:A` or `1:1`) */
613
- REF_BEAM: string;
614
- /** A name / named range identifier (`income`) */
615
- REF_NAMED: string;
616
- /** A range identifier (`A1`) */
617
- REF_RANGE: string;
618
- /** A structured reference identifier (`table[[Column1]:[Column2]]`) */
619
- REF_STRUCT: string;
620
- /** A ternary range identifier (`B2:B`) */
621
- REF_TERNARY: string;
622
- /** String literal (`"Lorem ipsum"`) */
623
- STRING: string;
624
- /** Any unidentifiable range of characters. */
625
- UNKNOWN: string;
626
- /** Whitespace character sequence (` `) */
627
- WHITESPACE: string;
628
- }>;
629
-
630
- export declare type AstExpression = (ReferenceIdentifier | Literal | ErrorLiteral | UnaryExpression | BinaryExpression | CallExpression | MatrixExpression | LambdaExpression | LetExpression);
631
-
632
- export declare type BinaryExpression = {
633
- arguments: Array<AstExpression>;
634
- loc?: SourceLocation;
635
- operator: ("=" | "<" | ">" | "<=" | ">=" | "<>" | "-" | "+" | "*" | "/" | "^" | ":" | " " | "," | "&");
636
- type: "BinaryExpression";
637
- };
638
-
639
- export declare type CallExpression = {
640
- arguments: Array<AstExpression>;
641
- callee: Identifier;
642
- loc?: SourceLocation;
643
- type: "CallExpression";
644
- };
645
-
646
- export declare type ErrorLiteral = {
647
- loc?: SourceLocation;
648
- raw: string;
649
- type: "ErrorLiteral";
650
- value: string;
651
- };
652
-
653
- export declare type Identifier = {
654
- loc?: SourceLocation;
655
- name: string;
656
- type: "Identifier";
657
- };
658
-
659
- export declare type LambdaExpression = {
660
- body: (null | AstExpression);
661
- loc?: SourceLocation;
662
- params: Array<Identifier>;
663
- type: "LambdaExpression";
664
- };
665
-
666
- export declare type LetDeclarator = {
667
- id: Identifier;
668
- init: (null | AstExpression);
669
- loc?: SourceLocation;
670
- type: "LetDeclarator";
671
- };
672
-
673
- export declare type LetExpression = {
674
- body: (null | AstExpression);
675
- declarations: Array<LetDeclarator>;
676
- loc?: SourceLocation;
677
- type: "LetExpression";
678
- };
679
-
680
- export declare type Literal = {
681
- loc?: SourceLocation;
682
- raw: string;
683
- type: "Literal";
684
- value: (string | number | boolean);
685
- };
686
-
687
- export declare type MatrixExpression = {
688
- arguments: Array<Array<(ReferenceIdentifier | Literal | ErrorLiteral | CallExpression)>>;
689
- loc?: SourceLocation;
690
- type: "ArrayExpression";
691
- };
692
-
693
- /** A range in A1 style coordinates. */
694
- export declare type RangeA1 = {
695
- /** Signifies that bottom is a "locked" value */
696
- $bottom?: (boolean | null);
697
- /** Signifies that left is a "locked" value */
698
- $left?: (boolean | null);
699
- /** Signifies that right is a "locked" value */
700
- $right?: (boolean | null);
701
- /** Signifies that top is a "locked" value */
702
- $top?: (boolean | null);
703
- /** Bottom row of the range */
704
- bottom?: (number | null);
705
- /** Left column of the range */
706
- left?: (number | null);
707
- /** Right column of the range */
708
- right?: (number | null);
709
- /** Top row of the range */
710
- top?: (number | null);
711
- /** Should empty rows and columns at the top/left or bottom/right be discarded when range is read? */
712
- trim?: ("head" | "tail" | "both");
713
- };
714
-
715
- /** A range in R1C1 style coordinates. */
716
- export declare type RangeR1C1 = {
717
- /** Signifies that c0 is an absolute value */
718
- $c0?: (boolean | null);
719
- /** Signifies that c1 is an absolute value */
720
- $c1?: (boolean | null);
721
- /** Signifies that r0 is an absolute value */
722
- $r0?: (boolean | null);
723
- /** Signifies that r1 is an absolute value */
724
- $r1?: (boolean | null);
725
- /** Left column of the range */
726
- c0?: (number | null);
727
- /** Right column of the range */
728
- c1?: (number | null);
729
- /** Top row of the range */
730
- r0?: (number | null);
731
- /** Bottom row of the range */
732
- r1?: (number | null);
733
- };
734
-
735
- /**
736
- * A reference containing an A1 style range. See [Prefixes.md] for
737
- * documentation on how scopes work in Fx.
738
- */
739
- export declare type ReferenceA1 = {
740
- /** A collection of scopes for the reference */
741
- context?: Array<string>;
742
- /** The reference's range */
743
- range?: RangeA1;
744
- /** A context sheet scope */
745
- sheetName?: string;
746
- /** A context workbook scope */
747
- workbookName?: string;
748
- };
749
-
750
- export declare type ReferenceIdentifier = {
751
- kind: ("name" | "range" | "beam" | "table");
752
- loc?: SourceLocation;
753
- type: "ReferenceIdentifier";
754
- value: string;
755
- };
756
-
757
- /**
758
- * A reference containing a R1C1 style range. See [Prefixes.md] for
759
- * documentation on how scopes work in Fx.
760
- */
761
- export declare type ReferenceR1C1 = {
762
- /** A collection of scopes for the reference */
763
- context?: Array<string>;
764
- /** The reference's range */
765
- range?: RangeR1C1;
766
- /** A context sheet scope */
767
- sheetName?: string;
768
- /** A context workbook scope */
769
- workbookName?: string;
770
- };
771
-
772
- /**
773
- * A reference containing a table slice definition. See [Prefixes.md] for
774
- * documentation on how scopes work in Fx.
775
- */
776
- export declare type ReferenceStruct = {
777
- /** The sections this reference targets */
778
- columns?: Array<string>;
779
- /** A collection of scopes for the reference */
780
- context?: Array<string>;
781
- /** The sections this reference targets */
782
- sections?: Array<string>;
783
- /** A context sheet scope */
784
- sheetName?: string;
785
- /** The table this reference targets */
786
- table?: string;
787
- /** A context workbook scope */
788
- workbookName?: string;
789
- };
790
-
791
- export declare type SourceLocation = Array<number>;
792
-
793
- /** A formula language token. */
794
- export declare type Token = Record<string,any> & {
795
- /** Source position offsets to the token */
796
- loc?: Array<number>;
797
- /** The type of the token */
798
- type: string;
799
- /** Signifies an unterminated string token */
800
- unterminated?: boolean;
801
- /** The value of the token */
802
- value: string;
803
- };
804
-
805
- /** A token with extra meta data. */
806
- export declare type TokenEnhanced = Token & {
807
- /** This token's level of nesting inside parentheses */
808
- depth?: number;
809
- /** Token is of unknown type or a paren without a match */
810
- error?: boolean;
811
- /** The ID of a group which this token belongs (e.g. matching parens) */
812
- groupId?: string;
813
- /** A zero based position in a token list */
814
- index: number;
815
- };
816
-
817
- export declare type UnaryExpression = {
818
- arguments: Array<AstExpression>;
819
- loc?: SourceLocation;
820
- operator: ("+" | "-" | "%" | "#" | "@");
821
- type: "UnaryExpression";
822
- };
823
-