@borgar/fx 4.2.0 → 4.3.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/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 (be careful that `mergeRefs`
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, { allowTernary: true });
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 (be careful that `mergeRefs`
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, { allowTernary: true });
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.2.0",
3
+ "version": "4.3.1",
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 '# _fx_ API\n'>docs/API.md; jsdoc -c .jsdoc/config.json -d console lib>>docs/API.md",
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": {