@univerjs/sheets-formula
Version:
| Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales | | --- | --- | --- | --- | --- | :---: | :---: | | `@univerjs/sheets-formula` | `UniverSheetsFormula` | [![][npm-version-shield]][npm-version-link] | ![
1,597 lines • 519 kB
JavaScript
var Ka = Object.defineProperty;
var ka = (a, e, n) => e in a ? Ka(a, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : a[e] = n;
var W = (a, e, n) => ka(a, typeof e != "symbol" ? e + "" : e, n);
import { CommandType as yt, ICommandService as fe, ObjectMatrix as G, generateRandomId as da, IUniverInstanceService as Se, Rectangle as me, sequenceExecuteAsync as Qa, Inject as X, LifecycleService as $a, Disposable as ne, ILogService as ja, IConfigService as Ae, LocaleService as qt, isFormulaString as Me, isFormulaId as pa, cellToRange as La, Tools as Xe, Direction as Ke, getIntersectRange as We, RANGE_TYPE as Y, UniverInstanceType as ae, Injector as ut, InterceptorEffectEnum as za, CellValueType as wt, isRealNum as Za, createIdentifier as gt, toDisposable as oe, moveRangeByOffset as St, DisposableCollection as ke, AbsoluteRefType as Qe, Optional as Ja, DependentOn as Pa, Plugin as Ta, merge as Ea, touchDependencies as Fe, isNodeEnv as Yt } from "@univerjs/core";
import { SetRangeValuesCommand as Ia, SheetsSelectionsService as en, getSheetCommandTarget as tn, findFirstNonEmptyCell as an, alignToMergedCellsBorders as st, expandToContinuousRange as nn, SetSelectionsOperation as rn, SetRangeValuesMutation as z, SetStyleCommand as ht, SetBorderCommand as Na, ClearSelectionFormatCommand as Ra, handleMoveCols as Sa, EffectRefRangId as H, runRefRangeMutations as w, handleMoveRows as Aa, handleMoveRange as Ca, handleInsertRangeMoveRight as Da, handleInsertRangeMoveDown as ba, handleDeleteRangeMoveUp as Oa, handleDeleteRangeMoveLeft as Ma, handleIRemoveCol as xa, handleIRemoveRow as _a, handleInsertCol as ya, handleInsertRow as qa, RemoveDefinedNameCommand as ga, SetDefinedNameCommand as ha, RemoveSheetCommand as on, SetWorksheetNameCommand as un, DeleteRangeMoveLeftCommand as mn, DeleteRangeMoveUpCommand as fn, RemoveColCommand as cn, RemoveRowCommand as ln, InsertRangeMoveDownCommand as sn, InsertRangeMoveRightCommand as dn, InsertColCommand as pn, InsertRowCommand as Ln, MoveColsCommand as Pn, MoveRowsCommand as Tn, MoveRangeCommand as En, SheetInterceptorService as Ut, RemoveSheetMutation as At, InsertSheetMutation as Ct, MoveRangeMutation as Xt, MoveRowsMutation as Wt, MoveColsMutation as Kt, ReorderRangeMutation as kt, RemoveRowMutation as Qt, RemoveColMutation as $t, InsertColMutation as jt, InsertRowMutation as zt, SetRowHiddenMutation as Zt, SetRowVisibleMutation as Jt, INTERCEPTOR_POINT as In, SetWorksheetActiveOperation as Nn, SCOPE_WORKBOOK_VALUE_DEFINED_NAME as dt, RefRangeService as Rn, handleDefaultRangeChangeWithEffectRefCommands as ea, getSeparateEffectedRangesOnCommand as Sn, handleCommonDefaultRangeChangeWithEffectRefCommands as An, UniverSheetsPlugin as Cn } from "@univerjs/sheets";
import { serializeRange as _e, IActiveDirtyManagerService as Ft, SetFormulaCalculationResultMutation as Dn, RemoveOtherFormulaMutation as bn, SetOtherFormulaMutation as On, FormulaDataModel as mt, SetFormulaCalculationStartMutation as Ee, SetFormulaCalculationStopMutation as ta, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as aa, SetFormulaCalculationNotificationMutation as Mn, FormulaExecuteStageType as Be, FormulaExecutedStateType as De, sequenceNodeType as te, deserializeRangeWithSheetWithCache as Le, serializeRangeToRefString as ft, ErrorType as Q, IDefinedNamesService as Bt, LexerTreeBuilder as vt, generateStringWithSequence as $e, SetDefinedNameMutation as ye, SetFormulaDataMutation as ve, SetArrayFormulaDataMutation as Dt, initSheetFormulaData as na, RemoveDefinedNameMutation as bt, stripErrorMargin as xn, FunctionType as t, FUNCTION_NAMES_ARRAY as ia, FUNCTION_NAMES_COMPATIBILITY as x, FUNCTION_NAMES_CUBE as de, FUNCTION_NAMES_DATABASE as k, FUNCTION_NAMES_DATE as v, FUNCTION_NAMES_ENGINEERING as C, FUNCTION_NAMES_FINANCIAL as D, FUNCTION_NAMES_INFORMATION as V, FUNCTION_NAMES_LOGICAL as K, FUNCTION_NAMES_LOOKUP as y, FUNCTION_NAMES_MATH as T, FUNCTION_NAMES_STATISTICAL as l, FUNCTION_NAMES_TEXT as b, FUNCTION_NAMES_WEB as pt, IFunctionService as Vt, isReferenceStrings as _n, functionArray as yn, functionCompatibility as qn, functionCube as gn, functionDatabase as hn, functionDate as Un, functionEngineering as Fn, functionFinancial as Bn, functionInformation as vn, functionLogical as Vn, functionLookup as Gn, functionMath as Hn, functionMeta as wn, functionStatistical as Yn, functionText as Xn, functionUniver as Wn, functionWeb as Kn, serializeRangeWithSpreadsheet as ra, serializeRangeWithSheet as oa, CustomFunction as Ot, AsyncCustomFunction as Ua, UniverFormulaEnginePlugin as Fa, IFormulaCurrentConfigService as kn, Lexer as Qn, AstTreeBuilder as $n, Interpreter as jn, generateExecuteAstNodeData as zn, getObjectValue as Zn } from "@univerjs/engine-formula";
import { Subject as ua, BehaviorSubject as Ba, bufferWhen as Jn, filter as ma, map as ei } from "rxjs";
import { IRPCChannelService as va, fromModule as ti, toModule as ai } from "@univerjs/rpc";
const ni = {
id: "formula.command.insert-function",
type: yt.COMMAND,
handler: async (a, e) => {
const { list: n, listOfRangeHasNumber: i } = e, r = a.get(fe), o = new G();
n.forEach((f) => {
const { range: m, primary: c, formula: d } = f, { row: P, column: E } = c, p = da(6);
o.setValue(P, E, {
f: d,
si: p
});
const { startRow: I, startColumn: S, endRow: R, endColumn: s } = m;
for (let A = I; A <= R; A++)
for (let N = S; N <= s; N++)
(A !== P || N !== E) && o.setValue(A, N, {
si: p
});
}), i && i.length > 0 && i.forEach((f) => {
const { primary: m, formula: c } = f;
o.setValue(m.row, m.column, {
f: c
});
});
const u = {
value: o.getData()
};
return r.executeCommand(Ia.id, u);
}
}, ii = {
id: "sheets-formula.command.quick-sum",
type: yt.COMMAND,
handler: async (a) => {
const n = a.get(en).getCurrentLastSelection();
if (!n) return !1;
const i = a.get(Se), r = tn(i);
if (!r) return !1;
const o = n.range, { worksheet: u } = r;
let f = an(o, u);
if (!f) return !1;
f = st(f, u);
const m = nn({
startRow: f.startRow,
startColumn: f.startColumn,
endRow: o.endRow,
endColumn: o.endColumn
}, { left: !0, right: !0, up: !0, down: !0 }, u), c = new G(), d = st({
startRow: m.endRow,
endRow: m.endRow,
startColumn: m.startColumn,
endColumn: m.endColumn
}, u);
if (!me.equals(d, m))
for (const p of u.iterateByColumn(d))
(!p.value || !u.cellHasValue(p.value)) && c.setValue(p.row, p.col, {
f: `=SUM(${_e({
startColumn: p.col,
endColumn: p.col,
startRow: m.startRow,
endRow: p.row - 1
})})`
});
const P = st({
startRow: m.startRow,
startColumn: m.endColumn,
endRow: m.endRow,
endColumn: m.endColumn
}, u);
if (!me.equals(P, m))
for (const p of u.iterateByRow(P))
(!p.value || !u.cellHasValue(p.value)) && c.setValue(p.row, p.col, {
f: `=SUM(${_e({
startColumn: m.startColumn,
endColumn: p.col - 1,
startRow: p.row,
endRow: p.row
})})`
});
const E = a.get(fe);
return (await Qa([
{
id: Ia.id,
params: {
range: m,
value: c.getMatrix()
}
},
{
id: rn.id,
params: {
unitId: r.unitId,
subUnitId: r.subUnitId,
selections: [{
range: m,
primary: me.contains(m, n.primary) ? n.primary : { ...f, actualRow: f.startRow, actualColumn: f.startColumn },
style: null
}]
}
}
], E)).result;
}
}, Oe = {
type: yt.MUTATION,
id: "sheet.mutation.data-validation-formula-mark-dirty",
handler() {
return !0;
}
}, ge = "sheets-formula.base.config";
var Pe = /* @__PURE__ */ ((a) => (a[a.FORCED = 0] = "FORCED", a[a.WHEN_EMPTY = 1] = "WHEN_EMPTY", a[a.NO_CALCULATION = 2] = "NO_CALCULATION", a))(Pe || {});
const fa = {}, ri = "sheets-formula.remote.config", ca = {};
var Ie = /* @__PURE__ */ ((a) => (a[a.NOT_REGISTER = 1] = "NOT_REGISTER", a[a.SUCCESS = 2] = "SUCCESS", a[a.WAIT = 3] = "WAIT", a[a.ERROR = 4] = "ERROR", a))(Ie || {}), oi = Object.getOwnPropertyDescriptor, ui = (a, e, n, i) => {
for (var r = i > 1 ? void 0 : i ? oi(e, n) : e, o = a.length - 1, u; o >= 0; o--)
(u = a[o]) && (r = u(r) || r);
return r;
}, Lt = (a, e) => (n, i) => e(n, i, a);
let je = class extends ne {
constructor(e, n, i) {
super();
W(this, "_formulaCacheMap", /* @__PURE__ */ new Map());
W(this, "_formulaChangeWithRange$", new ua());
W(this, "formulaChangeWithRange$", this._formulaChangeWithRange$.asObservable());
// FIXME: this design could be improved.
W(this, "_formulaResult$", new ua());
W(this, "formulaResult$", this._formulaResult$.asObservable());
W(this, "calculateStarted$", new Ba(!1));
this._commandService = e, this._activeDirtyManagerService = n, this._lifecycleService = i, this._initFormulaRegister(), this._initFormulaCalculationResultChange();
}
dispose() {
super.dispose(), this._formulaChangeWithRange$.complete(), this._formulaResult$.complete(), this.calculateStarted$.complete();
}
_ensureCacheMap(e, n) {
let i = this._formulaCacheMap.get(e);
i || (i = /* @__PURE__ */ new Map(), this._formulaCacheMap.set(e, i));
let r = i.get(n);
return r || (r = /* @__PURE__ */ new Map(), i.set(n, r)), r;
}
_createFormulaId(e, n) {
return `formula.${e}_${n}_${da(8)}`;
}
_initFormulaRegister() {
this._activeDirtyManagerService.register(
Oe.id,
{
commandId: Oe.id,
getDirtyData(n) {
return {
dirtyUnitOtherFormulaMap: n.params
};
}
}
);
const e = (n) => {
const { unitId: i, subUnitId: r, formulaText: o, formulaId: u, ranges: f } = n;
if (!this._ensureCacheMap(i, r).has(u))
return;
const c = {
unitId: i,
subUnitId: r,
formulaMap: {
[u]: {
f: o,
ranges: f
}
}
};
this._commandService.executeCommand(On.id, c).then(() => {
this._commandService.executeCommand(
Oe.id,
{ [i]: { [r]: { [u]: !0 } } }
);
});
};
this.disposeWithMe(
this._formulaChangeWithRange$.pipe(Jn(() => this.calculateStarted$.pipe(ma((n) => n)))).subscribe((n) => n.forEach(e))
), this.disposeWithMe(
this._formulaChangeWithRange$.pipe(ma(() => this.calculateStarted$.getValue())).subscribe(e)
);
}
_initFormulaCalculationResultChange() {
this.disposeWithMe(this._commandService.onCommandExecuted((e) => {
if (e.id === Dn.id) {
const n = e.params, { unitOtherData: i } = n, r = {};
for (const o in i) {
const u = i[o], f = {};
r[o] = f;
for (const m in u) {
const c = this._ensureCacheMap(o, m), d = u[m], P = [];
f[m] = P;
for (const E in d) {
const p = d[E];
if (c.has(E)) {
const I = c.get(E);
if (!I)
continue;
I.result || (I.result = {});
const S = new G(p), R = new G(I.result);
S.forValue((s, A, N) => {
R.setValue(s, A, N);
}), I.status = Ie.SUCCESS, I.callbacks.forEach((s) => {
s(p);
}), I.callbacks.clear(), P.push(I);
}
}
}
}
this._formulaResult$.next(r);
}
}));
}
registerFormulaWithRange(e, n, i, r = [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }], o) {
const u = this._createFormulaId(e, n);
return this._ensureCacheMap(e, n).set(u, {
result: void 0,
status: Ie.WAIT,
formulaId: u,
callbacks: /* @__PURE__ */ new Set(),
extra: o
}), this._formulaChangeWithRange$.next({
unitId: e,
subUnitId: n,
formulaText: i,
formulaId: u,
ranges: r
}), u;
}
deleteFormula(e, n, i) {
const r = {
unitId: e,
subUnitId: n,
formulaIdList: i
};
this._commandService.executeCommand(bn.id, r);
const o = this._ensureCacheMap(e, n);
i.forEach((u) => o.delete(u));
}
getFormulaValue(e, n, i) {
const r = this._ensureCacheMap(e, n), o = r.get(i);
return o ? o.status === Ie.SUCCESS || o.status === Ie.ERROR ? Promise.resolve(o) : new Promise((u) => {
o.callbacks.add(() => {
u(r.get(i));
});
}) : Promise.resolve(null);
}
getFormulaValueSync(e, n, i) {
return this._ensureCacheMap(e, n).get(i);
}
markFormulaDirty(e, n, i) {
const r = this.getFormulaValueSync(e, n, i);
r && (r.status = Ie.WAIT, this._commandService.executeCommand(
Oe.id,
{ [e]: { [n]: { [i]: !0 } } }
));
}
};
je = ui([
Lt(0, fe),
Lt(1, Ft),
Lt(2, X($a))
], je);
var mi = Object.getOwnPropertyDescriptor, fi = (a, e, n, i) => {
for (var r = i > 1 ? void 0 : i ? mi(e, n) : e, o = a.length - 1, u; o >= 0; o--)
(u = a[o]) && (r = u(r) || r);
return r;
}, pe = (a, e) => (n, i) => e(n, i, a);
const la = { done: 0, count: 0 }, Pt = { onlyLocal: !0 };
let xe = class extends ne {
constructor(e, n, i, r, o, u, f) {
super();
W(this, "_waitingCommandQueue", []);
W(this, "_executingDirtyData", {
forceCalculation: !1,
dirtyRanges: [],
dirtyNameMap: {},
dirtyDefinedNameMap: {},
dirtyUnitFeatureMap: {},
dirtyUnitOtherFormulaMap: {},
clearDependencyTreeCache: {}
});
W(this, "_setTimeoutKey", -1);
W(this, "_startExecutionTime", 0);
W(this, "_totalCalculationTaskCount", 0);
W(this, "_doneCalculationTaskCount", 0);
W(this, "_executionInProgressParams", null);
W(this, "_restartCalculation", !1);
/**
* The mark of forced calculation. If a new mutation triggers dirty area calculation during the forced calculation process, forced calculation is still required.
*/
W(this, "_forceCalculating", !1);
W(this, "_progress$", new Ba(la));
W(this, "progress$", this._progress$.asObservable());
this._commandService = e, this._activeDirtyManagerService = n, this._logService = i, this._configService = r, this._formulaDataModel = o, this._localeService = u, this._registerOtherFormulaService = f, this._commandExecutedListener(), this._initialExecuteFormulaProcessListener(), this._initialExecuteFormula();
}
_emitProgress(e) {
this._progress$.next({ done: this._doneCalculationTaskCount, count: this._totalCalculationTaskCount, label: e });
}
_startProgress() {
this._doneCalculationTaskCount = 0, this._totalCalculationTaskCount = 1;
const e = this._localeService.t("formula.progress.analyzing");
this._emitProgress(e);
}
_calculateProgress(e) {
if (this._executionInProgressParams) {
const { totalFormulasToCalculate: n, completedFormulasCount: i, totalArrayFormulasToCalculate: r, completedArrayFormulasCount: o } = this._executionInProgressParams;
if (this._doneCalculationTaskCount = i + o, this._totalCalculationTaskCount = n + r, this._totalCalculationTaskCount === 0)
return;
this._emitProgress(e);
}
}
_completeProgress() {
this._doneCalculationTaskCount = this._totalCalculationTaskCount = 1;
const e = this._localeService.t("formula.progress.done");
this._emitProgress(e);
}
clearProgress() {
this._doneCalculationTaskCount = 0, this._totalCalculationTaskCount = 0, this._emitProgress();
}
dispose() {
super.dispose(), this._progress$.next(la), this._progress$.complete(), clearTimeout(this._setTimeoutKey);
}
_getCalculationMode() {
var n;
const e = this._configService.getConfig(ge);
return (n = e == null ? void 0 : e.initialFormulaComputing) != null ? n : Pe.WHEN_EMPTY;
}
_commandExecutedListener() {
this.disposeWithMe(
this._commandService.beforeCommandExecuted((e) => {
if (e.id === Ee.id) {
const n = e.params;
n.rowData = this._formulaDataModel.getHiddenRowsFiltered();
}
})
), this.disposeWithMe(
this._commandService.onCommandExecuted((e, n) => {
if (this._activeDirtyManagerService.get(e.id)) {
if (e.id === z.id) {
const i = e.params;
if (n && n.onlyLocal === !0 || i.trigger === ht.id || i.trigger === Na.id || i.trigger === Ra.id)
return;
}
this._waitingCommandQueue.push(e), clearTimeout(this._setTimeoutKey), this._setTimeoutKey = setTimeout(() => {
const i = this._generateDirty(this._waitingCommandQueue);
this._executingDirtyData = this._mergeDirty(this._executingDirtyData, i), this._executionInProgressParams == null ? this._commandService.executeCommand(Ee.id, { ...this._executingDirtyData }, Pt) : (this._restartCalculation = !0, this._commandService.executeCommand(ta.id, {})), this._waitingCommandQueue = [];
}, 100);
}
})
);
}
_generateDirty(e) {
const n = [], i = {}, r = {}, o = {}, u = {}, f = {};
for (const m of e) {
const c = this._activeDirtyManagerService.get(m.id);
if (c == null)
continue;
const d = c.getDirtyData(m), { dirtyRanges: P, dirtyNameMap: E, dirtyDefinedNameMap: p, dirtyUnitFeatureMap: I, dirtyUnitOtherFormulaMap: S, clearDependencyTreeCache: R } = d;
P != null && this._mergeDirtyRanges(n, P), E != null && this._mergeDirtyNameMap(i, E), p != null && this._mergeDirtyNameMap(r, p), I != null && this._mergeDirtyUnitFeatureOrOtherFormulaMap(o, I), S != null && this._mergeDirtyUnitFeatureOrOtherFormulaMap(u, S), R != null && this._mergeDirtyNameMap(f, R);
}
return {
dirtyRanges: n,
dirtyNameMap: i,
dirtyDefinedNameMap: r,
dirtyUnitFeatureMap: o,
dirtyUnitOtherFormulaMap: u,
forceCalculation: !1,
clearDependencyTreeCache: f,
maxIteration: this._configService.getConfig(aa)
// numfmtItemMap,
};
}
_mergeDirty(e, n) {
const i = [...e.dirtyRanges, ...n.dirtyRanges], r = { ...e.dirtyNameMap }, o = { ...e.dirtyDefinedNameMap }, u = { ...e.dirtyUnitFeatureMap }, f = { ...e.dirtyUnitOtherFormulaMap }, m = { ...e.clearDependencyTreeCache };
this._mergeDirtyNameMap(r, n.dirtyNameMap), this._mergeDirtyNameMap(o, n.dirtyDefinedNameMap), this._mergeDirtyUnitFeatureOrOtherFormulaMap(u, n.dirtyUnitFeatureMap), this._mergeDirtyUnitFeatureOrOtherFormulaMap(f, n.dirtyUnitOtherFormulaMap), this._mergeDirtyNameMap(m, n.clearDependencyTreeCache);
const c = e.maxIteration || n.maxIteration;
return {
dirtyRanges: i,
dirtyNameMap: r,
dirtyDefinedNameMap: o,
dirtyUnitFeatureMap: u,
dirtyUnitOtherFormulaMap: f,
forceCalculation: !!this._forceCalculating,
clearDependencyTreeCache: m,
maxIteration: c
};
}
/**
* dirtyRanges may overlap with the ranges in allDirtyRanges and need to be deduplicated
* @param allDirtyRanges
* @param dirtyRanges
*/
_mergeDirtyRanges(e, n) {
for (const i of n) {
let r = !1;
for (const o of e)
if (i.unitId === o.unitId && i.sheetId === o.sheetId) {
const { startRow: u, startColumn: f, endRow: m, endColumn: c } = i.range, { startRow: d, startColumn: P, endRow: E, endColumn: p } = o.range;
if (u === d && f === P && m === E && c === p) {
r = !0;
break;
}
}
r || e.push(i);
}
}
_mergeDirtyNameMap(e, n) {
Object.keys(n).forEach((i) => {
e[i] == null && (e[i] = {}), Object.keys(n[i]).forEach((r) => {
var o;
(o = n[i]) != null && o[r] && (e[i][r] = n[i][r]);
});
});
}
_mergeDirtyUnitFeatureOrOtherFormulaMap(e, n) {
Object.keys(n).forEach((i) => {
e[i] == null && (e[i] = {}), Object.keys(n[i]).forEach((r) => {
e[i][r] == null && (e[i][r] = {}), Object.keys(n[i][r]).forEach((o) => {
e[i][r][o] = n[i][r][o] || !1;
});
});
});
}
// eslint-disable-next-line max-lines-per-function
_initialExecuteFormulaProcessListener() {
let e = null, n = 0;
this.disposeWithMe(
// eslint-disable-next-line max-lines-per-function, complexity
this._commandService.onCommandExecuted((i) => {
if (i.id === Ee.id) {
const { forceCalculation: o = !1 } = i.params;
o && (this._forceCalculating = !0);
} else i.id === ta.id && this.clearProgress();
if (i.id !== Mn.id)
return;
const r = i.params;
if (r.stageInfo != null) {
const {
stage: o
} = r.stageInfo;
if (o === Be.START)
n === 0 && (this._startExecutionTime = performance.now()), n++, e !== null && (clearTimeout(e), e = null), e = setTimeout(() => {
e = null, this._startProgress();
}, 1e3);
else if (o === Be.CURRENTLY_CALCULATING) {
if (this._executionInProgressParams = r.stageInfo, e === null) {
const u = this._localeService.t("formula.progress.calculating");
this._calculateProgress(u);
}
} else if (o === Be.START_DEPENDENCY_ARRAY_FORMULA) {
if (this._executionInProgressParams = r.stageInfo, e === null) {
const u = this._localeService.t("formula.progress.array-analysis");
this._calculateProgress(u);
}
} else if (o === Be.CURRENTLY_CALCULATING_ARRAY_FORMULA && (this._executionInProgressParams = r.stageInfo, e === null)) {
const u = this._localeService.t("formula.progress.array-calculation");
this._calculateProgress(u);
}
} else {
const o = r.functionsExecutedState;
let u = "";
switch (n--, o) {
case De.NOT_EXECUTED:
u = "No tasks are being executed anymore", this._resetExecutingDirtyData();
break;
case De.STOP_EXECUTION:
u = "The execution of the formula has been stopped", n = 0;
break;
case De.SUCCESS:
u = "Formula calculation succeeded", (n === 0 || n === -1) && (u += `. Total time consumed: ${performance.now() - this._startExecutionTime} ms`), this._resetExecutingDirtyData();
break;
case De.INITIAL:
u = "Waiting for calculation", this._resetExecutingDirtyData();
break;
}
(n === 0 || n === -1) && (e ? (clearTimeout(e), e = null, this.clearProgress()) : this._completeProgress(), n = 0, this._doneCalculationTaskCount = 0, this._totalCalculationTaskCount = 0, this._forceCalculating = !1), o === De.STOP_EXECUTION && this._restartCalculation ? (this._restartCalculation = !1, this._commandService.executeCommand(
Ee.id,
{
...this._executingDirtyData
},
Pt
)) : this._executionInProgressParams = null, this._logService.debug("[TriggerCalculationController]", u);
}
})
);
}
_resetExecutingDirtyData() {
this._executingDirtyData = {
dirtyRanges: [],
dirtyNameMap: {},
dirtyDefinedNameMap: {},
dirtyUnitFeatureMap: {},
dirtyUnitOtherFormulaMap: {},
forceCalculation: !1,
clearDependencyTreeCache: {}
};
}
_initialExecuteFormula() {
const e = this._getCalculationMode(), n = this._getDirtyDataByCalculationMode(e);
this._commandService.executeCommand(Ee.id, n, Pt), this._registerOtherFormulaService.calculateStarted$.next(!0);
}
_getDirtyDataByCalculationMode(e) {
const n = e === Pe.FORCED, i = e === Pe.WHEN_EMPTY ? this._formulaDataModel.getFormulaDirtyRanges() : [];
return {
forceCalculation: n,
dirtyRanges: i,
dirtyNameMap: {},
dirtyDefinedNameMap: {},
dirtyUnitFeatureMap: {},
dirtyUnitOtherFormulaMap: {},
clearDependencyTreeCache: {},
maxIteration: this._configService.getConfig(aa)
};
}
};
xe = fi([
pe(0, fe),
pe(1, Ft),
pe(2, ja),
pe(3, Ae),
pe(4, X(mt)),
pe(5, X(qt)),
pe(6, X(je))
], xe);
function ci(a, e, n) {
var i;
return a == null || a[e] == null || ((i = a[e]) == null ? void 0 : i[n]) == null;
}
function Tt(a, e, n) {
var i;
if (n) {
if (a && a[e] && ((i = a[e]) != null && i[n]))
return delete a[e][n], {
[e]: {
[n]: null
}
};
} else if (a && a[e])
return delete a[e], {
[e]: null
};
}
var _ = /* @__PURE__ */ ((a) => (a[a.MoveRange = 0] = "MoveRange", a[a.MoveRows = 1] = "MoveRows", a[a.MoveCols = 2] = "MoveCols", a[a.InsertRow = 3] = "InsertRow", a[a.InsertColumn = 4] = "InsertColumn", a[a.RemoveRow = 5] = "RemoveRow", a[a.RemoveColumn = 6] = "RemoveColumn", a[a.DeleteMoveLeft = 7] = "DeleteMoveLeft", a[a.DeleteMoveUp = 8] = "DeleteMoveUp", a[a.InsertMoveDown = 9] = "InsertMoveDown", a[a.InsertMoveRight = 10] = "InsertMoveRight", a[a.SetName = 11] = "SetName", a[a.RemoveSheet = 12] = "RemoveSheet", a[a.SetDefinedName = 13] = "SetDefinedName", a[a.RemoveDefinedName = 14] = "RemoveDefinedName", a))(_ || {});
const li = [
11,
12,
13,
14
/* RemoveDefinedName */
];
function si(a, e, n) {
const { type: i } = n;
return li.includes(i) ? di(a, e) : pi(a, e, n);
}
function di(a, e) {
const n = [], i = [];
return Object.keys(e).forEach((r) => {
const o = e[r], u = a[r];
if (o == null || u == null)
return !0;
Object.keys(o).forEach((f) => {
const m = new G(o[f] || {}), c = new G(u[f] || {}), d = new G(), P = new G();
if (m.forValue((R, s, A) => {
if (A == null)
return !0;
const N = qe(A);
N !== null && (d.setValue(R, s, N), P.setValue(R, s, c.getValue(R, s)));
}), d.getSizeOf() === 0)
return;
const E = {
subUnitId: f,
unitId: r,
cellValue: d.getMatrix()
}, p = {
id: z.id,
params: E
};
i.push(p);
const I = {
subUnitId: f,
unitId: r,
cellValue: P.getMatrix()
}, S = {
id: z.id,
params: I
};
n.push(S);
});
}), {
undos: n,
redos: i
};
}
function pi(a, e, n) {
const { redoFormulaData: i, undoFormulaData: r } = Li(a, e, n), o = [], u = [];
return Object.keys(i).forEach((f) => {
Object.keys(i[f]).forEach((m) => {
if (Object.keys(i[f][m]).length !== 0) {
const c = {
subUnitId: m,
unitId: f,
cellValue: i[f][m]
}, d = {
id: z.id,
params: c
};
o.push(d);
}
});
}), Object.keys(r).forEach((f) => {
Object.keys(r[f]).forEach((m) => {
if (Object.keys(r[f][m]).length !== 0) {
const c = {
subUnitId: m,
unitId: f,
cellValue: r[f][m]
}, d = {
id: z.id,
params: c
};
u.push(d);
}
});
}), {
undos: u,
redos: o
};
}
function Li(a, e, n) {
const i = {}, r = {}, { unitId: o, sheetId: u } = n;
return (/* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(e)])).forEach((m) => {
if (ci(a, m, u))
return;
(/* @__PURE__ */ new Set([
...Object.keys(a[m] || {}),
...Object.keys(e[m] || {})
])).forEach((d) => {
var A, N;
const P = (A = a[m]) == null ? void 0 : A[d], E = (N = e[m]) == null ? void 0 : N[d], p = new G(P || {}), I = new G(E || {});
let S = [];
m !== o || d !== u ? S = Ti(I) : S = Pi(p, I, n);
const R = yi(S, p, I), s = qi(S, p);
i[m] || (i[m] = {}), r[m] || (r[m] = {}), i[m][d] = {
...i[m][d],
...R
}, r[m][d] = {
...r[m][d],
...s
};
});
}), {
redoFormulaData: i,
undoFormulaData: r
};
}
function Pi(a, e, n) {
const { type: i, from: r, to: o, range: u } = n, f = [];
return a.forValue((m, c, d) => {
if (d == null || !Va(d)) return !0;
const P = La(m, c);
let E = null, p = !1;
if ([
0,
1,
2
/* MoveCols */
].includes(i))
E = Ei(i, r, o, P);
else if (u != null) {
const I = Ii(P, n);
E = I.newCell, p = I.isReverse;
}
if (Xe.diffValue(P, E) && !e.getValue(m, c))
return !0;
p ? f.unshift({ oldCell: P, newCell: E }) : f.push({ oldCell: P, newCell: E });
}), f;
}
function Ti(a) {
const e = [];
return a.forValue((n, i, r) => {
if (r == null || !Va(r)) return !0;
const o = La(n, i);
e.push({ oldCell: o, newCell: o });
}), e;
}
function Ei(a, e, n, i) {
if (e == null || n == null)
return null;
switch (a) {
case 0:
return Ni(e, n, i);
case 1:
return Ri(e, n, i);
case 2:
return Si(e, n, i);
default:
return null;
}
}
function Ii(a, e) {
const { type: n, rangeFilteredRows: i } = e, r = e.range;
let o = null, u = !1;
switch (n) {
case 3:
o = Ai(r, a), u = !0;
break;
case 4:
o = Ci(r, a), u = !0;
break;
case 5:
o = Di(r, a, i);
break;
case 6:
o = bi(r, a);
break;
case 7:
o = Oi(r, a);
break;
case 8:
o = Mi(r, a);
break;
case 9:
o = xi(r, a), u = !0;
break;
case 10:
o = _i(r, a), u = !0;
break;
}
return { newCell: o, isReverse: u };
}
function Ni(a, e, n) {
const i = Ca(
{
id: H.MoveRangeCommandId,
params: { toRange: e, fromRange: a }
},
n
);
return w(i, n);
}
function Ri(a, e, n) {
const i = Aa(
{
id: H.MoveRowsCommandId,
params: { toRange: e, fromRange: a }
},
n
);
return w(i, n);
}
function Si(a, e, n) {
const i = Sa(
{
id: H.MoveColsCommandId,
params: { toRange: e, fromRange: a }
},
n
);
return w(i, n);
}
function Ai(a, e) {
const n = qa(
{
id: H.InsertRowCommandId,
params: { range: a, unitId: "", subUnitId: "", direction: Ke.DOWN }
},
e
);
return w(n, e);
}
function Ci(a, e) {
const n = ya(
{
id: H.InsertColCommandId,
params: { range: a, unitId: "", subUnitId: "", direction: Ke.RIGHT }
},
e
);
return w(n, e);
}
function Di(a, e, n) {
const i = _a(
{
id: H.RemoveRowCommandId,
params: { range: a }
},
e,
n
);
return w(i, e);
}
function bi(a, e) {
const n = xa(
{
id: H.RemoveColCommandId,
params: { range: a }
},
e
);
return w(n, e);
}
function Oi(a, e) {
const n = Ma(
{
id: H.DeleteRangeMoveLeftCommandId,
params: { range: a }
},
e
);
return w(n, e);
}
function Mi(a, e) {
const n = Oa(
{
id: H.DeleteRangeMoveUpCommandId,
params: { range: a }
},
e
);
return w(n, e);
}
function xi(a, e) {
const n = ba(
{
id: H.InsertRangeMoveDownCommandId,
params: { range: a }
},
e
);
return w(n, e);
}
function _i(a, e) {
const n = Da(
{
id: H.InsertRangeMoveRightCommandId,
params: { range: a }
},
e
);
return w(n, e);
}
function yi(a, e, n) {
var r, o, u;
const i = new G({});
for (let f = 0; f < a.length; f++) {
const { oldCell: m, newCell: c } = a[f];
if ((r = i.getValue(m.startRow, m.startColumn)) != null && r.f || (o = i.getValue(m.startRow, m.startColumn)) != null && o.si || i.setValue(m.startRow, m.startColumn, { f: null, si: null }), c) {
const d = (u = n.getValue(m.startRow, m.startColumn)) != null ? u : e.getValue(m.startRow, m.startColumn), P = qe(d);
i.setValue(c.startRow, c.startColumn, P);
}
}
return i.getMatrix();
}
function qi(a, e) {
const n = new G({});
for (let i = a.length - 1; i >= 0; i--) {
const { oldCell: r, newCell: o } = a[i], u = e.getValue(r.startRow, r.startColumn), f = qe(u);
if (n.setValue(r.startRow, r.startColumn, f), o) {
const m = e.getValue(o.startRow, o.startColumn), c = qe(m);
n.setValue(o.startRow, o.startColumn, c != null ? c : { f: null, si: null });
}
}
return n.getMatrix();
}
function qe(a) {
if (a == null)
return;
const { f: e, si: n, x: i = 0, y: r = 0 } = a, o = Me(e), u = pa(n);
if (!o && !u)
return {
f: null,
si: null
};
const f = {};
return u && (f.si = n), o && i === 0 && r === 0 && (f.f = e), f.f === void 0 && (f.f = null), f.si === void 0 && (f.si = null), f;
}
function gi(a) {
const e = new G({});
return new G(a).forValue((i, r, o) => {
const u = qe(o);
u && e.setValue(i, r, u);
}), e.getMatrix();
}
function Va(a) {
const e = (a == null ? void 0 : a.f) || "", n = (a == null ? void 0 : a.si) || "", i = Me(e), r = pa(n);
return !!(i || r);
}
function Ga(a, e, n, i, r, o) {
if ((r == null || r.length === 0) && (o == null || o.length === 0)) {
if (a === n && e === i)
return !0;
} else if ((a === r || r == null || r.length === 0) && e === o)
return !0;
return !1;
}
function Ha(a, e, n = 0, i = 0) {
const r = [];
for (let o = 0, u = a.length; o < u; o++) {
const f = a[o];
if (typeof f == "string" || f.nodeType !== te.REFERENCE || e.includes(o)) {
r.push(f);
continue;
}
const { token: m } = f, c = Le(m), { range: d, sheetName: P, unitId: E } = c, p = me.moveOffset(d, n, i);
r.push({
...f,
token: ft({
range: p,
unitId: E,
sheetName: P
})
});
}
return r;
}
function wa(a, e, n, i) {
const { type: r, unitId: o, sheetId: u, range: f, from: m, to: c, rangeFilteredRows: d } = e, {
range: P,
sheetId: E,
unitId: p,
sheetName: I,
refOffsetX: S,
refOffsetY: R
} = a;
if (!Ga(
o,
u,
n,
i,
p,
E
))
return;
const s = me.moveOffset(P, S, R);
let A = null;
if (r === _.MoveRange) {
if (m == null || c == null)
return;
const N = It(s, m), L = We(s, m);
if (L == null || N !== 4)
return;
const g = Ca(
{ id: H.MoveRangeCommandId, params: { toRange: c, fromRange: m } },
L
), q = w(g, L);
if (q == null)
return Q.REF;
A = Et(N, q, m, c, s, L);
} else if (r === _.MoveRows) {
if (m == null || c == null)
return;
const N = It(s, m);
let L = We(s, m);
if (L == null && (m.endRow < s.startRow && c.endRow <= s.startRow || m.startRow > s.endRow && c.startRow > s.endRow))
return;
L == null && (L = {
startRow: s.startRow,
endRow: s.endRow,
startColumn: s.startColumn,
endColumn: s.endColumn,
rangeType: Y.NORMAL
});
const g = Aa(
{ id: H.MoveRowsCommandId, params: { toRange: c, fromRange: m } },
L
), q = w(g, L);
if (q == null)
return Q.REF;
A = Et(N, q, m, c, s, L);
} else if (r === _.MoveCols) {
if (m == null || c == null)
return;
const N = It(s, m);
let L = We(s, m);
if (L == null && (m.endColumn < s.startColumn && c.endColumn <= s.startColumn || m.startColumn > s.endColumn && c.startColumn > s.endColumn))
return;
L == null && (L = {
startRow: s.startRow,
endRow: s.endRow,
startColumn: s.startColumn,
endColumn: s.endColumn,
rangeType: Y.NORMAL
});
const g = Sa(
{ id: H.MoveColsCommandId, params: { toRange: c, fromRange: m } },
L
), q = w(g, L);
if (q == null)
return Q.REF;
A = Et(N, q, m, c, s, L);
}
if (f != null) {
if (r === _.InsertRow) {
const N = qa(
{
id: H.InsertRowCommandId,
params: { range: f, unitId: "", subUnitId: "", direction: Ke.DOWN }
},
s
), L = w(N, s);
if (L == null)
return;
A = {
...s,
...L
};
} else if (r === _.InsertColumn) {
const N = ya(
{
id: H.InsertColCommandId,
params: { range: f, unitId: "", subUnitId: "", direction: Ke.RIGHT }
},
s
), L = w(N, s);
if (L == null)
return;
A = {
...s,
...L
};
} else if (r === _.RemoveRow) {
const N = _a(
{
id: H.RemoveRowCommandId,
params: { range: f }
},
s,
d
), L = w(N, s);
if (L == null)
return Q.REF;
A = {
...s,
...L
};
} else if (r === _.RemoveColumn) {
const N = xa(
{
id: H.RemoveColCommandId,
params: { range: f }
},
s
), L = w(N, s);
if (L == null)
return Q.REF;
A = {
...s,
...L
};
} else if (r === _.DeleteMoveLeft) {
const N = Ma(
{
id: H.DeleteRangeMoveLeftCommandId,
params: { range: f }
},
s
), L = w(N, s);
if (L == null)
return Q.REF;
A = {
...s,
...L
};
} else if (r === _.DeleteMoveUp) {
const N = Oa(
{
id: H.DeleteRangeMoveUpCommandId,
params: { range: f }
},
s
), L = w(N, s);
if (L == null)
return Q.REF;
A = {
...s,
...L
};
} else if (r === _.InsertMoveDown) {
const N = ba(
{
id: H.InsertRangeMoveDownCommandId,
params: { range: f }
},
s
), L = w(N, s);
if (L == null)
return;
A = {
...s,
...L
};
} else if (r === _.InsertMoveRight) {
const N = Da(
{
id: H.InsertRangeMoveRightCommandId,
params: { range: f }
},
s
), L = w(N, s);
if (L == null)
return;
A = {
...s,
...L
};
}
}
if (A != null)
return ft({
range: A,
sheetName: I,
unitId: p
});
}
function Et(a, e, n, i, r, o) {
const { startRow: u, endRow: f, startColumn: m, endColumn: c, rangeType: d } = be(e), {
startRow: P,
startColumn: E,
endRow: p,
endColumn: I,
rangeType: S = Y.NORMAL
} = be(n), { startRow: R, startColumn: s, endRow: A, endColumn: N } = be(i), {
startRow: L,
endRow: g,
startColumn: q,
endColumn: U
} = be(o), {
startRow: h,
endRow: M,
startColumn: F,
endColumn: B,
rangeType: J = Y.NORMAL
} = be(r), O = { ...r };
function ce() {
return d === Y.COLUMN && J === Y.COLUMN ? !0 : m >= F && c <= B;
}
function ee() {
return d === Y.ROW && J === Y.ROW ? !0 : u >= h && f <= M;
}
if (a === 0)
if (ce())
if (u < h)
O.startRow = u;
else if (u >= M)
O.endRow -= p + 1 - h;
else
return;
else
return;
else if (a === 1)
if (ce())
if (f > M)
O.endRow = f;
else if (f <= h)
O.startRow += M - P + 1;
else
return;
else
return;
else if (a === 2)
if (ee())
if (m < F)
O.startColumn = m;
else if (m >= B)
O.endColumn -= I + 1 - F;
else
return;
else
return;
else if (a === 3)
if (ee())
if (c > B)
O.endColumn = c;
else if (c <= F)
O.startColumn += B - E + 1;
else
return;
else
return;
else a === 4 ? (O.startRow = u, O.startColumn = m, O.endRow = f, O.endColumn = c) : E <= F && I >= B || S === Y.ROW && J === Y.ROW ? n.endRow < h ? (R >= h && (O.startRow -= p - P + 1), R >= M && (O.endRow -= p - P + 1)) : n.startRow > M ? (A <= M && (O.endRow += p - P + 1), A <= h && (O.startRow += p - P + 1)) : n.startRow >= h && n.endRow <= M && (R <= h ? O.startRow += p - P + 1 : R >= M && (O.endRow -= p - P + 1)) : P <= h && p >= M || S === Y.COLUMN && J === Y.COLUMN ? n.endColumn < F ? (s >= F && (O.startColumn -= I - E + 1), s >= B && (O.endColumn -= I - E + 1)) : n.startColumn > B ? (N <= B && (O.endColumn += I - E + 1), N <= F && (O.startColumn += I - E + 1)) : n.startColumn >= F && n.endColumn <= B && (s <= F ? O.startColumn += I - E + 1 : s >= B && (O.endColumn -= I - E + 1)) : ((s <= U + 1 && N >= B || s <= F && N >= q - 1) && R <= h && A >= M || (R <= g + 1 && A >= M || R <= h && A >= L - 1) && s <= F && N >= B, O.startRow = u, O.startColumn = m, O.endRow = f, O.endColumn = c);
return O;
}
function It(a, e) {
const n = Ne(a.startRow), i = Re(a.endRow), r = Ne(a.startColumn), o = Re(a.endColumn), u = Ne(e.startRow), f = Re(e.endRow), m = Ne(e.startColumn), c = Re(e.endColumn);
function d() {
return a.rangeType === Y.COLUMN && e.rangeType === Y.COLUMN ? !0 : n >= u && i <= f;
}
function P() {
return a.rangeType === Y.ROW && e.rangeType === Y.ROW ? !0 : r >= m && o <= c;
}
function E() {
return a.rangeType === Y.ALL && e.rangeType === Y.ALL;
}
return d() && P() || E() ? 4 : P() && n >= u && n <= f && i > f ? 0 : P() && i >= u && i <= f && n < u ? 1 : d() && r >= m && r <= c && o > c ? 2 : d() && o >= m && o <= c && r < m ? 3 : null;
}
function Ne(a) {
return isNaN(a) ? -1 / 0 : a;
}
function Re(a) {
return isNaN(a) ? 1 / 0 : a;
}
function be(a) {
const { startRow: e, endRow: n, startColumn: i, endColumn: r } = a;
return {
...a,
startRow: Ne(e),
endRow: Re(n),
startColumn: Ne(i),
endColumn: Re(r)
};
}
function Ya(a, e) {
const { id: n } = e;
let i = null;
switch (n) {
case En.id:
i = hi(e, a);
break;
case Tn.id:
i = Ui(e, a);
break;
case Pn.id:
i = Fi(e, a);
break;
case Ln.id:
i = Bi(e);
break;
case pn.id:
i = vi(e);
break;
case dn.id:
i = Vi(e, a);
break;
case sn.id:
i = Gi(e, a);
break;
case ln.id:
i = Hi(e, a);
break;
case cn.id:
i = wi(e, a);
break;
case fn.id:
i = Yi(e, a);
break;
case mn.id:
i = Xi(e, a);
break;
case un.id:
i = Wi(e, a);
break;
case on.id:
i = Ki(e, a);
break;
case ha.id:
i = ki(e, a);
break;
case ga.id:
i = Qi(e, a);
break;
}
return i;
}
function Z(a) {
var i;
const e = a.getUnitId(), n = ((i = a.getActiveSheet()) == null ? void 0 : i.getSheetId()) || "";
return {
unitId: e,
sheetId: n
};
}
function hi(a, e) {
const { params: n } = a;
if (!n) return null;
const { fromRange: i, toRange: r } = n;
if (!i || !r) return null;
const { unitId: o, sheetId: u } = Z(e);
return {
type: _.MoveRange,
from: i,
to: r,
unitId: o,
sheetId: u
};
}
function Ui(a, e) {
const { params: n } = a;
if (!n) return null;
const {
fromRange: { startRow: i, endRow: r },
toRange: { startRow: o, endRow: u }
} = n, f = e.getUnitId(), m = e.getActiveSheet();
if (!m) return null;
const c = m.getSheetId(), d = {
startRow: i,
startColumn: 0,
endRow: r,
endColumn: m.getColumnCount() - 1,
rangeType: Y.ROW
}, P = {
startRow: o,
startColumn: 0,
endRow: u,
endColumn: m.getColumnCount() - 1,
rangeType: Y.ROW
};
return {
type: _.MoveRows,
from: d,
to: P,
unitId: f,
sheetId: c
};
}
function Fi(a, e) {
const { params: n } = a;
if (!n) return null;
const {
fromRange: { startColumn: i, endColumn: r },
toRange: { startColumn: o, endColumn: u }
} = n, f = e.getUnitId(), m = e.getActiveSheet();
if (!m) return null;
const c = m.getSheetId(), d = {
startRow: 0,
startColumn: i,
endRow: m.getRowCount() - 1,
endColumn: r,
rangeType: Y.COLUMN
}, P = {
startRow: 0,
startColumn: o,
endRow: m.getRowCount() - 1,
endColumn: u,
rangeType: Y.COLUMN
};
return {
type: _.MoveCols,
from: d,
to: P,
unitId: f,
sheetId: c
};
}
function Bi(a) {
const { params: e } = a;
if (!e) return null;
const { range: n, unitId: i, subUnitId: r } = e;
return {
type: _.InsertRow,
range: n,
unitId: i,
sheetId: r
};
}
function vi(a) {
const { params: e } = a;
if (!e) return null;
const { range: n, unitId: i, subUnitId: r } = e;
return {
type: _.InsertColumn,
range: n,
unitId: i,
sheetId: r
};
}
function Vi(a, e) {
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.InsertMoveRight,
range: i,
unitId: r,
sheetId: o
};
}
function Gi(a, e) {
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.InsertMoveDown,
range: i,
unitId: r,
sheetId: o
};
}
function Hi(a, e) {
var u, f;
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.RemoveRow,
range: i,
unitId: r,
sheetId: o,
rangeFilteredRows: (f = (u = e.getSheetBySheetId(o)) == null ? void 0 : u.getRangeFilterRows(i)) != null ? f : []
};
}
function wi(a, e) {
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.RemoveColumn,
range: i,
unitId: r,
sheetId: o
};
}
function Yi(a, e) {
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.DeleteMoveUp,
range: i,
unitId: r,
sheetId: o
};
}
function Xi(a, e) {
const { params: n } = a;
if (!n) return null;
const { range: i } = n, { unitId: r, sheetId: o } = Z(e);
return {
type: _.DeleteMoveLeft,
range: i,
unitId: r,
sheetId: o
};
}
function Wi(a, e) {
const { params: n } = a;
if (!n) return null;
const { unitId: i, subUnitId: r, name: o } = n, { unitId: u, sheetId: f } = Z(e);
return {
type: _.SetName,
unitId: i || u,
sheetId: r || f,
sheetName: o
};
}
function Ki(a, e) {
const { params: n } = a;
if (!n) return null;
const { unitId: i, subUnitId: r } = n, { unitId: o, sheetId: u } = Z(e);
return {
type: _.RemoveSheet,
unitId: i || o,
sheetId: r || u
};
}
function ki(a, e) {
const { params: n } = a;
if (!n) return null;
const { unitId: i, name: r, id: o } = n, { sheetId: u } = Z(e);
return {
type: _.SetDefinedName,
unitId: i,
sheetId: u,
definedName: r,
definedNameId: o
};
}
function Qi(a, e) {
const { params: n } = a;
if (!n) return null;
const { unitId: i, name: r, id: o } = n, { sheetId: u } = Z(e);
return {
type: _.RemoveDefinedName,
unitId: i,
sheetId: u,
definedName: r,
definedNameId: o
};
}
var $i = Object.getOwnPropertyDescriptor, ji = (a, e, n, i) => {
for (var r = i > 1 ? void 0 : i ? $i(e, n) : e, o = a.length - 1, u; o >= 0; o--)
(u = a[o]) && (r = u(r) || r);
return r;
}, Ve = (a, e) => (n, i) => e(n, i, a);
let ze = class extends ne {
constructor(a, e, n, i) {
super(), this._definedNamesService = a, this._univerInstanceService = e, this._sheetInterceptorService = n, this._lexerTreeBuilder = i, this._initialize();
}
_initialize() {
this._commandExecutedListener();
}
_commandExecutedListener() {
this.disposeWithMe(
this._sheetInterceptorService.interceptCommand({
getMutations: (a) => {
if (a.id === ha.id || a.id === ga.id)
return {
redos: [],
undos: []
};
const e = this._univerInstanceService.getCurrentUnitForType(ae.UNIVER_SHEET);
if (e == null)
return {
redos: [],
undos: []
};
const n = Ya(e, a);
return n ? this._getUpdateDefinedNameMutations(e, n) : {
redos: [],
undos: []
};
}
})
);
}
// eslint-disable-next-line max-lines-per-function
_getUpdateDefinedNameMutations(a, e) {
const { type: n, unitId: i, sheetId: r } = e, o = this._definedNamesService.getDefinedNameMap(i);
if (!o)
return {
redos: [],
undos: []
};
const u = [], f = [];
return Object.values(o).forEach((m) => {
var R;
const { formulaOrRefString: c } = m, d = this._lexerTreeBuilder.sequenceNodesBuilder(c);
if (d == null)
return !0;
let P = !1;
const E = [];
for (let s = 0, A = d.length; s < A; s++) {
const N = d[s];
if (typeof N == "string" || N.nodeType !== te.REFERENCE)
continue;
const { token: L } = N, g = Le(L), { range: q, sheetName: U, unitId: h } = g, M = ((R = a.getSheetBySheetName(U)) == null ? void 0 : R.getSheetId()) || "", F = {
range: q,
sheetId: M,
unitId: h,
sheetName: U,
refOffsetX: 0,
refOffsetY: 0
};
let B = null;
if (n === _.RemoveSheet)
B = this._removeSheet(m, i, r);
else if (n === _.SetName) {
const {
sheetId: J,
sheetName: O
} = e;
if (O == null || M == null || M.length === 0 || J !== M)
continue;
B = ft({
range: q,
sheetName: O,
unitId: h
});
} else
B = wa(
F,
e,
i,
r
);
B != null && (d[s] = {
...N,
token: B
}, P = !0, E.push(s));
}
if (!P)
return !0;
const p = $e(Ha(d, E)), I = {
id: ye.id,
params: {
unitId: i,
...m,
formulaOrRefString: p
}
};
u.push(I);
const S = {
id: ye.id,
params: {
unitId: i,
...m
}
};
f.push(S);
}), {
redos: u,
undos: f
};
}