@borgar/fx 4.1.0 → 4.3.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.
- package/.jsdoc/publish.js +30 -8
- package/dist/fx.js +1 -1
- package/docs/API.md +74 -24
- package/docs/Prefixes.md +82 -0
- package/lib/a1.js +11 -5
- package/lib/a1.spec.js +143 -7
- package/lib/addTokenMeta.js +54 -17
- package/lib/addTokenMeta.spec.js +10 -1
- package/lib/fixRanges.js +15 -8
- package/lib/fixRanges.spec.js +19 -0
- package/lib/lexer-srefs.spec.js +9 -1
- package/lib/lexer.js +1 -0
- package/lib/lexer.spec.js +72 -0
- package/lib/lexerParts.js +33 -6
- package/lib/parseRef.js +77 -15
- package/lib/parseRef.spec.js +60 -0
- package/lib/parser.js +3 -2
- package/lib/parser.spec.js +11 -0
- package/lib/rc.js +11 -5
- package/lib/rc.spec.js +145 -12
- package/lib/sr.js +24 -10
- package/lib/sr.spec.js +93 -7
- package/lib/stringifyPrefix.js +18 -0
- package/lib/translate-toA1.spec.js +23 -3
- package/lib/translate-toRC.spec.js +31 -2
- package/lib/translate.js +16 -11
- package/package.json +2 -2
|
@@ -2,7 +2,7 @@ import { test, Test } from 'tape';
|
|
|
2
2
|
import { translateToR1C1 } from './translate.js';
|
|
3
3
|
import { tokenize } from './lexer.js';
|
|
4
4
|
import { addTokenMeta } from './addTokenMeta.js';
|
|
5
|
-
import { FUNCTION, FX_PREFIX, OPERATOR, REF_RANGE, REF_BEAM } from './constants.js';
|
|
5
|
+
import { FUNCTION, FX_PREFIX, OPERATOR, REF_RANGE, REF_BEAM, REF_STRUCT } from './constants.js';
|
|
6
6
|
|
|
7
7
|
Test.prototype.isA2R = function isTokens (expr, anchor, result) {
|
|
8
8
|
this.is(translateToR1C1(expr, anchor), result, expr);
|
|
@@ -115,9 +115,17 @@ test('translate mixed rel/abs coords from A1 to RC', t => {
|
|
|
115
115
|
t.end();
|
|
116
116
|
});
|
|
117
117
|
|
|
118
|
-
test('translate involved
|
|
118
|
+
test('translate involved cases from A1 to RC', t => {
|
|
119
119
|
t.isA2R('=SUM(IF(E10,$E$2,$E$3),Sheet1!$2:$2*Sheet2!B:B)', 'D10',
|
|
120
120
|
'=SUM(IF(RC[1],R2C5,R3C5),Sheet1!R2*Sheet2!C[-2])');
|
|
121
|
+
|
|
122
|
+
// make sure we don't get confused by structured, or named refs
|
|
123
|
+
t.isA2R('=A1+Table1[#Data]', 'D10', '=R[-9]C[-3]+Table1[#Data]');
|
|
124
|
+
t.isA2R('=A1+foobar', 'D10', '=R[-9]C[-3]+foobar');
|
|
125
|
+
|
|
126
|
+
// This [123]Sheet!A1 variant of the syntax is used internally in xlsx files
|
|
127
|
+
t.isA2R('=[2]Sheet1!A1', 'D10', '=[2]Sheet1!R[-9]C[-3]');
|
|
128
|
+
|
|
121
129
|
t.end();
|
|
122
130
|
});
|
|
123
131
|
|
|
@@ -150,3 +158,24 @@ test('translate works with merged ranges', t => {
|
|
|
150
158
|
t.deepEqual(translateToR1C1(tokens, 'D10'), expected, expr);
|
|
151
159
|
t.end();
|
|
152
160
|
});
|
|
161
|
+
|
|
162
|
+
test('translate works with xlsx mode', t => {
|
|
163
|
+
const testExpr = (expr, anchor, expected) => {
|
|
164
|
+
const opts = { mergeRefs: true, xlsx: true, r1c1: false };
|
|
165
|
+
const tokens = tokenize(expr, opts);
|
|
166
|
+
t.deepEqual(translateToR1C1(tokens, anchor, opts), expected, expr);
|
|
167
|
+
};
|
|
168
|
+
testExpr("'[My Fancy Workbook.xlsx]'!B$1", 'B2', [
|
|
169
|
+
{ type: REF_RANGE, value: "'[My Fancy Workbook.xlsx]'!R1C" }
|
|
170
|
+
]);
|
|
171
|
+
testExpr('[Workbook.xlsx]!B$1', 'B2', [
|
|
172
|
+
{ type: REF_RANGE, value: '[Workbook.xlsx]!R1C' }
|
|
173
|
+
]);
|
|
174
|
+
testExpr('[Workbook.xlsx]Sheet1!B$1', 'B2', [
|
|
175
|
+
{ type: REF_RANGE, value: '[Workbook.xlsx]Sheet1!R1C' }
|
|
176
|
+
]);
|
|
177
|
+
testExpr('[Workbook.xlsx]!table[#data]', 'B2', [
|
|
178
|
+
{ type: REF_STRUCT, value: '[Workbook.xlsx]!table[#data]' }
|
|
179
|
+
]);
|
|
180
|
+
t.end();
|
|
181
|
+
});
|
package/lib/translate.js
CHANGED
|
@@ -23,8 +23,7 @@ const settings = {
|
|
|
23
23
|
* relative R1C1 syntax.
|
|
24
24
|
*
|
|
25
25
|
* Returns the same formula with the ranges translated. If an array of tokens
|
|
26
|
-
* was supplied, then the same array is returned
|
|
27
|
-
* *must* be `false`).
|
|
26
|
+
* was supplied, then the same array is returned.
|
|
28
27
|
*
|
|
29
28
|
* ```js
|
|
30
29
|
* translateToR1C1("=SUM(E10,$E$2,Sheet!$E$3)", "D10");
|
|
@@ -33,21 +32,24 @@ const settings = {
|
|
|
33
32
|
*
|
|
34
33
|
* @param {(string | Array<Object>)} formula A string (an Excel formula) or a token list that should be adjusted.
|
|
35
34
|
* @param {string} anchorCell A simple string reference to an A1 cell ID (`AF123` or`$C$5`).
|
|
35
|
+
* @param {Object} [options={}] The options
|
|
36
|
+
* @param {boolean} [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
|
|
36
37
|
* @return {(string | Array<Object>)} A formula string or token list (depending on which was input)
|
|
37
38
|
*/
|
|
38
|
-
export function translateToR1C1 (fx, anchorCell) {
|
|
39
|
+
export function translateToR1C1 (fx, anchorCell, { xlsx = false } = {}) {
|
|
39
40
|
const { top, left } = fromA1(anchorCell);
|
|
40
41
|
const isString = typeof fx === 'string';
|
|
41
42
|
|
|
42
43
|
const tokens = isString
|
|
43
|
-
? tokenize(fx, settings)
|
|
44
|
+
? tokenize(fx, { ...settings, xlsx })
|
|
44
45
|
: fx;
|
|
45
46
|
|
|
46
47
|
let offsetSkew = 0;
|
|
48
|
+
const refOpts = { xlsx, allowTernary: true };
|
|
47
49
|
tokens.forEach(token => {
|
|
48
50
|
if (isRange(token)) {
|
|
49
51
|
const tokenValue = token.value;
|
|
50
|
-
const ref = parseA1Ref(tokenValue,
|
|
52
|
+
const ref = parseA1Ref(tokenValue, refOpts);
|
|
51
53
|
const d = ref.range;
|
|
52
54
|
const range = {};
|
|
53
55
|
range.r0 = calc(d.$top, d.top, top);
|
|
@@ -59,7 +61,7 @@ export function translateToR1C1 (fx, anchorCell) {
|
|
|
59
61
|
range.$c0 = d.$left;
|
|
60
62
|
range.$c1 = d.$right;
|
|
61
63
|
ref.range = range;
|
|
62
|
-
token.value = stringifyR1C1Ref(ref);
|
|
64
|
+
token.value = stringifyR1C1Ref(ref, refOpts);
|
|
63
65
|
// if token includes offsets, those offsets are now likely wrong!
|
|
64
66
|
if (token.loc) {
|
|
65
67
|
token.loc[0] += offsetSkew;
|
|
@@ -105,7 +107,8 @@ function toFixed (val, abs, base, max, wrapEdges = true) {
|
|
|
105
107
|
|
|
106
108
|
const defaultOptions = {
|
|
107
109
|
wrapEdges: true,
|
|
108
|
-
mergeRefs: true
|
|
110
|
+
mergeRefs: true,
|
|
111
|
+
xlsx: false
|
|
109
112
|
};
|
|
110
113
|
|
|
111
114
|
/**
|
|
@@ -113,8 +116,7 @@ const defaultOptions = {
|
|
|
113
116
|
* absolute A1 syntax.
|
|
114
117
|
*
|
|
115
118
|
* Returns the same formula with the ranges translated. If an array of tokens
|
|
116
|
-
* was supplied, then the same array is returned
|
|
117
|
-
* *must* be `false`).
|
|
119
|
+
* was supplied, then the same array is returned.
|
|
118
120
|
*
|
|
119
121
|
* ```js
|
|
120
122
|
* translateToA1("=SUM(RC[1],R2C5,Sheet!R3C5)", "D10");
|
|
@@ -145,6 +147,7 @@ const defaultOptions = {
|
|
|
145
147
|
* @param {Object} [options={}] The options
|
|
146
148
|
* @param {boolean} [options.wrapEdges=true] Wrap out-of-bounds ranges around sheet edges rather than turning them to #REF! errors
|
|
147
149
|
* @param {boolean} [options.mergeRefs=true] Should ranges be treated as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`).
|
|
150
|
+
* @param {boolean} [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
|
|
148
151
|
* @return {(string | Array<Object>)} A formula string or token list (depending on which was input)
|
|
149
152
|
*/
|
|
150
153
|
export function translateToA1 (formula, anchorCell, options = defaultOptions) {
|
|
@@ -156,16 +159,18 @@ export function translateToA1 (formula, anchorCell, options = defaultOptions) {
|
|
|
156
159
|
? tokenize(formula, {
|
|
157
160
|
withLocation: false,
|
|
158
161
|
mergeRefs: opts.mergeRefs,
|
|
162
|
+
xlsx: opts.xlsx,
|
|
159
163
|
allowTernary: true,
|
|
160
164
|
r1c1: true
|
|
161
165
|
})
|
|
162
166
|
: formula;
|
|
163
167
|
|
|
164
168
|
let offsetSkew = 0;
|
|
169
|
+
const refOpts = { xlsx: opts.xlsx, allowTernary: true };
|
|
165
170
|
tokens.forEach(token => {
|
|
166
171
|
if (isRange(token)) {
|
|
167
172
|
const tokenValue = token.value;
|
|
168
|
-
const ref = parseR1C1Ref(tokenValue,
|
|
173
|
+
const ref = parseR1C1Ref(tokenValue, refOpts);
|
|
169
174
|
const d = ref.range;
|
|
170
175
|
const range = {};
|
|
171
176
|
const r0 = toFixed(d.r0, d.$r0, anchor.top, MAX_ROWS, opts.wrapEdges);
|
|
@@ -204,7 +209,7 @@ export function translateToA1 (formula, anchorCell, options = defaultOptions) {
|
|
|
204
209
|
}
|
|
205
210
|
else {
|
|
206
211
|
ref.range = range;
|
|
207
|
-
token.value = stringifyA1Ref(ref);
|
|
212
|
+
token.value = stringifyA1Ref(ref, refOpts);
|
|
208
213
|
}
|
|
209
214
|
// if token includes offsets, those offsets are now likely wrong!
|
|
210
215
|
if (token.loc) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@borgar/fx",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Utilities for working with Excel formulas",
|
|
5
5
|
"main": "dist/fx.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"version": "npm run build",
|
|
16
16
|
"lint": "eslint lib/*.js",
|
|
17
17
|
"test": "tape lib/*.spec.js | tap-min",
|
|
18
|
-
"build:docs": "echo '#
|
|
18
|
+
"build:docs": "echo '# _Fx_ API\n'>docs/API.md; jsdoc -c .jsdoc/config.json -d console lib>>docs/API.md",
|
|
19
19
|
"build": "NODE_ENV=production rollup -c"
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|