@univerjs/engine-formula 0.6.0 → 0.6.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/es/facade.js CHANGED
@@ -25,41 +25,73 @@ let s = class extends f {
25
25
  }
26
26
  /**
27
27
  * Offsets the formula
28
- * @param {string} formulaString
29
- * @param {number} refOffsetX
30
- * @param {number} refOffsetY
31
- * @param {boolean} [ignoreAbsolute] default is false
28
+ * @param {string} formulaString - The formula string to offset
29
+ * @param {number} refOffsetX - The offset column
30
+ * @param {number} refOffsetY - The offset row
31
+ * @param {boolean} [ignoreAbsolute] - Whether to ignore the absolute reference
32
+ * @returns {string} The offset formula string
33
+ *
32
34
  * @example
33
- * const result = moveFormulaRefOffset('sum(a1,b2)',1,1)
34
- * // result is 'sum(b2,c3)'
35
+ * ```ts
36
+ * const formulaEngine = univerAPI.getFormula();
37
+ * const result = formulaEngine.moveFormulaRefOffset('=SUM(A1,B2)', 1, 1);
38
+ * console.log(result);
39
+ * ```
35
40
  */
36
41
  moveFormulaRefOffset(e, t, r, i) {
37
42
  return this._lexerTreeBuilder.moveFormulaRefOffset(e, t, r, i);
38
43
  }
39
44
  /**
40
45
  * Resolves the formula string to a 'node' node
41
- * @param {string} formulaString
42
- * @returns {*} {((string | ISequenceNode)[])}
43
- * @memberof FFormula
46
+ * @param {string} formulaString - The formula string to resolve
47
+ * @returns {Array<ISequenceNode | string>} The nodes of the formula string
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const formulaEngine = univerAPI.getFormula();
52
+ * const nodes = formulaEngine.sequenceNodesBuilder('=SUM(A1,B2)');
53
+ * console.log(nodes);
54
+ * ```
44
55
  */
45
56
  sequenceNodesBuilder(e) {
46
57
  return this._lexerTreeBuilder.sequenceNodesBuilder(e) || [];
47
58
  }
48
59
  /**
49
60
  * Start the calculation of the formula.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const formulaEngine = univerAPI.getFormula();
65
+ * formulaEngine.executeCalculation();
66
+ * ```
50
67
  */
