@borgar/fx 3.0.0 → 4.0.0-rc.1

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.
package/docs/API.md ADDED
@@ -0,0 +1,708 @@
1
+ # @borgar/fx API
2
+
3
+ ## Constants
4
+
5
+ ### <a name="nodeTypes" href="#nodeTypes">#</a> nodeTypes ⇒ `Object.<string>`
6
+
7
+ A dictionary of the types used to identify AST node variants.
8
+
9
+ **See also:** [parse](#parse).
10
+
11
+ #### Properties
12
+
13
+ | Name | Type | Description |
14
+ | ---- | ---- | ----------- |
15
+ | UNARY | `string` | A unary operation (`10%`) |
16
+ | BINARY | `string` | A binary operation (`10+10`) |
17
+ | REFERENCE | `string` | A range identifier (`A1`) |
18
+ | LITERAL | `string` | A literal (number, string, or boolean) (`123`, `"foo"`, `false`) |
19
+ | ERROR | `string` | An error literal (`#VALUE!`) |
20
+ | CALL | `string` | A function call expression (`SUM(1,2)`) |
21
+ | ARRAY | `string` | An array expression (`{1,2;3,4}`) |
22
+ | IDENTIFIER | `string` | A function name identifier (`SUM`) |
23
+
24
+ ---
25
+
26
+ ### <a name="tokenTypes" href="#tokenTypes">#</a> tokenTypes ⇒ `Object.<string>`
27
+
28
+ A dictionary of the types used to identify token variants.
29
+
30
+ **See also:** [tokenize](#tokenize).
31
+
32
+ #### Properties
33
+
34
+ | Name | Type | Description |
35
+ | ---- | ---- | ----------- |
36
+ | OPERATOR | `string` | Newline (`\n`) |
37
+ | BOOLEAN | `string` | Boolean literal (`TRUE`) |
38
+ | ERROR | `string` | Error literal (`#VALUE!`) |
39
+ | NUMBER | `string` | Number literal (`123.4`, `-1.5e+2`) |
40
+ | FUNCTION | `string` | Function name (`SUM`) |
41
+ | NEWLINE | `string` | Newline character (`\n`) |
42
+ | WHITESPACE | `string` | Whitespace character sequence (` `) |
43
+ | STRING | `string` | String literal (`"Lorem ipsum"`) |
44
+ | CONTEXT | `string` | Reference context ([Workbook.xlsx]Sheet1) |
45
+ | CONTEXT_QUOTE | `string` | Quoted reference context (`'[My workbook.xlsx]Sheet1'`) |
46
+ | REF_RANGE | `string` | A range identifier (`A1`) |
47
+ | REF_BEAM | `string` | A range "beam" identifier (`A:A` or `1:1`) |
48
+ | REF_TERNARY | `string` | A ternary range identifier (`B2:B`) |
49
+ | REF_NAMED | `string` | A name / named range identifier (`income`) |
50
+ | REF_STRUCT | `string` | A structured reference identifier (`table[[Column1]:[Column2]]`) |
51
+ | FX_PREFIX | `string` | A leading equals sign at the start of a formula (`=`) |
52
+ | UNKNOWN | `string` | Any unidentifiable range of characters. |
53
+
54
+ ## Functions
55
+
56
+ ### <a name="addA1RangeBounds" href="#addA1RangeBounds">#</a> addA1RangeBounds( range ) ⇒ `Object`
57
+
58
+ Fill the any missing bounds in range objects. Top will be set to 0, bottom to 1048575, left to 0, and right to 16383, if they are `null` or `undefined`.
59
+
60
+ ```js
61
+ addA1RangeBounds({
62
+ context: [ 'Sheet1' ],
63
+ range: {
64
+ top: 0,
65
+ left: 0,
66
+ bottom: 1,
67
+ $top: true,
68
+ $left: false,
69
+ $bottom: false,
70
+ }
71
+ });
72
+ // => {
73
+ // context: [ 'Sheet1' ],
74
+ // range: {
75
+ // top: 0,
76
+ // left: 0,
77
+ // bottom: 1,
78
+ // right: 16383,
79
+ // $top: true,
80
+ // $left: false,
81
+ // $bottom: false,
82
+ // $right: false
83
+ // }
84
+ // }
85
+ ```
86
+
87
+ #### Parameters
88
+
89
+ | Name | Type | Description |
90
+ | ---- | ---- | ----------- |
91
+ | range | `Object` | The range part of a reference object. |
92
+
93
+ #### Returns
94
+
95
+ `Object` – same range with missing bounds filled in.
96
+
97
+ ---
98
+
99
+ ### <a name="addTokenMeta" href="#addTokenMeta">#</a> addTokenMeta( tokenlist, _[context = `{}`]_ ) ⇒ `Array.<Object>`
100
+
101
+ Runs through a list of tokens and adds extra attributes such as matching parens and ranges.
102
+
103
+ The `context` parameter defines default reference attributes: `{ workbookName: 'report.xlsx', sheetName: 'Sheet1' }`. If supplied, these are used to match `A1` to `Sheet1!A1`.
104
+
105
+ All tokens will be tagged with a `.depth` number value to indicating the level of nesting in parentheses as well as an `.index` number indicating their zero based position in the list.
106
+
107
+ The returned output will be the same array of tokens but the following properties will added to tokens (as applicable):
108
+
109
+ #### Parentheses ( )
110
+
111
+ Matching parens will be tagged with `.groupId` string identifier as well as a `.depth` number value (indicating the level of nesting).
112
+
113
+ Closing parens without a counterpart will be tagged with `.error` (boolean true).
114
+
115
+ #### Curly brackets { }
116
+
117
+ Matching curly brackets will be tagged with `.groupId` string identifier. These may not be nested in Excel.
118
+
119
+ Closing curly brackets without a counterpart will be tagged with `.error` (boolean `true`).
120
+
121
+ #### Ranges (`REF_RANGE` or `REF_BEAM` type tokens)
122
+
123
+ All ranges will be tagged with `.groupId` string identifier regardless of the number of times they occur.
124
+
125
+ #### Tokens of type `UNKNOWN`
126
+
127
+ All will be tagged with `.error` (boolean `true`).
128
+
129
+ #### Parameters
130
+
131
+ | Name | Type | Default | Description |
132
+ | ---- | ---- | ------- | ----------- |
133
+ | tokenlist | `Array.<Object>` | | An array of tokens (from `tokenize()`) |
134
+ | _[context]_ | `Object` | `{}` | A contest used to match `A1` to `Sheet1!A1`. |
135
+ | _[context]_.sheetName | `string` | `""` | An implied sheet name ('Sheet1') |
136
+ | _[context]_.workbookName | `string` | `""` | An implied workbook name ('report.xlsx') |
137
+
138
+ #### Returns
139
+
140
+ `Array.<Object>` – The input array with the enchanced tokens
141
+
142
+ ---
143
+
144
+ ### <a name="fixRanges" href="#fixRanges">#</a> fixRanges( formula, _[options = `{}`]_ ) ⇒ `string` | `Array.<Object>`
145
+
146
+ Normalizes A1 style ranges in a formula or list of tokens so that the top and left coordinates of the range are on the left-hand side of a colon operator:
147
+
148
+ `B2:A1` → `A1:B2`
149
+ `1:A1` → `A1:1`
150
+ `A:A1` → `A1:A`
151
+ `B:A` → `A:B`
152
+ `2:1` → `1:2`
153
+ `A1:A1` → `A1`
154
+
155
+ When `{ addBounds: true }` is passed as an option, the missing bounds are also added. This can be done to ensure Excel compatible ranges. The fixes then additionally include:
156
+
157
+ `1:A1` → `A1:1` → `1:1`
158
+ `A:A1` → `A1:A` → `A:A`
159
+ `A1:A` → `A:A`
160
+ `A1:1` → `A:1`
161
+ `B2:B` → `B2:1048576`
162
+ `B2:2` → `B2:XFD2`
163
+
164
+ Returns the same formula with the ranges updated. If an array of tokens was supplied, then a new array is returned.
165
+
166
+ #### Parameters
167
+
168
+ | Name | Type | Default | Description |
169
+ | ---- | ---- | ------- | ----------- |
170
+ | formula | `string` \| `Array.<Object>` | | A string (an Excel formula) or a token list that should be adjusted. |
171
+ | _[options]_ | `Object` | `{}` | Options |
172
+ | _[options]_.addBounds | `boolean` | `false` | Fill in any undefined bounds of range objects. Top to 0, bottom to 1048575, left to 0, and right to 16383. |
173
+ | _[options]_.r1c1 | `boolean` | `false` | Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. |
174
+
175
+ #### Returns
176
+
177
+ `string` | `Array.<Object>` – A formula string or token list (depending on which was input)
178
+
179
+ ---
180
+
181
+ ### <a name="fromCol" href="#fromCol">#</a> fromCol( columnString ) ⇒ `number`
182
+
183
+ Convert a column string representation to a 0 based offset number (`"C"` = `2`).
184
+
185
+ The method expects a valid column identifier made up of _only_ A-Z letters, which may be either upper or lower case. Other input will return garbage.
186
+
187
+ #### Parameters
188
+
189
+ | Name | Type | Description |
190
+ | ---- | ---- | ----------- |
191
+ | columnString | `string` | The column string identifier |
192
+
193
+ #### Returns
194
+
195
+ `number` – Zero based column index number
196
+
197
+ ---
198
+
199
+ ### <a name="isError" href="#isError">#</a> isError( token ) ⇒ `boolean`
200
+
201
+ Determines whether the specified token is an error.
202
+
203
+ Returns `true` if the input is a token of type ERROR (`#VALUE!`). In all other cases `false` is returned.
204
+
205
+ #### Parameters
206
+
207
+ | Name | Type | Description |
208
+ | ---- | ---- | ----------- |
209
+ | token | `Object` | The token |
210
+
211
+ #### Returns
212
+
213
+ `boolean` – True if the specified token is error, False otherwise.
214
+
215
+ ---
216
+
217
+ ### <a name="isFunction" href="#isFunction">#</a> isFunction( token ) ⇒ `boolean`
218
+
219
+ Determines whether the specified token is a function.
220
+
221
+ Returns `true` if the input is a token of type FUNCTION. In all other cases `false` is returned.
222
+
223
+ #### Parameters
224
+
225
+ | Name | Type | Description |
226
+ | ---- | ---- | ----------- |
227
+ | token | `Object` | The token |
228
+
229
+ #### Returns
230
+
231
+ `boolean` – True if the specified token is function, False otherwise.
232
+
233
+ ---
234
+
235
+ ### <a name="isFxPrefix" href="#isFxPrefix">#</a> isFxPrefix( token ) ⇒ `boolean`
236
+
237
+ Returns `true` if the input is a token of type FX_PREFIX (leading `=` in formula). In all other cases `false` is returned.
238
+
239
+ #### Parameters
240
+
241
+ | Name | Type | Description |
242
+ | ---- | ---- | ----------- |
243
+ | token | `Object` | The token |
244
+
245
+ #### Returns
246
+
247
+ `boolean` – True if the specified token is effects prefix, False otherwise.
248
+
249
+ ---
250
+
251
+ ### <a name="isLiteral" href="#isLiteral">#</a> isLiteral( token ) ⇒ `boolean`
252
+
253
+ Determines whether the specified token is a literal.
254
+
255
+ Returns `true` if the input is a token of type BOOLEAN (`TRUE` or `FALSE`), ERROR (`#VALUE!`), NUMBER (123.4), or STRING (`"lorem ipsum"`). In all other cases `false` is returned.
256
+
257
+ #### Parameters
258
+
259
+ | Name | Type | Description |
260
+ | ---- | ---- | ----------- |
261
+ | token | `Object` | The token |
262
+
263
+ #### Returns
264
+
265
+ `boolean` – True if the specified token is literal, False otherwise.
266
+
267
+ ---
268
+
269
+ ### <a name="isOperator" href="#isOperator">#</a> isOperator( token ) ⇒ `boolean`
270
+
271
+ Determines whether the specified token is an operator.
272
+
273
+ Returns `true` if the input is a token of type OPERATOR (`+` or `:`). In all other cases `false` is returned.
274
+
275
+ #### Parameters
276
+
277
+ | Name | Type | Description |
278
+ | ---- | ---- | ----------- |
279
+ | token | `Object` | The token |
280
+
281
+ #### Returns
282
+
283
+ `boolean` – True if the specified token is operator, False otherwise.
284
+
285
+ ---
286
+
287
+ ### <a name="isRange" href="#isRange">#</a> isRange( token ) ⇒ `boolean`
288
+
289
+ Determines whether the specified token is a range.
290
+
291
+ Returns `true` if the input is a token that has a type of either REF_RANGE (`A1` or `A1:B2`), REF_TERNARY (`A1:A`, `A1:1`, `1:A1`, or `A:A1`), or REF_BEAM (`A:A` or `1:1`). In all other cases `false` is returned.
292
+
293
+ #### Parameters
294
+
295
+ | Name | Type | Description |
296
+ | ---- | ---- | ----------- |
297
+ | token | `Object` | A token |
298
+
299
+ #### Returns
300
+
301
+ `boolean` – True if the specified token is range, False otherwise.
302
+
303
+ ---
304
+
305
+ ### <a name="isReference" href="#isReference">#</a> isReference( token ) ⇒ `boolean`
306
+
307
+ Determines whether the specified token is a reference.
308
+
309
+ Returns `true` if the input is a token of type REF_RANGE (`A1` or `A1:B2`), REF_TERNARY (`A1:A`, `A1:1`, `1:A1`, or `A:A1`), REF_BEAM (`A:A` or `1:1`), or REF_NAMED (`myrange`). In all other cases `false` is returned.
310
+
311
+ #### Parameters
312
+
313
+ | Name | Type | Description |
314
+ | ---- | ---- | ----------- |
315
+ | token | `Object` | The token |
316
+
317
+ #### Returns
318
+
319
+ `boolean` – True if the specified token is reference, False otherwise.
320
+
321
+ ---
322
+
323
+ ### <a name="isWhitespace" href="#isWhitespace">#</a> isWhitespace( token ) ⇒ `boolean`
324
+
325
+ Determines whether the specified token is whitespace.
326
+
327
+ Returns `true` if the input is a token of type WHITESPACE (` `) or NEWLINE (`\n`). In all other cases `false` is returned.
328
+
329
+ #### Parameters
330
+
331
+ | Name | Type | Description |
332
+ | ---- | ---- | ----------- |
333
+ | token | `Object` | The token |
334
+
335
+ #### Returns
336
+
337
+ `boolean` – True if the specified token is whitespace, False otherwise.
338
+
339
+ ---
340
+
341
+ ### <a name="mergeRefTokens" href="#mergeRefTokens">#</a> mergeRefTokens( tokenlist ) ⇒ `Array`
342
+
343
+ Merges context with reference tokens as possible in a list of tokens.
344
+
345
+ When given a tokenlist, this function returns a new list with ranges returned as whole references (`Sheet1!A1:B2`) rather than separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`).
346
+
347
+ #### Parameters
348
+
349
+ | Name | Type | Description |
350
+ | ---- | ---- | ----------- |
351
+ | tokenlist | `Array.<Object>` | An array of tokens (from `tokenize()`) |
352
+
353
+ #### Returns
354
+
355
+ `Array` – A new list of tokens with range parts merged.
356
+
357
+ ---
358
+
359
+ ### <a name="parse" href="#parse">#</a> parse( formula, _[options = `{}`]_ ) ⇒ `Object`
360
+
361
+ Parses a string formula or list of tokens into an AST.
362
+
363
+ The parser requires `mergeRefs` to have been `true` in tokenlist options, because it does not recognize reference context tokens.
364
+
365
+ The AST Abstract Syntax Tree's format is documented in [AST format.md][AST format.md]
366
+
367
+ **See also:** [nodeTypes](#nodeTypes).
368
+
369
+ #### Parameters
370
+
371
+ | Name | Type | Default | Description |
372
+ | ---- | ---- | ------- | ----------- |
373
+ | formula | `string` \| `Array.<Object>` | | An Excel formula string (an Excel expression) or an array of tokens. |
374
+ | _[options]_ | `Object` | `{}` | Options |
375
+ | _[options]_.allowNamed | `boolean` | `true` | Enable parsing names as well as ranges. |
376
+ | _[options]_.allowTernary | `boolean` | `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. |
377
+ | _[options]_.negativeNumbers | `boolean` | `true` | Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree). |
378
+ | _[options]_.permitArrayRanges | `boolean` | `false` | Ranges are allowed as elements of arrays. This is a features in Google Sheets while Excel does not support it. |
379
+ | _[options]_.r1c1 | `boolean` | `false` | Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. |
380
+ | _[options]_.withLocation | `boolean` | `true` | Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` |
381
+
382
+ #### Returns
383
+
384
+ `Object` – An AST of nodes
385
+
386
+ ---
387
+
388
+ ### <a name="parseA1Ref" href="#parseA1Ref">#</a> parseA1Ref( refString, _[options = `{}`]_ ) ⇒ `Object` | `null`
389
+
390
+ Parse a string reference into an object representing it.
391
+
392
+ ```js
393
+ parseA1Ref('Sheet1!A$1:$B2');
394
+ // => {
395
+ // context: [ 'Sheet1' ],
396
+ // range: {
397
+ // top: 0,
398
+ // left: 0,
399
+ // bottom: 1,
400
+ // right: 1
401
+ // $top: true,
402
+ // $left: false,
403
+ // $bottom: false,
404
+ // $right: true
405
+ // }
406
+ // }
407
+ ```
408
+
409
+ For A:A or A1:A style ranges, `null` will be used for any dimensions that the syntax does not specify:
410
+
411
+ #### Parameters
412
+
413
+ | Name | Type | Default | Description |
414
+ | ---- | ---- | ------- | ----------- |
415
+ | refString | `string` | | An A1-style reference string |
416
+ | _[options]_ | `Object` | `{}` | Options |
417
+ | _[options]_.allowNamed | `boolean` | `true` | Enable parsing names as well as ranges. |
418
+ | _[options]_.allowTernary | `boolean` | `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. |
419
+
420
+ #### Returns
421
+
422
+ `Object` | `null` – An object representing a valid reference or null if it is invalid.
423
+
424
+ ---
425
+
426
+ ### <a name="parseR1C1Ref" href="#parseR1C1Ref">#</a> parseR1C1Ref( refString, _[options = `{}`]_ ) ⇒ `Object` | `null`
427
+
428
+ Parse a string reference into an object representing it.
429
+
430
+ ```js
431
+ parseR1C1Ref('Sheet1!R[9]C9:R[9]C9');
432
+ // => {
433
+ // context: [ 'Sheet1' ],
434
+ // range: {
435
+ // r0: 9,
436
+ // c0: 8,
437
+ // r1: 9,
438
+ // c1: 8,
439
+ // $c0: true,
440
+ // $c1: true
441
+ // $r0: false,
442
+ // $r1: false
443
+ // }
444
+ // }
445
+ ```
446
+
447
+ #### Parameters
448
+
449
+ | Name | Type | Default | Description |
450
+ | ---- | ---- | ------- | ----------- |
451
+ | refString | `string` | | An R1C1-style reference string |
452
+ | _[options]_ | `Object` | `{}` | Options |
453
+ | _[options]_.allowNamed | `boolean` | `true` | Enable parsing names as well as ranges. |
454
+ | _[options]_.allowTernary | `boolean` | `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. |
455
+
456
+ #### Returns
457
+
458
+ `Object` | `null` – An object representing a valid reference or null if it is invalid.
459
+
460
+ ---
461
+
462
+ ### <a name="parseStructRef" href="#parseStructRef">#</a> parseStructRef( ref, _[options = `{}`]_ ) ⇒ `Object` | `null`
463
+
464
+ Parse a structured reference string into an object representing it.
465
+
466
+ ```js
467
+ parseStructRef('workbook.xlsx!tableName[[#Data],[Column1]:[Column2]]');
468
+ // => {
469
+ // context: [ 'workbook.xlsx' ],
470
+ // sections: [ 'data' ],
471
+ // columns: [ 'my column', '@foo' ],
472
+ // table: 'tableName',
473
+ // }
474
+ ```
475
+
476
+ For A:A or A1:A style ranges, `null` will be used for any dimensions that the syntax does not specify:
477
+
478
+ #### Parameters
479
+
480
+ | Name | Type | Default | Description |
481
+ | ---- | ---- | ------- | ----------- |
482
+ | ref | `string` | | A structured reference string |
483
+ | _[options]_ | `Object` | `{}` | Options |
484
+
485
+ #### Returns
486
+
487
+ `Object` | `null` – An object representing a valid reference or null if it is invalid.
488
+
489
+ ---
490
+
491
+ ### <a name="stringifyA1Ref" href="#stringifyA1Ref">#</a> stringifyA1Ref( refObject ) ⇒ `Object`
492
+
493
+ Get an A1-style string representation of a reference object.
494
+
495
+ ```js
496
+ stringifyA1Ref({
497
+ context: [ 'Sheet1' ],
498
+ range: {
499
+ top: 0,
500
+ left: 0,
501
+ bottom: 1,
502
+ right: 1,
503
+ $top: true,
504
+ $left: false,
505
+ $bottom: false,
506
+ $right: true
507
+ }
508
+ });
509
+ // => 'Sheet1!A$1:$B2'
510
+ ```
511
+
512
+ #### Parameters
513
+
514
+ | Name | Type | Description |
515
+ | ---- | ---- | ----------- |
516
+ | refObject | `Object` | A reference object |
517
+
518
+ #### Returns
519
+
520
+ `Object` – The reference in A1-style string format
521
+
522
+ ---
523
+
524
+ ### <a name="stringifyR1C1Ref" href="#stringifyR1C1Ref">#</a> stringifyR1C1Ref( refObject ) ⇒ `Object`
525
+
526
+ Get an R1C1-style string representation of a reference object.
527
+
528
+ ```js
529
+ stringifyR1C1Ref({
530
+ context: [ 'Sheet1' ],
531
+ range: {
532
+ r0: 9,
533
+ c0: 8,
534
+ r1: 9,
535
+ c1: 8,
536
+ $c0: true,
537
+ $c1: true
538
+ $r0: false,
539
+ $r1: false
540
+ }
541
+ });
542
+ // => 'Sheet1!R[9]C9:R[9]C9'
543
+ ```
544
+
545
+ #### Parameters
546
+
547
+ | Name | Type | Description |
548
+ | ---- | ---- | ----------- |
549
+ | refObject | `Object` | A reference object |
550
+
551
+ #### Returns
552
+
553
+ `Object` – The reference in R1C1-style string format
554
+
555
+ ---
556
+
557
+ ### <a name="stringifyStructRef" href="#stringifyStructRef">#</a> stringifyStructRef( refObject ) ⇒ `Object`
558
+
559
+ Get a string representation of a structured reference object.
560
+
561
+ ```js
562
+ stringifyStructRef({
563
+ context: [ 'workbook.xlsx' ],
564
+ sections: [ 'data' ],
565
+ columns: [ 'my column', '@foo' ],
566
+ table: 'tableName',
567
+ });
568
+ // => 'workbook.xlsx!tableName[[#Data],[Column1]:[Column2]]'
569
+ ```
570
+
571
+ #### Parameters
572
+
573
+ | Name | Type | Description |
574
+ | ---- | ---- | ----------- |
575
+ | refObject | `Object` | A structured reference object |
576
+
577
+ #### Returns
578
+
579
+ `Object` – The structured reference in string format
580
+
581
+ ---
582
+
583
+ ### <a name="toCol" href="#toCol">#</a> toCol( columnIndex ) ⇒ `string`
584
+
585
+ Convert a 0 based offset number to a column string representation (`2` = `"C"`).
586
+
587
+ The method expects a number between 0 and 16383. Other input will return garbage.
588
+
589
+ #### Parameters
590
+
591
+ | Name | Type | Description |
592
+ | ---- | ---- | ----------- |
593
+ | columnIndex | `number` | Zero based column index number |
594
+
595
+ #### Returns
596
+
597
+ `string` – The column string identifier
598
+
599
+ ---
600
+
601
+ ### <a name="tokenize" href="#tokenize">#</a> tokenize( formula, _[options = `{}`]_ ) ⇒ `Array.<Object>`
602
+
603
+ Breaks a string formula into a list of tokens.
604
+
605
+ The returned output will be an array of objects representing the tokens:
606
+
607
+ ```js
608
+ [
609
+ { type: FX_PREFIX, value: '=' },
610
+ { type: FUNCTION, value: 'SUM' },
611
+ { type: OPERATOR, value: '(' },
612
+ { type: REF_RANGE, value: 'A1:B2' },
613
+ { type: OPERATOR, value: ')' }
614
+ ]
615
+ ```
616
+
617
+ Token types may be found as an Object as the [`tokenTypes` export](#tokenTypes) on the package (`import {tokenTypes} from '@borgar/fx';`).
618
+
619
+ To support syntax highlighting as you type, `STRING` tokens are allowed to be "unterminated". For example, the incomplete formula `="Hello world` would be tokenized as:
620
+
621
+ ```js
622
+ [
623
+ { type: FX_PREFIX, value: '=' },
624
+ { type: STRING, value: '"Hello world', unterminated: true },
625
+ ]
626
+ ```
627
+
628
+ **See also:** [tokenTypes](#tokenTypes).
629
+
630
+ #### Parameters
631
+
632
+ | Name | Type | Default | Description |
633
+ | ---- | ---- | ------- | ----------- |
634
+ | formula | `string` | | An Excel formula string (an Excel expression) or an array of tokens. |
635
+ | _[options]_ | `Object` | `{}` | Options |
636
+ | _[options]_.allowTernary | `boolean` | `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. |
637
+ | _[options]_.negativeNumbers | `boolean` | `true` | Merges unary minuses with their immediately following number tokens (`-`,`1`) => `-1` (alternatively these will be unary operations in the tree). |
638
+ | _[options]_.r1c1 | `boolean` | `false` | Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. |
639
+ | _[options]_.withLocation | `boolean` | `true` | Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` |
640
+ | _[options]_.mergeRefs | `boolean` | `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) |
641
+
642
+ #### Returns
643
+
644
+ `Array.<Object>` – An AST of nodes
645
+
646
+ ---
647
+
648
+ ### <a name="translateToA1" href="#translateToA1">#</a> translateToA1( formula, anchorCell, _[options = `{}`]_ ) ⇒ `string` | `Array.<Object>`
649
+
650
+ Translates ranges in a formula or list of tokens from relative R1C1 syntax to absolute A1 syntax.
651
+
652
+ Returns the same formula with the ranges translated. If an array of tokens was supplied, then the same array is returned (be careful that `mergeRefs` *must* be `false`).
653
+
654
+ ```js
655
+ translateToA1("=SUM(RC[1],R2C5,Sheet!R3C5)", "D10");
656
+ // => "=SUM(E10,$E$2,Sheet!$E$3)");
657
+ ```
658
+
659
+ If an input range is -1,-1 relative rows/columns and the anchor is A1, the resulting range will (by default) wrap around to the bottom of the sheet resulting in the range XFD1048576. This may not be what you want so may set `wrapEdges` to false which will instead turn the range into a `#REF!` error.
660
+
661
+ ```js
662
+ translateToA1("=R[-1]C[-1]", "A1");
663
+ // => "=XFD1048576");
664
+
665
+ translateToA1("=R[-1]C[-1]", "A1", { wrapEdges: false });
666
+ // => "=#REF!");
667
+ ```
668
+
669
+ Note that if you are passing in a list of tokens that was not created using `mergeRefs` and you disable edge wrapping (or you simply set both options to false), you can end up with a formula such as `=#REF!:B2` or `=Sheet3!#REF!:F3`. These are valid formulas in the Excel formula language and Excel will accept them, but they are not supported in Google Sheets.
670
+
671
+ #### Parameters
672
+
673
+ | Name | Type | Default | Description |
674
+ | ---- | ---- | ------- | ----------- |
675
+ | formula | `string` \| `Array.<Object>` | | A string (an Excel formula) or a token list that should be adjusted. |
676
+ | anchorCell | `string` | | A simple string reference to an A1 cell ID (`AF123` or`$C$5`). |
677
+ | _[options]_ | `Object` | `{}` | The options |
678
+ | _[options]_.wrapEdges | `boolean` | `true` | Wrap out-of-bounds ranges around sheet edges rather than turning them to #REF! errors |
679
+ | _[options]_.mergeRefs | `boolean` | `true` | Should ranges be treated as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`). |
680
+
681
+ #### Returns
682
+
683
+ `string` | `Array.<Object>` – A formula string or token list (depending on which was input)
684
+
685
+ ---
686
+
687
+ ### <a name="translateToR1C1" href="#translateToR1C1">#</a> translateToR1C1( formula, anchorCell ) ⇒ `string` | `Array.<Object>`
688
+
689
+ Translates ranges in a formula or list of tokens from absolute A1 syntax to relative R1C1 syntax.
690
+
691
+ Returns the same formula with the ranges translated. If an array of tokens was supplied, then the same array is returned (be careful that `mergeRefs` *must* be `false`).
692
+
693
+ ```js
694
+ translateToR1C1("=SUM(E10,$E$2,Sheet!$E$3)", "D10");
695
+ // => "=SUM(RC[1],R2C5,Sheet!R3C5)");
696
+ ```
697
+
698
+ #### Parameters
699
+
700
+ | Name | Type | Description |
701
+ | ---- | ---- | ----------- |
702
+ | formula | `string` \| `Array.<Object>` | A string (an Excel formula) or a token list that should be adjusted. |
703
+ | anchorCell | `string` | A simple string reference to an A1 cell ID (`AF123` or`$C$5`). |
704
+
705
+ #### Returns
706
+
707
+ `string` | `Array.<Object>` – A formula string or token list (depending on which was input)
708
+