51
68
  executeCalculation() {
52
69
  this._commandService.executeCommand(l.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
53
70
  }
54
71
  /**
55
72
  * Stop the calculation of the formula.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const formulaEngine = univerAPI.getFormula();
77
+ * formulaEngine.stopCalculation();
78
+ * ```
56
79
  */
57
80
  stopCalculation() {
58
81
  this._commandService.executeCommand(S.id, {});
59
82
  }
60
83
  /**
61
84
  * Listening calculation starts.
62
- * @param callback
85
+ * @param {Function} callback - The callback function to be called when the formula calculation starts.
86
+ * @returns {IDisposable} The disposable instance.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * const formulaEngine = univerAPI.getFormula();
91
+ * formulaEngine.calculationStart((forceCalculation) => {
92
+ * console.log('Calculation start', forceCalculation);
93
+ * });
94
+ * ```
63
95
  */
64
96
  calculationStart(e) {
65
97
  return this._commandService.onCommandExecuted((t) => {
@@ -71,7 +103,16 @@ let s = class extends f {
71
103
  }
72
104
  /**
73
105
  * Listening calculation ends.
74
- * @param callback
106
+ * @param {Function} callback - The callback function to be called when the formula calculation ends.
107
+ * @returns {IDisposable} The disposable instance.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const formulaEngine = univerAPI.getFormula();
112
+ * formulaEngine.calculationEnd((functionsExecutedState) => {
113
+ * console.log('Calculation end', functionsExecutedState);
114
+ * });
115
+ * ```
75
116
  */
76
117
  calculationEnd(e) {
77
118
  return this._commandService.onCommandExecuted((t) => {
@@ -88,6 +129,14 @@ let s = class extends f {
88
129
  * value is 30,000 milliseconds.
89
130
  * @returns {Promise<boolean>} This method returns `true` if the computing is complete. If the timeout is reached, this
90
131
  * method returns `false`.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const formulaEngine = univerAPI.getFormula();
136
+ * formulaEngine.whenComputingCompleteAsync(3000).then((isComplete) => {
137
+ * console.log('Computing complete:', isComplete);
138
+ * });
139
+ * ```
91
140
  */
92
141
  whenComputingCompleteAsync(e) {
93
142
  const t = this._injector.get(x);
@@ -111,7 +160,16 @@ let s = class extends f {
111
160
  }
112
161
  /**
113
162
  * Listening calculation processing.
114
- * @param callback
163
+ * @param {Function} callback - The callback function to be called when the formula calculation is in progress.
164
+ * @returns {IDisposable} The disposable instance.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const formulaEngine = univerAPI.getFormula();
169
+ * formulaEngine.calculationProcessing((stageInfo) => {
170
+ * console.log('Calculation processing', stageInfo);
171
+ * });
172
+ * ```
115
173
  */
116
174
  calculationProcessing(e) {
117
175
  return this._commandService.onCommandExecuted((t) => {
@@ -123,7 +181,8 @@ let s = class extends f {
123
181
  }
124
182
  /**
125
183
  * When a formula contains a circular reference, set the maximum number of iterations for the formula calculation.
126
- * @param maxIteration The maximum number of iterations. The default value is 1.
184
+ * @param {number} maxIteration The maximum number of iterations. The default value is 1.
185
+ *
127
186
  * @example
128
187
  * ```ts
129
188
  * // Set the maximum number of iterations for the formula calculation to 5.
package/lib/es/index.js CHANGED
@@ -3,7 +3,7 @@ var Hu = (i, a, e) => a in i ? Yu(i, a, { enumerable: !0, configurable: !0, writ
3
3
  var A = (i, a, e) => Hu(i, typeof a != "symbol" ? a + "" : a, e);
4
4
  import { ObjectMatrix as Ce, CommandType as Ie, RANGE_TYPE as Ze, AbsoluteRefType as Se, Tools as ze, createIdentifier as ft, Disposable as Oe, IUniverInstanceService as ra, LRUMap as Gu, hashAlgorithm as Qu, moveRangeByOffset as na, isValidRange as Wu, isFormulaString as bt, isFormulaId as Pt, Rectangle as Wa, cellToRange as qu, Inject as Ve, UniverInstanceType as Dr, LocaleService as Ku, LocaleType as Mr, numfmt as gt, BuildTextUtils as Xu, isRealNum as Q, CellValueType as yt, toDisposable as Zu, Injector as Gi, sortRules as zu, RTree as ys, IConfigService as sa, requestImmediateMacroTask as Ju, ICommandService as ar, BooleanNumber as ec, DataStreamTreeTokenType as ve, DisposableCollection as tc, Optional as rc, Plugin as nc, merge as sc, touchDependencies as ss } from "@univerjs/core";
5
5
  import { Subject as cr, BehaviorSubject as Qi, distinctUntilChanged as Wi, combineLatest as ac, map as ic, Observable as oc, shareReplay as uc } from "rxjs";
6
- import { DEFAULT_TEXT_FORMAT as cc } from "@univerjs/engine-numfmt";
6
+ import { isTextFormat as cc } from "@univerjs/engine-numfmt";
7
7
  import { DataSyncPrimaryController as lc } from "@univerjs/rpc";
8
8
  var er = /* @__PURE__ */ ((i) => (i.FALSE = "FALSE", i.TRUE = "TRUE", i))(er || {}), xt = /* @__PURE__ */ ((i) => (i[i.SUCCESS = 0] = "SUCCESS", i[i.ERROR = 1] = "ERROR", i))(xt || {}), ot = /* @__PURE__ */ ((i) => (i[i.FRONT = 0] = "FRONT", i[i.BACK = 1] = "BACK", i))(ot || {});
9
9
  function fc(i, a, e, t, r) {
@@ -355,6 +355,8 @@ let Ms = class extends Oe {
355
355
  super();
356
356
  // 18.2.6 definedNames (Defined Names)
357
357
  A(this, "_definedNameMap", {});
358
+ A(this, "_nameCacheMap", {});
359
+ // Cache for name-to-definition mapping
358
360
  A(this, "_update$", new cr());
359
361
  A(this, "update$", this._update$.asObservable());
360
362
  A(this, "_currentRange", {
@@ -374,7 +376,7 @@ let Ms = class extends Oe {
374
376
  this._univerInstanceService = a;
375
377
  }
376
378
  dispose() {
377
- this._definedNameMap = {};
379
+ this._definedNameMap = {}, this._nameCacheMap = {};
378
380
  }
379
381
  getWorksheetByRef(a, e) {
380
382
  var r;
@@ -383,7 +385,7 @@ let Ms = class extends Oe {
383
385
  }
384
386
  focusRange(a, e) {
385
387
  const t = this.getValueById(a, e);
386
- t != null && this._focusRange$.next({ ...t, unitId: a });
388
+ t !== void 0 && this._focusRange$.next({ ...t, unitId: a });
387
389
  }
388
390
  setCurrentRange(a) {
389
391
  this._currentRange = a, this._currentRange$.next(a);
@@ -395,36 +397,56 @@ let Ms = class extends Oe {
395
397
  return hr(this._currentRange.range);
396
398
  }
397
399
  registerDefinedNames(a, e) {
398
- this._definedNameMap[a] = e, this._update();
400
+ this._definedNameMap[a] = e, this._updateCache(a), this._update();
399
401
  }
400
402
  registerDefinedName(a, e) {
401
- this._definedNameMap[a] == null && (this._definedNameMap[a] = {}), this._definedNameMap[a][e.id] = e, this._update();
403
+ this._definedNameMap[a] === void 0 && (this._definedNameMap[a] = {}), this._definedNameMap[a][e.id] = e, this._updateCache(a), this._update();
402
404
  }
403
405
  removeDefinedName(a, e) {
404
406
  var t;
405
- (t = this._definedNameMap[a]) == null || delete t[e], this._update();
407
+ (t = this._definedNameMap[a]) == null || delete t[e], this._updateCache(a), this._update();
406
408
  }
407
409
  removeUnitDefinedName(a) {
408
- delete this._definedNameMap[a], this._update();
410
+ delete this._definedNameMap[a], this._updateCache(a), this._update();
409
411
  }
410
412
  getDefinedNameMap(a) {
411
413
  return this._definedNameMap[a];
412
414
  }
413
415
  getValueByName(a, e) {
414
- var r;
415
- const t = this._definedNameMap[a];
416
- return t == null ? null : (r = Array.from(Object.values(t)).filter((n) => n.name === e)) == null ? void 0 : r[0];
416
+ const t = this._nameCacheMap[a];
417
+ if (t)
418
+ return t[e] || null;
419
+ const r = this._definedNameMap[a];
420
+ if (r === void 0)
421
+ return null;
422
+ let n = null;
423
+ for (const s of Object.values(r))
424
+ if (s.name === e) {
425
+ n = s;
426
+ break;
427
+ }
428
+ return n && (this._nameCacheMap[a] = this._nameCacheMap[a] || {}, this._nameCacheMap[a][e] = n), n;
417
429
  }
418
430
  getValueById(a, e) {
419
431
  var t;
420
432
  return (t = this._definedNameMap[a]) == null ? void 0 : t[e];
421
433
  }
422
434
  hasDefinedName(a) {
423
- return this._definedNameMap[a] == null ? !1 : (Array.from(Object.values(this._definedNameMap[a])).length || 0) !== 0;
435
+ return this._definedNameMap[a] === void 0 ? !1 : (Array.from(Object.values(this._definedNameMap[a])).length || 0) !== 0;
424
436
  }
425
437
  _update() {
426
438
  this._update$.next(null);
427
439
  }
440
+ _updateCache(a) {
441
+ const e = this._definedNameMap[a];
442
+ if (e === void 0) {
443
+ delete this._nameCacheMap[a];
444
+ return;
445
+ }
446
+ this._nameCacheMap[a] = {};
447
+ for (const t of Object.values(e))
448
+ this._nameCacheMap[a][t.name] = t;
449
+ }
428
450
  };
429
451
  Ms = Hc([
430
452
  Gc(0, ra)
@@ -6411,7 +6433,7 @@ class Hn extends Tn {
6411
6433
  return g.create(t);
6412
6434
  if (e.t === yt.NUMBER) {
6413
6435
  const r = this._getPatternByCell(e);
6414
- return r === cc ? j.create(t.toString()) : Yn(t, r);
6436
+ return cc(r) ? j.create(t.toString()) : Yn(t, r);
6415
6437
  }
6416
6438
  return e.t === yt.STRING || e.t === yt.FORCE_STRING ? j.create(t.toString()) : e.t === yt.BOOLEAN ? $n(t) : pt.create(t);
6417
6439
  }
@@ -22,38 +22,79 @@ export declare class FFormula extends FBase {
22
22
  get lexerTreeBuilder(): LexerTreeBuilder;
23
23
  /**
24
24
  * Offsets the formula
25
- * @param {string} formulaString
26
- * @param {number} refOffsetX
27
- * @param {number} refOffsetY
28
- * @param {boolean} [ignoreAbsolute] default is false
25
+ * @param {string} formulaString - The formula string to offset
26
+ * @param {number} refOffsetX - The offset column
27
+ * @param {number} refOffsetY - The offset row
28
+ * @param {boolean} [ignoreAbsolute] - Whether to ignore the absolute reference
29
+ * @returns {string} The offset formula string
30
+ *
29
31
  * @example
30
- * const result = moveFormulaRefOffset('sum(a1,b2)',1,1)
31
- * // result is 'sum(b2,c3)'
32
+ * ```ts
33
+ * const formulaEngine = univerAPI.getFormula();
34
+ * const result = formulaEngine.moveFormulaRefOffset('=SUM(A1,B2)', 1, 1);
35
+ * console.log(result);
36
+ * ```
32
37
  */
33
38
  moveFormulaRefOffset(formulaString: string, refOffsetX: number, refOffsetY: number, ignoreAbsolute?: boolean): string;
34
39
  /**
35
40
  * Resolves the formula string to a 'node' node
36
- * @param {string} formulaString
37
- * @returns {*} {((string | ISequenceNode)[])}
38
- * @memberof FFormula
41
+ * @param {string} formulaString - The formula string to resolve
42
+ * @returns {Array<ISequenceNode | string>} The nodes of the formula string
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * const formulaEngine = univerAPI.getFormula();
47
+ * const nodes = formulaEngine.sequenceNodesBuilder('=SUM(A1,B2)');
48
+ * console.log(nodes);
49
+ * ```
39
50
  */
40
51
  sequenceNodesBuilder(formulaString: string): (string | ISequenceNode)[];
41
52
  /**
42
53
  * Start the calculation of the formula.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const formulaEngine = univerAPI.getFormula();
58
+ * formulaEngine.executeCalculation();
59
+ * ```
43
60
  */
44
61
  executeCalculation(): void;
45
62
  /**
46
63
  * Stop the calculation of the formula.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * const formulaEngine = univerAPI.getFormula();
68
+ * formulaEngine.stopCalculation();
69
+ * ```
47
70
  */
48
71
  stopCalculation(): void;
49
72
  /**
50
73
  * Listening calculation starts.
51
- * @param callback
74
+ * @param {Function} callback - The callback function to be called when the formula calculation starts.
75
+ * @returns {IDisposable} The disposable instance.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * const formulaEngine = univerAPI.getFormula();
80
+ * formulaEngine.calculationStart((forceCalculation) => {
81
+ * console.log('Calculation start', forceCalculation);
82
+ * });
83
+ * ```
52
84
  */
53
85
  calculationStart(callback: (forceCalculation: boolean) => void): IDisposable;
54
86
  /**
55
87
  * Listening calculation ends.
56
- * @param callback
88
+ * @param {Function} callback - The callback function to be called when the formula calculation ends.
89
+ * @returns {IDisposable} The disposable instance.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const formulaEngine = univerAPI.getFormula();
94
+ * formulaEngine.calculationEnd((functionsExecutedState) => {
95
+ * console.log('Calculation end', functionsExecutedState);
96
+ * });
97
+ * ```
57
98
  */
58
99
  calculationEnd(callback: (functionsExecutedState: FormulaExecutedStateType) => void): IDisposable;
59
100
  /**
@@ -63,6 +104,14 @@ export declare class FFormula extends FBase {
63
104
  * value is 30,000 milliseconds.
64
105
  * @returns {Promise<boolean>} This method returns `true` if the computing is complete. If the timeout is reached, this
65
106
  * method returns `false`.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * const formulaEngine = univerAPI.getFormula();
111
+ * formulaEngine.whenComputingCompleteAsync(3000).then((isComplete) => {
112
+ * console.log('Computing complete:', isComplete);
113
+ * });
114
+ * ```
66
115
  */
67
116
  whenComputingCompleteAsync(timeout?: number): Promise<boolean>;
68
117
  /**
@@ -72,12 +121,22 @@ export declare class FFormula extends FBase {
72
121
  onCalculationEnd(): Promise<void>;
73
122
  /**
74
123
  * Listening calculation processing.
75
- * @param callback
124
+ * @param {Function} callback - The callback function to be called when the formula calculation is in progress.
125
+ * @returns {IDisposable} The disposable instance.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const formulaEngine = univerAPI.getFormula();
130
+ * formulaEngine.calculationProcessing((stageInfo) => {
131
+ * console.log('Calculation processing', stageInfo);
132
+ * });
133
+ * ```
76
134
  */
77
135
  calculationProcessing(callback: (stageInfo: IExecutionInProgressParams) => void): IDisposable;
78
136
  /**
79
137
  * When a formula contains a circular reference, set the maximum number of iterations for the formula calculation.
80
- * @param maxIteration The maximum number of iterations. The default value is 1.
138
+ * @param {number} maxIteration The maximum number of iterations. The default value is 1.
139
+ *
81
140
  * @example
82
141
  * ```ts
83
142
  * // Set the maximum number of iterations for the formula calculation to 5.
@@ -38,6 +38,7 @@ export interface IDefinedNamesService {
38
38
  export declare class DefinedNamesService extends Disposable implements IDefinedNamesService {
39
39
  private readonly _univerInstanceService;
40
40
  private _definedNameMap;
41
+ private _nameCacheMap;
41
42
  private readonly _update$;
42
43
  readonly update$: Observable<unknown>;
43
44
  private _currentRange;
@@ -61,5 +62,6 @@ export declare class DefinedNamesService extends Disposable implements IDefinedN
61
62
  getValueById(unitId: string, id: string): IDefinedNamesServiceParam;
62
63
  hasDefinedName(unitId: string): boolean;
63
64
  private _update;
65
+ private _updateCache;
64
66
  }
65
67
  export declare const IDefinedNamesService: import('@wendellhu/redi').IdentifierDecorator<DefinedNamesService>;