@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 lines • 410 kB
JavaScript
(function(N,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs"),require("@univerjs/rpc")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","@univerjs/engine-formula","rxjs","@univerjs/rpc"],f):(N=typeof globalThis<"u"?globalThis:N||self,f(N.UniverSheetsFormula={},N.UniverCore,N.UniverSheets,N.UniverEngineFormula,N.rxjs,N.UniverRpc))})(this,function(N,f,s,t,H,rt){"use strict";var Ca=Object.defineProperty;var Ra=(N,f,s)=>f in N?Ca(N,f,{enumerable:!0,configurable:!0,writable:!0,value:s}):N[f]=s;var h=(N,f,s)=>Ra(N,typeof f!="symbol"?f+"":f,s);const Dt={id:"formula.command.insert-function",type:f.CommandType.COMMAND,handler:async(r,e)=>{const{list:a,listOfRangeHasNumber:n}=e,i=r.get(f.ICommandService),o=new f.ObjectMatrix;a.forEach(c=>{const{range:m,primary:l,formula:d}=c,{row:T,column:E}=l,L=f.generateRandomId(6);o.setValue(T,E,{f:d,si:L});const{startRow:S,startColumn:C,endRow:R,endColumn:p}=m;for(let A=S;A<=R;A++)for(let I=C;I<=p;I++)(A!==T||I!==E)&&o.setValue(A,I,{si:L})}),n&&n.length>0&&n.forEach(c=>{const{primary:m,formula:l}=c;o.setValue(m.row,m.column,{f:l})});const u={value:o.getData()};return i.executeCommand(s.SetRangeValuesCommand.id,u)}},bt={id:"sheets-formula.command.quick-sum",type:f.CommandType.COMMAND,handler:async r=>{const a=r.get(s.SheetsSelectionsService).getCurrentLastSelection();if(!a)return!1;const n=r.get(f.IUniverInstanceService),i=s.getSheetCommandTarget(n);if(!i)return!1;const o=a.range,{worksheet:u}=i;let c=s.findFirstNonEmptyCell(o,u);if(!c)return!1;c=s.alignToMergedCellsBorders(c,u);const m=s.expandToContinuousRange({startRow:c.startRow,startColumn:c.startColumn,endRow:o.endRow,endColumn:o.endColumn},{left:!0,right:!0,up:!0,down:!0},u),l=new f.ObjectMatrix,d=s.alignToMergedCellsBorders({startRow:m.endRow,endRow:m.endRow,startColumn:m.startColumn,endColumn:m.endColumn},u);if(!f.Rectangle.equals(d,m))for(const L of u.iterateByColumn(d))(!L.value||!u.cellHasValue(L.value))&&l.setValue(L.row,L.col,{f:`=SUM(${t.serializeRange({startColumn:L.col,endColumn:L.col,startRow:m.startRow,endRow:L.row-1})})`});const T=s.alignToMergedCellsBorders({startRow:m.startRow,startColumn:m.endColumn,endRow:m.endRow,endColumn:m.endColumn},u);if(!f.Rectangle.equals(T,m))for(const L of u.iterateByRow(T))(!L.value||!u.cellHasValue(L.value))&&l.setValue(L.row,L.col,{f:`=SUM(${t.serializeRange({startColumn:m.startColumn,endColumn:L.col-1,startRow:L.row,endRow:L.row})})`});const E=r.get(f.ICommandService);return(await f.sequenceExecuteAsync([{id:s.SetRangeValuesCommand.id,params:{range:m,value:l.getMatrix()}},{id:s.SetSelectionsOperation.id,params:{unitId:i.unitId,subUnitId:i.subUnitId,selections:[{range:m,primary:f.Rectangle.contains(m,a.primary)?a.primary:{...c,actualRow:c.startRow,actualColumn:c.startColumn},style:null}]}}],E)).result}},Q={type:f.CommandType.MUTATION,id:"sheet.mutation.data-validation-formula-mark-dirty",handler(){return!0}},j="sheets-formula.base.config";var Y=(r=>(r[r.FORCED=0]="FORCED",r[r.WHEN_EMPTY=1]="WHEN_EMPTY",r[r.NO_CALCULATION=2]="NO_CALCULATION",r))(Y||{});const yt={},Yt="sheets-formula.remote.config",xt={};var $=(r=>(r[r.NOT_REGISTER=1]="NOT_REGISTER",r[r.SUCCESS=2]="SUCCESS",r[r.WAIT=3]="WAIT",r[r.ERROR=4]="ERROR",r))($||{}),wt=Object.getOwnPropertyDescriptor,Xt=(r,e,a,n)=>{for(var i=n>1?void 0:n?wt(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},Et=(r,e)=>(a,n)=>e(a,n,r);N.RegisterOtherFormulaService=class extends f.Disposable{constructor(a,n,i){super();h(this,"_formulaCacheMap",new Map);h(this,"_formulaChangeWithRange$",new H.Subject);h(this,"formulaChangeWithRange$",this._formulaChangeWithRange$.asObservable());h(this,"_formulaResult$",new H.Subject);h(this,"formulaResult$",this._formulaResult$.asObservable());h(this,"calculateStarted$",new H.BehaviorSubject(!1));this._commandService=a,this._activeDirtyManagerService=n,this._lifecycleService=i,this._initFormulaRegister(),this._initFormulaCalculationResultChange()}dispose(){super.dispose(),this._formulaChangeWithRange$.complete(),this._formulaResult$.complete(),this.calculateStarted$.complete()}_ensureCacheMap(a,n){let i=this._formulaCacheMap.get(a);i||(i=new Map,this._formulaCacheMap.set(a,i));let o=i.get(n);return o||(o=new Map,i.set(n,o)),o}_createFormulaId(a,n){return`formula.${a}_${n}_${f.generateRandomId(8)}`}_initFormulaRegister(){this._activeDirtyManagerService.register(Q.id,{commandId:Q.id,getDirtyData(n){return{dirtyUnitOtherFormulaMap:n.params}}});const a=n=>{const{unitId:i,subUnitId:o,formulaText:u,formulaId:c,ranges:m}=n;if(!this._ensureCacheMap(i,o).has(c))return;const d={unitId:i,subUnitId:o,formulaMap:{[c]:{f:u,ranges:m}}};this._commandService.executeCommand(t.SetOtherFormulaMutation.id,d).then(()=>{this._commandService.executeCommand(Q.id,{[i]:{[o]:{[c]:!0}}})})};this.disposeWithMe(this._formulaChangeWithRange$.pipe(H.bufferWhen(()=>this.calculateStarted$.pipe(H.filter(n=>n)))).subscribe(n=>n.forEach(a))),this.disposeWithMe(this._formulaChangeWithRange$.pipe(H.filter(()=>this.calculateStarted$.getValue())).subscribe(a))}_initFormulaCalculationResultChange(){this.disposeWithMe(this._commandService.onCommandExecuted(a=>{if(a.id===t.SetFormulaCalculationResultMutation.id){const n=a.params,{unitOtherData:i}=n,o={};for(const u in i){const c=i[u],m={};o[u]=m;for(const l in c){const d=this._ensureCacheMap(u,l),T=c[l],E=[];m[l]=E;for(const L in T){const S=T[L];if(d.has(L)){const C=d.get(L);if(!C)continue;C.result||(C.result={});const R=new f.ObjectMatrix(S),p=new f.ObjectMatrix(C.result);R.forValue((A,I,P)=>{p.setValue(A,I,P)}),C.status=$.SUCCESS,C.callbacks.forEach(A=>{A(S)}),C.callbacks.clear(),E.push(C)}}}}this._formulaResult$.next(o)}}))}registerFormulaWithRange(a,n,i,o=[{startRow:0,endRow:0,startColumn:0,endColumn:0}],u){const c=this._createFormulaId(a,n);return this._ensureCacheMap(a,n).set(c,{result:void 0,status:$.WAIT,formulaId:c,callbacks:new Set,extra:u}),this._formulaChangeWithRange$.next({unitId:a,subUnitId:n,formulaText:i,formulaId:c,ranges:o}),c}deleteFormula(a,n,i){const o={unitId:a,subUnitId:n,formulaIdList:i};this._commandService.executeCommand(t.RemoveOtherFormulaMutation.id,o);const u=this._ensureCacheMap(a,n);i.forEach(c=>u.delete(c))}getFormulaValue(a,n,i){const o=this._ensureCacheMap(a,n),u=o.get(i);return u?u.status===$.SUCCESS||u.status===$.ERROR?Promise.resolve(u):new Promise(c=>{u.callbacks.add(()=>{c(o.get(i))})}):Promise.resolve(null)}getFormulaValueSync(a,n,i){return this._ensureCacheMap(a,n).get(i)}markFormulaDirty(a,n,i){const o=this.getFormulaValueSync(a,n,i);o&&(o.status=$.WAIT,this._commandService.executeCommand(Q.id,{[a]:{[n]:{[i]:!0}}}))}},N.RegisterOtherFormulaService=Xt([Et(0,f.ICommandService),Et(1,t.IActiveDirtyManagerService),Et(2,f.Inject(f.LifecycleService))],N.RegisterOtherFormulaService);var Wt=Object.getOwnPropertyDescriptor,Kt=(r,e,a,n)=>{for(var i=n>1?void 0:n?Wt(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},g=(r,e)=>(a,n)=>e(a,n,r);const Ut={done:0,count:0},It={onlyLocal:!0};N.TriggerCalculationController=class extends f.Disposable{constructor(a,n,i,o,u,c,m){super();h(this,"_waitingCommandQueue",[]);h(this,"_executingDirtyData",{forceCalculation:!1,dirtyRanges:[],dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{}});h(this,"_setTimeoutKey",-1);h(this,"_startExecutionTime",0);h(this,"_totalCalculationTaskCount",0);h(this,"_doneCalculationTaskCount",0);h(this,"_executionInProgressParams",null);h(this,"_restartCalculation",!1);h(this,"_forceCalculating",!1);h(this,"_progress$",new H.BehaviorSubject(Ut));h(this,"progress$",this._progress$.asObservable());this._commandService=a,this._activeDirtyManagerService=n,this._logService=i,this._configService=o,this._formulaDataModel=u,this._localeService=c,this._registerOtherFormulaService=m,this._commandExecutedListener(),this._initialExecuteFormulaProcessListener(),this._initialExecuteFormula()}_emitProgress(a){this._progress$.next({done:this._doneCalculationTaskCount,count:this._totalCalculationTaskCount,label:a})}_startProgress(){this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=1;const a=this._localeService.t("formula.progress.analyzing");this._emitProgress(a)}_calculateProgress(a){if(this._executionInProgressParams){const{totalFormulasToCalculate:n,completedFormulasCount:i,totalArrayFormulasToCalculate:o,completedArrayFormulasCount:u}=this._executionInProgressParams;if(this._doneCalculationTaskCount=i+u,this._totalCalculationTaskCount=n+o,this._totalCalculationTaskCount===0)return;this._emitProgress(a)}}_completeProgress(){this._doneCalculationTaskCount=this._totalCalculationTaskCount=1;const a=this._localeService.t("formula.progress.done");this._emitProgress(a)}clearProgress(){this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=0,this._emitProgress()}dispose(){super.dispose(),this._progress$.next(Ut),this._progress$.complete(),clearTimeout(this._setTimeoutKey)}_getCalculationMode(){var n;const a=this._configService.getConfig(j);return(n=a==null?void 0:a.initialFormulaComputing)!=null?n:Y.WHEN_EMPTY}_commandExecutedListener(){this.disposeWithMe(this._commandService.beforeCommandExecuted(a=>{if(a.id===t.SetFormulaCalculationStartMutation.id){const n=a.params;n.rowData=this._formulaDataModel.getHiddenRowsFiltered()}})),this.disposeWithMe(this._commandService.onCommandExecuted((a,n)=>{if(this._activeDirtyManagerService.get(a.id)){if(a.id===s.SetRangeValuesMutation.id){const i=a.params;if(n&&n.onlyLocal===!0||i.trigger===s.SetStyleCommand.id||i.trigger===s.SetBorderCommand.id||i.trigger===s.ClearSelectionFormatCommand.id)return}this._waitingCommandQueue.push(a),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(t.SetFormulaCalculationStartMutation.id,{...this._executingDirtyData},It):(this._restartCalculation=!0,this._commandService.executeCommand(t.SetFormulaCalculationStopMutation.id,{})),this._waitingCommandQueue=[]},100)}}))}_generateDirty(a){const n=[],i={},o={},u={},c={},m={};for(const l of a){const d=this._activeDirtyManagerService.get(l.id);if(d==null)continue;const T=d.getDirtyData(l),{dirtyRanges:E,dirtyNameMap:L,dirtyDefinedNameMap:S,dirtyUnitFeatureMap:C,dirtyUnitOtherFormulaMap:R,clearDependencyTreeCache:p}=T;E!=null&&this._mergeDirtyRanges(n,E),L!=null&&this._mergeDirtyNameMap(i,L),S!=null&&this._mergeDirtyNameMap(o,S),C!=null&&this._mergeDirtyUnitFeatureOrOtherFormulaMap(u,C),R!=null&&this._mergeDirtyUnitFeatureOrOtherFormulaMap(c,R),p!=null&&this._mergeDirtyNameMap(m,p)}return{dirtyRanges:n,dirtyNameMap:i,dirtyDefinedNameMap:o,dirtyUnitFeatureMap:u,dirtyUnitOtherFormulaMap:c,forceCalculation:!1,clearDependencyTreeCache:m,maxIteration:this._configService.getConfig(t.ENGINE_FORMULA_CYCLE_REFERENCE_COUNT)}}_mergeDirty(a,n){const i=[...a.dirtyRanges,...n.dirtyRanges],o={...a.dirtyNameMap},u={...a.dirtyDefinedNameMap},c={...a.dirtyUnitFeatureMap},m={...a.dirtyUnitOtherFormulaMap},l={...a.clearDependencyTreeCache};this._mergeDirtyNameMap(o,n.dirtyNameMap),this._mergeDirtyNameMap(u,n.dirtyDefinedNameMap),this._mergeDirtyUnitFeatureOrOtherFormulaMap(c,n.dirtyUnitFeatureMap),this._mergeDirtyUnitFeatureOrOtherFormulaMap(m,n.dirtyUnitOtherFormulaMap),this._mergeDirtyNameMap(l,n.clearDependencyTreeCache);const d=a.maxIteration||n.maxIteration;return{dirtyRanges:i,dirtyNameMap:o,dirtyDefinedNameMap:u,dirtyUnitFeatureMap:c,dirtyUnitOtherFormulaMap:m,forceCalculation:!!this._forceCalculating,clearDependencyTreeCache:l,maxIteration:d}}_mergeDirtyRanges(a,n){for(const i of n){let o=!1;for(const u of a)if(i.unitId===u.unitId&&i.sheetId===u.sheetId){const{startRow:c,startColumn:m,endRow:l,endColumn:d}=i.range,{startRow:T,startColumn:E,endRow:L,endColumn:S}=u.range;if(c===T&&m===E&&l===L&&d===S){o=!0;break}}o||a.push(i)}}_mergeDirtyNameMap(a,n){Object.keys(n).forEach(i=>{a[i]==null&&(a[i]={}),Object.keys(n[i]).forEach(o=>{var u;(u=n[i])!=null&&u[o]&&(a[i][o]=n[i][o])})})}_mergeDirtyUnitFeatureOrOtherFormulaMap(a,n){Object.keys(n).forEach(i=>{a[i]==null&&(a[i]={}),Object.keys(n[i]).forEach(o=>{a[i][o]==null&&(a[i][o]={}),Object.keys(n[i][o]).forEach(u=>{a[i][o][u]=n[i][o][u]||!1})})})}_initialExecuteFormulaProcessListener(){let a=null,n=0;this.disposeWithMe(this._commandService.onCommandExecuted(i=>{if(i.id===t.SetFormulaCalculationStartMutation.id){const{forceCalculation:u=!1}=i.params;u&&(this._forceCalculating=!0)}else i.id===t.SetFormulaCalculationStopMutation.id&&this.clearProgress();if(i.id!==t.SetFormulaCalculationNotificationMutation.id)return;const o=i.params;if(o.stageInfo!=null){const{stage:u}=o.stageInfo;if(u===t.FormulaExecuteStageType.START)n===0&&(this._startExecutionTime=performance.now()),n++,a!==null&&(clearTimeout(a),a=null),a=setTimeout(()=>{a=null,this._startProgress()},1e3);else if(u===t.FormulaExecuteStageType.CURRENTLY_CALCULATING){if(this._executionInProgressParams=o.stageInfo,a===null){const c=this._localeService.t("formula.progress.calculating");this._calculateProgress(c)}}else if(u===t.FormulaExecuteStageType.START_DEPENDENCY_ARRAY_FORMULA){if(this._executionInProgressParams=o.stageInfo,a===null){const c=this._localeService.t("formula.progress.array-analysis");this._calculateProgress(c)}}else if(u===t.FormulaExecuteStageType.CURRENTLY_CALCULATING_ARRAY_FORMULA&&(this._executionInProgressParams=o.stageInfo,a===null)){const c=this._localeService.t("formula.progress.array-calculation");this._calculateProgress(c)}}else{const u=o.functionsExecutedState;let c="";switch(n--,u){case t.FormulaExecutedStateType.NOT_EXECUTED:c="No tasks are being executed anymore",this._resetExecutingDirtyData();break;case t.FormulaExecutedStateType.STOP_EXECUTION:c="The execution of the formula has been stopped",n=0;break;case t.FormulaExecutedStateType.SUCCESS:c="Formula calculation succeeded",(n===0||n===-1)&&(c+=`. Total time consumed: ${performance.now()-this._startExecutionTime} ms`),this._resetExecutingDirtyData();break;case t.FormulaExecutedStateType.INITIAL:c="Waiting for calculation",this._resetExecutingDirtyData();break}(n===0||n===-1)&&(a?(clearTimeout(a),a=null,this.clearProgress()):this._completeProgress(),n=0,this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=0,this._forceCalculating=!1),u===t.FormulaExecutedStateType.STOP_EXECUTION&&this._restartCalculation?(this._restartCalculation=!1,this._commandService.executeCommand(t.SetFormulaCalculationStartMutation.id,{...this._executingDirtyData},It)):this._executionInProgressParams=null,this._logService.debug("[TriggerCalculationController]",c)}}))}_resetExecutingDirtyData(){this._executingDirtyData={dirtyRanges:[],dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},forceCalculation:!1,clearDependencyTreeCache:{}}}_initialExecuteFormula(){const a=this._getCalculationMode(),n=this._getDirtyDataByCalculationMode(a);this._commandService.executeCommand(t.SetFormulaCalculationStartMutation.id,n,It),this._registerOtherFormulaService.calculateStarted$.next(!0)}_getDirtyDataByCalculationMode(a){const n=a===Y.FORCED,i=a===Y.WHEN_EMPTY?this._formulaDataModel.getFormulaDirtyRanges():[];return{forceCalculation:n,dirtyRanges:i,dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{},maxIteration:this._configService.getConfig(t.ENGINE_FORMULA_CYCLE_REFERENCE_COUNT)}}},N.TriggerCalculationController=Kt([g(0,f.ICommandService),g(1,t.IActiveDirtyManagerService),g(2,f.ILogService),g(3,f.IConfigService),g(4,f.Inject(t.FormulaDataModel)),g(5,f.Inject(f.LocaleService)),g(6,f.Inject(N.RegisterOtherFormulaService))],N.TriggerCalculationController);function gt(r,e,a){var n;return r==null||r[e]==null||((n=r[e])==null?void 0:n[a])==null}function At(r,e,a){var n;if(a){if(r&&r[e]&&((n=r[e])!=null&&n[a]))return delete r[e][a],{[e]:{[a]:null}}}else if(r&&r[e])return delete r[e],{[e]:null}}var D=(r=>(r[r.MoveRange=0]="MoveRange",r[r.MoveRows=1]="MoveRows",r[r.MoveCols=2]="MoveCols",r[r.InsertRow=3]="InsertRow",r[r.InsertColumn=4]="InsertColumn",r[r.RemoveRow=5]="RemoveRow",r[r.RemoveColumn=6]="RemoveColumn",r[r.DeleteMoveLeft=7]="DeleteMoveLeft",r[r.DeleteMoveUp=8]="DeleteMoveUp",r[r.InsertMoveDown=9]="InsertMoveDown",r[r.InsertMoveRight=10]="InsertMoveRight",r[r.SetName=11]="SetName",r[r.RemoveSheet=12]="RemoveSheet",r[r.SetDefinedName=13]="SetDefinedName",r[r.RemoveDefinedName=14]="RemoveDefinedName",r))(D||{});const kt=[11,12,13,14];function Qt(r,e,a){const{type:n}=a;return kt.includes(n)?jt(r,e):$t(r,e,a)}function jt(r,e){const a=[],n=[];return Object.keys(e).forEach(i=>{const o=e[i],u=r[i];if(o==null||u==null)return!0;Object.keys(o).forEach(c=>{const m=new f.ObjectMatrix(o[c]||{}),l=new f.ObjectMatrix(u[c]||{}),d=new f.ObjectMatrix,T=new f.ObjectMatrix;if(m.forValue((R,p,A)=>{if(A==null)return!0;const I=nt(A);I!==null&&(d.setValue(R,p,I),T.setValue(R,p,l.getValue(R,p)))}),d.getSizeOf()===0)return;const E={subUnitId:c,unitId:i,cellValue:d.getMatrix()},L={id:s.SetRangeValuesMutation.id,params:E};n.push(L);const S={subUnitId:c,unitId:i,cellValue:T.getMatrix()},C={id:s.SetRangeValuesMutation.id,params:S};a.push(C)})}),{undos:a,redos:n}}function $t(r,e,a){const{redoFormulaData:n,undoFormulaData:i}=zt(r,e,a),o=[],u=[];return Object.keys(n).forEach(c=>{Object.keys(n[c]).forEach(m=>{if(Object.keys(n[c][m]).length!==0){const l={subUnitId:m,unitId:c,cellValue:n[c][m]},d={id:s.SetRangeValuesMutation.id,params:l};o.push(d)}})}),Object.keys(i).forEach(c=>{Object.keys(i[c]).forEach(m=>{if(Object.keys(i[c][m]).length!==0){const l={subUnitId:m,unitId:c,cellValue:i[c][m]},d={id:s.SetRangeValuesMutation.id,params:l};u.push(d)}})}),{undos:u,redos:o}}function zt(r,e,a){const n={},i={},{unitId:o,sheetId:u}=a;return new Set([...Object.keys(r),...Object.keys(e)]).forEach(m=>{if(gt(r,m,u))return;new Set([...Object.keys(r[m]||{}),...Object.keys(e[m]||{})]).forEach(d=>{var A,I;const T=(A=r[m])==null?void 0:A[d],E=(I=e[m])==null?void 0:I[d],L=new f.ObjectMatrix(T||{}),S=new f.ObjectMatrix(E||{});let C=[];m!==o||d!==u?C=Jt(S):C=Zt(L,a);const R=pe(C,L,S),p=de(C,L);n[m]||(n[m]={}),i[m]||(i[m]={}),n[m][d]={...n[m][d],...R},i[m][d]={...i[m][d],...p}})}),{redoFormulaData:n,undoFormulaData:i}}function Zt(r,e){const{type:a,from:n,to:i,range:o}=e,u=[];return r.forValue((c,m,l)=>{if(l==null||!qt(l))return!0;const d=f.cellToRange(c,m);let T=null,E=!1;if([0,1,2].includes(a))T=te(a,n,i,d);else if(o!=null){const L=ee(d,e);T=L.newCell,E=L.isReverse}E?u.unshift({oldCell:d,newCell:T}):u.push({oldCell:d,newCell:T})}),u}function Jt(r){const e=[];return r.forValue((a,n,i)=>{if(i==null||!qt(i))return!0;const o=f.cellToRange(a,n);e.push({oldCell:o,newCell:o})}),e}function te(r,e,a,n){if(e==null||a==null)return null;switch(r){case 0:return ae(e,a,n);case 1:return ne(e,a,n);case 2:return ie(e,a,n);default:return null}}function ee(r,e){const{type:a,rangeFilteredRows:n}=e,i=e.range;let o=null,u=!1;switch(a){case 3:o=re(i,r),u=!0;break;case 4:o=oe(i,r),u=!0;break;case 5:o=ue(i,r,n);break;case 6:o=ce(i,r);break;case 7:o=fe(i,r);break;case 8:o=me(i,r);break;case 9:o=se(i,r),u=!0;break;case 10:o=le(i,r),u=!0;break}return{newCell:o,isReverse:u}}function ae(r,e,a){const n=s.handleMoveRange({id:s.EffectRefRangId.MoveRangeCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function ne(r,e,a){const n=s.handleMoveRows({id:s.EffectRefRangId.MoveRowsCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function ie(r,e,a){const n=s.handleMoveCols({id:s.EffectRefRangId.MoveColsCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function re(r,e){const a=s.handleInsertRow({id:s.EffectRefRangId.InsertRowCommandId,params:{range:r,unitId:"",subUnitId:"",direction:f.Direction.DOWN}},e);return s.runRefRangeMutations(a,e)}function oe(r,e){const a=s.handleInsertCol({id:s.EffectRefRangId.InsertColCommandId,params:{range:r,unitId:"",subUnitId:"",direction:f.Direction.RIGHT}},e);return s.runRefRangeMutations(a,e)}function ue(r,e,a){const n=s.handleIRemoveRow({id:s.EffectRefRangId.RemoveRowCommandId,params:{range:r}},e,a);return s.runRefRangeMutations(n,e)}function ce(r,e){const a=s.handleIRemoveCol({id:s.EffectRefRangId.RemoveColCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function fe(r,e){const a=s.handleDeleteRangeMoveLeft({id:s.EffectRefRangId.DeleteRangeMoveLeftCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function me(r,e){const a=s.handleDeleteRangeMoveUp({id:s.EffectRefRangId.DeleteRangeMoveUpCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function se(r,e){const a=s.handleInsertRangeMoveDown({id:s.EffectRefRangId.InsertRangeMoveDownCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function le(r,e){const a=s.handleInsertRangeMoveRight({id:s.EffectRefRangId.InsertRangeMoveRightCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function pe(r,e,a){var i,o,u;const n=new f.ObjectMatrix({});for(let c=0;c<r.length;c++){const{oldCell:m,newCell:l}=r[c];if((i=n.getValue(m.startRow,m.startColumn))!=null&&i.f||(o=n.getValue(m.startRow,m.startColumn))!=null&&o.si||n.setValue(m.startRow,m.startColumn,{f:null,si:null}),l){const d=(u=a.getValue(m.startRow,m.startColumn))!=null?u:e.getValue(m.startRow,m.startColumn),T=nt(d);n.setValue(l.startRow,l.startColumn,T)}}return n.getMatrix()}function de(r,e){const a=new f.ObjectMatrix({});for(let n=r.length-1;n>=0;n--){const{oldCell:i,newCell:o}=r[n],u=e.getValue(i.startRow,i.startColumn),c=nt(u);if(a.setValue(i.startRow,i.startColumn,c),o){const m=e.getValue(o.startRow,o.startColumn),l=nt(m);a.setValue(o.startRow,o.startColumn,l!=null?l:{f:null,si:null})}}return a.getMatrix()}function nt(r){if(r==null)return;const{f:e,si:a,x:n=0,y:i=0}=r,o=f.isFormulaString(e),u=f.isFormulaId(a);if(!o&&!u)return{f:null,si:null};const c={};return u&&(c.si=a),o&&n===0&&i===0&&(c.f=e),c.f===void 0&&(c.f=null),c.si===void 0&&(c.si=null),c}function Le(r){const e=new f.ObjectMatrix({});return new f.ObjectMatrix(r).forValue((n,i,o)=>{const u=nt(o);u&&e.setValue(n,i,u)}),e.getMatrix()}function qt(r){const e=(r==null?void 0:r.f)||"",a=(r==null?void 0:r.si)||"",n=f.isFormulaString(e),i=f.isFormulaId(a);return!!(n||i)}function ht(r,e,a,n,i,o){if((i==null||i.length===0)&&(o==null||o.length===0)){if(r===a&&e===n)return!0}else if((r===i||i==null||i.length===0)&&e===o)return!0;return!1}function Ft(r,e,a=0,n=0){const i=[];for(let o=0,u=r.length;o<u;o++){const c=r[o];if(typeof c=="string"||c.nodeType!==t.sequenceNodeType.REFERENCE||e.includes(o)){i.push(c);continue}const{token:m}=c,l=t.deserializeRangeWithSheetWithCache(m),{range:d,sheetName:T,unitId:E}=l,L=f.Rectangle.moveOffset(d,a,n);i.push({...c,token:t.serializeRangeToRefString({range:L,unitId:E,sheetName:T})})}return i}function Bt(r,e,a,n){const{type:i,unitId:o,sheetId:u,range:c,from:m,to:l,rangeFilteredRows:d}=e,{range:T,sheetId:E,unitId:L,sheetName:S,refOffsetX:C,refOffsetY:R}=r;if(!ht(o,u,a,n,L,E))return;const p=f.Rectangle.moveOffset(T,C,R);let A=null;if(i===D.MoveRange){if(m==null||l==null)return;const I=Ct(p,m),P=f.getIntersectRange(p,m);if(P==null||I!==4)return;const O=s.handleMoveRange({id:s.EffectRefRangId.MoveRangeCommandId,params:{toRange:l,fromRange:m}},P),b=s.runRefRangeMutations(O,P);if(b==null)return t.ErrorType.REF;A=St(I,b,m,l,p,P)}else if(i===D.MoveRows){if(m==null||l==null)return;const I=Ct(p,m);let P=f.getIntersectRange(p,m);if(P==null&&(m.endRow<p.startRow&&l.endRow<=p.startRow||m.startRow>p.endRow&&l.startRow>p.endRow))return;P==null&&(P={startRow:p.startRow,endRow:p.endRow,startColumn:p.startColumn,endColumn:p.endColumn,rangeType:f.RANGE_TYPE.NORMAL});const O=s.handleMoveRows({id:s.EffectRefRangId.MoveRowsCommandId,params:{toRange:l,fromRange:m}},P),b=s.runRefRangeMutations(O,P);if(b==null)return t.ErrorType.REF;A=St(I,b,m,l,p,P)}else if(i===D.MoveCols){if(m==null||l==null)return;const I=Ct(p,m);let P=f.getIntersectRange(p,m);if(P==null&&(m.endColumn<p.startColumn&&l.endColumn<=p.startColumn||m.startColumn>p.endColumn&&l.startColumn>p.endColumn))return;P==null&&(P={startRow:p.startRow,endRow:p.endRow,startColumn:p.startColumn,endColumn:p.endColumn,rangeType:f.RANGE_TYPE.NORMAL});const O=s.handleMoveCols({id:s.EffectRefRangId.MoveColsCommandId,params:{toRange:l,fromRange:m}},P),b=s.runRefRangeMutations(O,P);if(b==null)return t.ErrorType.REF;A=St(I,b,m,l,p,P)}if(c!=null){if(i===D.InsertRow){const I=s.handleInsertRow({id:s.EffectRefRangId.InsertRowCommandId,params:{range:c,unitId:"",subUnitId:"",direction:f.Direction.DOWN}},p),P=s.runRefRangeMutations(I,p);if(P==null)return;A={...p,...P}}else if(i===D.InsertColumn){const I=s.handleInsertCol({id:s.EffectRefRangId.InsertColCommandId,params:{range:c,unitId:"",subUnitId:"",direction:f.Direction.RIGHT}},p),P=s.runRefRangeMutations(I,p);if(P==null)return;A={...p,...P}}else if(i===D.RemoveRow){const I=s.handleIRemoveRow({id:s.EffectRefRangId.RemoveRowCommandId,params:{range:c}},p,d),P=s.runRefRangeMutations(I,p);if(P==null)return t.ErrorType.REF;A={...p,...P}}else if(i===D.RemoveColumn){const I=s.handleIRemoveCol({id:s.EffectRefRangId.RemoveColCommandId,params:{range:c}},p),P=s.runRefRangeMutations(I,p);if(P==null)return t.ErrorType.REF;A={...p,...P}}else if(i===D.DeleteMoveLeft){const I=s.handleDeleteRangeMoveLeft({id:s.EffectRefRangId.DeleteRangeMoveLeftCommandId,params:{range:c}},p),P=s.runRefRangeMutations(I,p);if(P==null)return t.ErrorType.REF;A={...p,...P}}else if(i===D.DeleteMoveUp){const I=s.handleDeleteRangeMoveUp({id:s.EffectRefRangId.DeleteRangeMoveUpCommandId,params:{range:c}},p),P=s.runRefRangeMutations(I,p);if(P==null)return t.ErrorType.REF;A={...p,...P}}else if(i===D.InsertMoveDown){const I=s.handleInsertRangeMoveDown({id:s.EffectRefRangId.InsertRangeMoveDownCommandId,params:{range:c}},p),P=s.runRefRangeMutations(I,p);if(P==null)return;A={...p,...P}}else if(i===D.InsertMoveRight){const I=s.handleInsertRangeMoveRight({id:s.EffectRefRangId.InsertRangeMoveRightCommandId,params:{range:c}},p),P=s.runRefRangeMutations(I,p);if(P==null)return;A={...p,...P}}}if(A!=null)return t.serializeRangeToRefString({range:A,sheetName:S,unitId:L})}function St(r,e,a,n,i,o){const{startRow:u,endRow:c,startColumn:m,endColumn:l,rangeType:d}=it(e),{startRow:T,startColumn:E,endRow:L,endColumn:S,rangeType:C=f.RANGE_TYPE.NORMAL}=it(a),{startRow:R,startColumn:p,endRow:A,endColumn:I}=it(n),{startRow:P,endRow:O,startColumn:b,endColumn:q}=it(o),{startRow:y,endRow:x,startColumn:_,endColumn:U,rangeType:F=f.RANGE_TYPE.NORMAL}=it(i),M={...i};function B(){return d===f.RANGE_TYPE.COLUMN&&F===f.RANGE_TYPE.COLUMN?!0:m>=_&&l<=U}function J(){return d===f.RANGE_TYPE.ROW&&F===f.RANGE_TYPE.ROW?!0:u>=y&&c<=x}if(r===0)if(B())if(u<y)M.startRow=u;else if(u>=x)M.endRow-=L+1-y;else return;else return;else if(r===1)if(B())if(c>x)M.endRow=c;else if(c<=y)M.startRow+=x-T+1;else return;else return;else if(r===2)if(J())if(m<_)M.startColumn=m;else if(m>=U)M.endColumn-=S+1-_;else return;else return;else if(r===3)if(J())if(l>U)M.endColumn=l;else if(l<=_)M.startColumn+=U-E+1;else return;else return;else r===4?(M.startRow=u,M.startColumn=m,M.endRow=c,M.endColumn=l):E<=_&&S>=U||C===f.RANGE_TYPE.ROW&&F===f.RANGE_TYPE.ROW?a.endRow<y?(R>=y&&(M.startRow-=L-T+1),R>=x&&(M.endRow-=L-T+1)):a.startRow>x?(A<=x&&(M.endRow+=L-T+1),A<=y&&(M.startRow+=L-T+1)):a.startRow>=y&&a.endRow<=x&&(R<=y?M.startRow+=L-T+1:R>=x&&(M.endRow-=L-T+1)):T<=y&&L>=x||C===f.RANGE_TYPE.COLUMN&&F===f.RANGE_TYPE.COLUMN?a.endColumn<_?(p>=_&&(M.startColumn-=S-E+1),p>=U&&(M.endColumn-=S-E+1)):a.startColumn>U?(I<=U&&(M.endColumn+=S-E+1),I<=_&&(M.startColumn+=S-E+1)):a.startColumn>=_&&a.endColumn<=U&&(p<=_?M.startColumn+=S-E+1:p>=U&&(M.endColumn-=S-E+1)):((p<=q+1&&I>=U||p<=_&&I>=b-1)&&R<=y&&A>=x||(R<=O+1&&A>=x||R<=y&&A>=P-1)&&p<=_&&I>=U,M.startRow=u,M.startColumn=m,M.endRow=c,M.endColumn=l);return M}function Ct(r,e){const a=z(r.startRow),n=Z(r.endRow),i=z(r.startColumn),o=Z(r.endColumn),u=z(e.startRow),c=Z(e.endRow),m=z(e.startColumn),l=Z(e.endColumn);function d(){return r.rangeType===f.RANGE_TYPE.COLUMN&&e.rangeType===f.RANGE_TYPE.COLUMN?!0:a>=u&&n<=c}function T(){return r.rangeType===f.RANGE_TYPE.ROW&&e.rangeType===f.RANGE_TYPE.ROW?!0:i>=m&&o<=l}function E(){return r.rangeType===f.RANGE_TYPE.ALL&&e.rangeType===f.RANGE_TYPE.ALL}return d()&&T()||E()?4:T()&&a>=u&&a<=c&&n>c?0:T()&&n>=u&&n<=c&&a<u?1:d()&&i>=m&&i<=l&&o>l?2:d()&&o>=m&&o<=l&&i<m?3:null}function z(r){return isNaN(r)?-1/0:r}function Z(r){return isNaN(r)?1/0:r}function it(r){const{startRow:e,endRow:a,startColumn:n,endColumn:i}=r;return{...r,startRow:z(e),endRow:Z(a),startColumn:z(n),endColumn:Z(i)}}function vt(r,e){const{id:a}=e;let n=null;switch(a){case s.MoveRangeCommand.id:n=Te(e,r);break;case s.MoveRowsCommand.id:n=Ne(e,r);break;case s.MoveColsCommand.id:n=Pe(e,r);break;case s.InsertRowCommand.id:n=Ee(e);break;case s.InsertColCommand.id:n=Ie(e);break;case s.InsertRangeMoveRightCommand.id:n=Ae(e,r);break;case s.InsertRangeMoveDownCommand.id:n=Se(e,r);break;case s.RemoveRowCommand.id:n=Ce(e,r);break;case s.RemoveColCommand.id:n=Re(e,r);break;case s.DeleteRangeMoveUpCommand.id:n=Oe(e,r);break;case s.DeleteRangeMoveLeftCommand.id:n=Me(e,r);break;case s.SetWorksheetNameCommand.id:n=_e(e,r);break;case s.RemoveSheetCommand.id:n=De(e,r);break;case s.SetDefinedNameCommand.id:n=be(e,r);break;case s.RemoveDefinedNameCommand.id:n=ye(e,r);break}return n}function V(r){var n;const e=r.getUnitId(),a=((n=r.getActiveSheet())==null?void 0:n.getSheetId())||"";return{unitId:e,sheetId:a}}function Te(r,e){const{params:a}=r;if(!a)return null;const{fromRange:n,toRange:i}=a;if(!n||!i)return null;const{unitId:o,sheetId:u}=V(e);return{type:D.MoveRange,from:n,to:i,unitId:o,sheetId:u}}function Ne(r,e){const{params:a}=r;if(!a)return null;const{fromRange:{startRow:n,endRow:i},toRange:{startRow:o,endRow:u}}=a,c=e.getUnitId(),m=e.getActiveSheet();if(!m)return null;const l=m.getSheetId(),d={startRow:n,startColumn:0,endRow:i,endColumn:m.getColumnCount()-1,rangeType:f.RANGE_TYPE.ROW},T={startRow:o,startColumn:0,endRow:u,endColumn:m.getColumnCount()-1,rangeType:f.RANGE_TYPE.ROW};return{type:D.MoveRows,from:d,to:T,unitId:c,sheetId:l}}function Pe(r,e){const{params:a}=r;if(!a)return null;const{fromRange:{startColumn:n,endColumn:i},toRange:{startColumn:o,endColumn:u}}=a,c=e.getUnitId(),m=e.getActiveSheet();if(!m)return null;const l=m.getSheetId(),d={startRow:0,startColumn:n,endRow:m.getRowCount()-1,endColumn:i,rangeType:f.RANGE_TYPE.COLUMN},T={startRow:0,startColumn:o,endRow:m.getRowCount()-1,endColumn:u,rangeType:f.RANGE_TYPE.COLUMN};return{type:D.MoveCols,from:d,to:T,unitId:c,sheetId:l}}function Ee(r){const{params:e}=r;if(!e)return null;const{range:a,unitId:n,subUnitId:i}=e;return{type:D.InsertRow,range:a,unitId:n,sheetId:i}}function Ie(r){const{params:e}=r;if(!e)return null;const{range:a,unitId:n,subUnitId:i}=e;return{type:D.InsertColumn,range:a,unitId:n,sheetId:i}}function Ae(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.InsertMoveRight,range:n,unitId:i,sheetId:o}}function Se(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.InsertMoveDown,range:n,unitId:i,sheetId:o}}function Ce(r,e){var u,c;const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.RemoveRow,range:n,unitId:i,sheetId:o,rangeFilteredRows:(c=(u=e.getSheetBySheetId(o))==null?void 0:u.getRangeFilterRows(n))!=null?c:[]}}function Re(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.RemoveColumn,range:n,unitId:i,sheetId:o}}function Oe(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.DeleteMoveUp,range:n,unitId:i,sheetId:o}}function Me(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=V(e);return{type:D.DeleteMoveLeft,range:n,unitId:i,sheetId:o}}function _e(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,subUnitId:i,name:o}=a,{unitId:u,sheetId:c}=V(e);return{type:D.SetName,unitId:n||u,sheetId:i||c,sheetName:o}}function De(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,subUnitId:i}=a,{unitId:o,sheetId:u}=V(e);return{type:D.RemoveSheet,unitId:n||o,sheetId:i||u}}function be(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,name:i,id:o}=a,{sheetId:u}=V(e);return{type:D.SetDefinedName,unitId:n,sheetId:u,definedName:i,definedNameId:o}}function ye(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,name:i,id:o}=a,{sheetId:u}=V(e);return{type:D.RemoveDefinedName,unitId:n,sheetId:u,definedName:i,definedNameId:o}}var xe=Object.getOwnPropertyDescriptor,Ue=(r,e,a,n)=>{for(var i=n>1?void 0:n?xe(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},ot=(r,e)=>(a,n)=>e(a,n,r);N.UpdateDefinedNameController=class extends f.Disposable{constructor(e,a,n,i){super(),this._definedNamesService=e,this._univerInstanceService=a,this._sheetInterceptorService=n,this._lexerTreeBuilder=i,this._initialize()}_initialize(){this._commandExecutedListener()}_commandExecutedListener(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===s.SetDefinedNameCommand.id||e.id===s.RemoveDefinedNameCommand.id)return{redos:[],undos:[]};const a=this._univerInstanceService.getCurrentUnitForType(f.UniverInstanceType.UNIVER_SHEET);if(a==null)return{redos:[],undos:[]};const n=vt(a,e);return n?this._getUpdateDefinedNameMutations(a,n):{redos:[],undos:[]}}}))}_getUpdateDefinedNameMutations(e,a){const{type:n,unitId:i,sheetId:o}=a,u=this._definedNamesService.getDefinedNameMap(i);if(!u)return{redos:[],undos:[]};const c=[],m=[];return Object.values(u).forEach(l=>{var p;const{formulaOrRefString:d}=l,T=this._lexerTreeBuilder.sequenceNodesBuilder(d);if(T==null)return!0;let E=!1;const L=[];for(let A=0,I=T.length;A<I;A++){const P=T[A];if(typeof P=="string"||P.nodeType!==t.sequenceNodeType.REFERENCE)continue;const{token:O}=P,b=t.deserializeRangeWithSheetWithCache(O),{range:q,sheetName:y,unitId:x}=b,_=((p=e.getSheetBySheetName(y))==null?void 0:p.getSheetId())||"",U={range:q,sheetId:_,unitId:x,sheetName:y,refOffsetX:0,refOffsetY:0};let F=null;if(n===D.RemoveSheet)F=this._removeSheet(l,i,o);else if(n===D.SetName){const{sheetId:M,sheetName:B}=a;if(B==null||_==null||_.length===0||M!==_)continue;F=t.serializeRangeToRefString({range:q,sheetName:B,unitId:x})}else F=Bt(U,a,i,o);F!=null&&(T[A]={...P,token:F},E=!0,L.push(A))}if(!E)return!0;const S=t.generateStringWithSequence(Ft(T,L)),C={id:t.SetDefinedNameMutation.id,params:{unitId:i,...l,formulaOrRefString:S}};c.push(C);const R={id:t.SetDefinedNameMutation.id,params:{unitId:i,...l}};m.push(R)}),{redos:c,undos:m}}_removeSheet(e,a,n){var u;const{formulaOrRefString:i}=e;return((u=this._definedNamesService.getWorksheetByRef(a,i))==null?void 0:u.getSheetId())===n?t.ErrorType.REF:null}},N.UpdateDefinedNameController=Ue([ot(0,t.IDefinedNamesService),ot(1,f.IUniverInstanceService),ot(2,f.Inject(s.SheetInterceptorService)),ot(3,f.Inject(t.LexerTreeBuilder))],N.UpdateDefinedNameController);var qe=Object.getOwnPropertyDescriptor,he=(r,e,a,n)=>{for(var i=n>1?void 0:n?qe(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},w=(r,e)=>(a,n)=>e(a,n,r);N.UpdateFormulaController=class extends f.Disposable{constructor(e,a,n,i,o,u,c,m){super(),this._univerInstanceService=e,this._commandService=a,this._lexerTreeBuilder=n,this._formulaDataModel=i,this._sheetInterceptorService=o,this._definedNamesService=u,this._configService=c,this._injector=m,this._commandExecutedListener()}_commandExecutedListener(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFormula(e)})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.params)if(e.id===s.RemoveSheetMutation.id){const{subUnitId:a,unitId:n}=e.params;this._handleWorkbookDisposed(n,a)}else e.id===s.InsertSheetMutation.id&&this._handleInsertSheetMutation(e.params)})),this.disposeWithMe(this._commandService.beforeCommandExecuted((e,a)=>{if(e.id===s.SetRangeValuesMutation.id){const n=e.params;if(a&&a.onlyLocal===!0||n.trigger===s.SetStyleCommand.id||n.trigger===s.SetBorderCommand.id||n.trigger===s.ClearSelectionFormatCommand.id)return;this._handleSetRangeValuesMutation(n)}})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(f.UniverInstanceType.UNIVER_SHEET).subscribe(e=>this._handleWorkbookAdded(e))),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(f.UniverInstanceType.UNIVER_SHEET).pipe(H.map(e=>e.getUnitId())).subscribe(e=>this._handleWorkbookDisposed(e)))}_handleSetRangeValuesMutation(e){const{subUnitId:a,unitId:n,cellValue:i}=e;if(i==null)return;const o=this._formulaDataModel.updateFormulaData(n,a,i),u={[n]:{[a]:o}};this._commandService.executeCommand(s.SetRangeValuesMutation.id,{unitId:n,subUnitId:a,cellValue:Le(o)},{onlyLocal:!0,fromFormula:!0}),this._formulaDataModel.updateArrayFormulaCellData(n,a,i),this._formulaDataModel.updateArrayFormulaRange(n,a,i),this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:u},{onlyLocal:!0}),this._commandService.executeCommand(t.SetArrayFormulaDataMutation.id,{arrayFormulaRange:this._formulaDataModel.getArrayFormulaRange(),arrayFormulaCellData:this._formulaDataModel.getArrayFormulaCellData()},{onlyLocal:!0,remove:!0})}_handleWorkbookDisposed(e,a){const n=this._formulaDataModel.getFormulaData(),i=At(n,e,a),o=this._formulaDataModel.getArrayFormulaRange(),u=At(o,e,a),c=this._formulaDataModel.getArrayFormulaCellData(),m=At(c,e,a);i&&this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:i},{onlyLocal:!0}),u&&m&&this._commandService.executeCommand(t.SetArrayFormulaDataMutation.id,{arrayFormulaRange:o,arrayFormulaCellData:c},{onlyLocal:!0})}_handleInsertSheetMutation(e){const{sheet:a,unitId:n}=e,i=this._formulaDataModel.getFormulaData(),{id:o,cellData:u}=a,c=new f.ObjectMatrix(u),m=t.initSheetFormulaData(i,n,o,c);this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:m},{onlyLocal:!0})}_handleWorkbookAdded(e){var l;const a={},n=e.getUnitId(),i={[n]:{}};e.getSheets().forEach(d=>{var S;const T=d.getCellMatrix(),E=d.getSheetId(),L=t.initSheetFormulaData(a,n,E,T);i[n][E]=(S=L[n])==null?void 0:S[E]}),this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:i},{onlyLocal:!0});const u=this._configService.getConfig(j),c=(l=u==null?void 0:u.initialFormulaComputing)!=null?l:Y.WHEN_EMPTY,m=this._getDirtyDataByCalculationMode(c);this._commandService.executeCommand(t.SetFormulaCalculationStartMutation.id,m,{onlyLocal:!0})}_getDirtyDataByCalculationMode(e){const a=e===Y.FORCED,n=e===Y.WHEN_EMPTY?this._formulaDataModel.getFormulaDirtyRanges():[];return{forceCalculation:a,dirtyRanges:n,dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{}}}_getUpdateFormula(e){const a=this._univerInstanceService.getCurrentUnitForType(f.UniverInstanceType.UNIVER_SHEET);if(!a)return{undos:[],redos:[]};const n=vt(a,e);if(n){const{unitSheetNameMap:i}=this._formulaDataModel.getCalculateData(),o=this._formulaDataModel.getFormulaData(),{newFormulaData:u}=this._getFormulaReferenceMoveInfo(o,i,n),{undos:c,redos:m}=Qt(o,u,n);return{undos:c,redos:m}}return{undos:[],redos:[]}}_getFormulaReferenceMoveInfo(e,a,n){if(!f.Tools.isDefine(e))return{newFormulaData:{},oldFormulaData:{}};const i=Object.keys(e);if(i.length===0)return{newFormulaData:{},oldFormulaData:{}};const o={},u={};for(const c of i){const m=e[c];if(m==null)continue;const l=Object.keys(m);f.Tools.isDefine(o[c])||(o[c]={}),f.Tools.isDefine(u[c])||(u[c]={});for(const d of l){const T=new f.ObjectMatrix(m[d]||{}),E=new f.ObjectMatrix;T.forValue((L,S,C)=>{var y;if(!C)return!0;const{f:R,x:p,y:A,si:I}=C,P=this._lexerTreeBuilder.sequenceNodesBuilder(R);if(P==null)return!0;let O=!1;const b=[];for(let x=0,_=P.length;x<_;x++){const U=P[x];if(typeof U=="string")continue;const{token:F,nodeType:M}=U,{type:B}=n;if((B===D.SetDefinedName||B===D.RemoveDefinedName)&&(M===t.sequenceNodeType.DEFINED_NAME||M===t.sequenceNodeType.FUNCTION)){const{definedNameId:et,definedName:at}=n;if(et===void 0||at===void 0)continue;const k=this._definedNamesService.getValueById(c,et);if(k==null||k.name!==F)continue;P[x]={...U,token:B===D.SetDefinedName?at:t.ErrorType.REF},O=!0,b.push(x);continue}else if(M!==t.sequenceNodeType.REFERENCE)continue;const J=t.deserializeRangeWithSheetWithCache(F),{range:tt,sheetName:X,unitId:v}=J,W=v==null||v.length===0?c:v,G=((y=a==null?void 0:a[W])==null?void 0:y[X])||"";if(!ht(n.unitId,n.sheetId,c,d,v,G))continue;const Pt={range:tt,sheetId:G,unitId:v,sheetName:X,refOffsetX:p||0,refOffsetY:A||0};let K=null;if(B===D.SetName){const{unitId:et,sheetId:at,sheetName:k}=n;if(k==null||G==null||G.length===0||at!==G)continue;K=t.serializeRangeToRefString({range:tt,sheetName:k,unitId:v})}else if(B===D.RemoveSheet){const{unitId:et,sheetId:at,sheetName:k}=n;if(G==null||G.length===0||at!==G)continue;K=t.ErrorType.REF}else B!==D.SetDefinedName&&(K=Bt(Pt,n,c,d));K!=null?(P[x]={...U,token:K},O=!0,b.push(x)):B===D.MoveRange&&I&&(O=!0)}if(!O)return!0;const q=Ft(P,b,p,A);E.setValue(L,S,{f:`=${t.generateStringWithSequence(q)}`})}),u[c]&&(u[c][d]=E.getData())}}return{newFormulaData:u}}},N.UpdateFormulaController=he([w(0,f.IUniverInstanceService),w(1,f.ICommandService),w(2,f.Inject(t.LexerTreeBuilder)),w(3,f.Inject(t.FormulaDataModel)),w(4,f.Inject(s.SheetInterceptorService)),w(5,t.IDefinedNamesService),w(6,f.IConfigService),w(7,f.Inject(f.Injector))],N.UpdateFormulaController);const Fe="SHEETS_FORMULA_PLUGIN";var Be=Object.getOwnPropertyDescriptor,ve=(r,e,a,n)=>{for(var i=n>1?void 0:n?Be(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},Rt=(r,e)=>(a,n)=>e(a,n,r);let ut=class extends f.Disposable{constructor(r,e,a){super(),this._activeDirtyManagerService=r,this._univerInstanceService=e,this._formulaDataModel=a,this._initialize()}_initialize(){this._initialConversion()}_initialConversion(){this._activeDirtyManagerService.register(s.SetRangeValuesMutation.id,{commandId:s.SetRangeValuesMutation.id,getDirtyData:r=>{const e=r.params;return e.trigger===s.SetStyleCommand.id?{}:{dirtyRanges:this._getSetRangeValuesMutationDirtyRange(e)}}}),this._initialMove(),this._initialRowAndColumn(),this._initialHideRow(),this._initialSheet(),this._initialDefinedName()}_initialMove(){this._activeDirtyManagerService.register(s.MoveRangeMutation.id,{commandId:s.MoveRangeMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRangeMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.to.subUnitId]:"1",[e.from.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.MoveRowsMutation.id,{commandId:s.MoveRowsMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRowsMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.MoveColsMutation.id,{commandId:s.MoveColsMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRowsMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.ReorderRangeMutation.id,{commandId:s.ReorderRangeMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getReorderRangeMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialRowAndColumn(){this._activeDirtyManagerService.register(s.RemoveRowMutation.id,{commandId:s.RemoveRowMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getRemoveRowOrColumnMutation(e,!0),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.RemoveColMutation.id,{commandId:s.RemoveColMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getRemoveRowOrColumnMutation(e,!1),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertColMutation.id,{commandId:s.InsertColMutation.id,getDirtyData:r=>{const e=r.params;return{clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertRowMutation.id,{commandId:s.InsertRowMutation.id,getDirtyData:r=>{const e=r.params;return{clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialHideRow(){this._activeDirtyManagerService.register(s.SetRowHiddenMutation.id,{commandId:s.SetRowHiddenMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getHideRowMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.SetRowVisibleMutation.id,{commandId:s.SetRowVisibleMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getHideRowMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialSheet(){this._activeDirtyManagerService.register(s.RemoveSheetMutation.id,{commandId:s.RemoveSheetMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyNameMap:this._getRemoveSheetMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertSheetMutation.id,{commandId:s.InsertSheetMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyNameMap:this._getInsertSheetMutation(e)}}})}_initialDefinedName(){this._activeDirtyManagerService.register(t.SetDefinedNameMutation.id,{commandId:t.SetDefinedNameMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyDefinedNameMap:this._getDefinedNameMutation(e)}}}),this._activeDirtyManagerService.register(t.RemoveDefinedNameMutation.id,{commandId:t.RemoveDefinedNameMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyDefinedNameMap:this._getDefinedNameMutation(e)}}})}_getDefinedNameMutation(r){const{unitId:e,name:a,formulaOrRefString:n}=r,i={};return r==null?{}:(i[e]={},i[e][a]=n,i)}_getSetRangeValuesMutationDirtyRange(r){const{subUnitId:e,unitId:a,cellValue:n}=r,i=[];return n==null||(i.push(...this._getDirtyRangesByCellValue(a,e,n)),i.push(...this._getDirtyRangesForArrayFormula(a,e,n))),i}_getMoveRangeMutationDirtyRange(r){const{unitId:e,from:a,to:n}=r,i=[];return i.push(...this._getDirtyRangesByCellValue(e,a.subUnitId,a.value)),i.push(...this._getDirtyRangesByCellValue(e,n.subUnitId,n.value)),i.push(...this._getDirtyRangesForArrayFormula(e,n.subUnitId,n.value)),i}_getMoveRowsMutationDirtyRange(r){const{subUnitId:e,unitId:a,sourceRange:n,targetRange:i}=r,o=[],u=this._rangeToMatrix(n).getData(),c=this._rangeToMatrix(i).getData();return o.push(...this._getDirtyRangesByCellValue(a,e,u)),o.push(...this._getDirtyRangesByCellValue(a,e,c)),o.push(...this._getDirtyRangesForArrayFormula(a,e,c)),o}_getReorderRangeMutationDirtyRange(r){const{unitId:e,subUnitId:a,range:n}=r,i=this._rangeToMatrix(n).getData(),o=[];return o.push(...this._getDirtyRangesByCellValue(e,a,i)),o.push(...this._getDirtyRangesForArrayFormula(e,a,i)),o}_getRemoveRowOrColumnMutation(r,e=!0){const{subUnitId:a,unitId:n,range:i}=r,o=[],u=this._univerInstanceService.getUniverSheetInstance(n),c=u==null?void 0:u.getSheetBySheetId(a),m=(c==null?void 0:c.getRowCount())||0,l=(c==null?void 0:c.getColumnCount())||0;let d=null;const{startRow:T,endRow:E,startColumn:L,endColumn:S}=i;e===!0?d=this._rangeToMatrix({startRow:T,startColumn:0,endRow:E,endColumn:l-1}):d=this._rangeToMatrix({startRow:0,startColumn:L,endRow:m,endColumn:S});const C=d.getData();return o.push(...this._getDirtyRangesByCellValue(n,a,C)),o.push(...this._getDirtyRangesForArrayFormula(n,a,C)),o}_getHideRowMutation(r){const{subUnitId:e,unitId:a,ranges:n}=r,i=[];return n.forEach(o=>{const u=this._rangeToMatrix(o).getMatrix();i.push(...this._getDirtyRangesByCellValue(a,e,u))}),i}_getRemoveSheetMutation(r){const e={},{subUnitId:a,unitId:n,subUnitName:i}=r;return e[n]==null&&(e[n]={}),e[n][a]=i,e}_getInsertSheetMutation(r){const e={},{sheet:a,unitId:n}=r;return e[n]==null&&(e[n]={}),e[n][a.id]=a.name,e}_rangeToMatrix(r){const e=new f.ObjectMatrix,{startRow:a,startColumn:n,endRow:i,endColumn:o}=r;for(let u=a;u<=i;u++)for(let c=n;c<=o;c++)e.setValue(u,c,{});return e}_getDirtyRangesByCellValue(r,e,a){const n=[];return a==null||new f.ObjectMatrix(a).getDiscreteRanges().forEach(u=>{n.push({unitId:r,sheetId:e,range:u})}),n}_getDirtyRangesForArrayFormula(r,e,a){var u,c;const n=[];if(a==null)return n;const i=new f.ObjectMatrix(a),o=this._formulaDataModel.getArrayFormulaRange();if((u=o==null?void 0:o[r])!=null&&u[e]){const m=new f.ObjectMatrix((c=o==null?void 0:o[r])==null?void 0:c[e]);i.forValue((l,d)=>{m.forValue((T,E,L)=>{if(L==null)return!0;const{startRow:S,startColumn:C,endRow:R,endColumn:p}=L;l>=S&&l<=R&&d>=C&&d<=p&&n.push({unitId:r,sheetId:e,range:{startRow:S,startColumn:C,endRow:S,endColumn:C}})})})}return n}};ut=ve([Rt(0,t.IActiveDirtyManagerService),Rt(1,f.IUniverInstanceService),Rt(2,f.Inject(t.FormulaDataModel))],ut);var Ve=Object.getOwnPropertyDescriptor,Ge=(r,e,a,n)=>{for(var i=n>1?void 0:n?Ve(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},ct=(r,e)=>(a,n)=>e(a,n,r);let ft=class extends f.Disposable{constructor(r,e,a,n){super(),this._commandService=r,this._configService=e,this._sheetInterceptorService=a,this._formulaDataModel=n,this._initialize()}_initialize(){this._commandExecutedListener(),this._initInterceptorCellContent()}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(r=>{var i;if(r.id!==t.SetArrayFormulaDataMutation.id)return;const e=r.params;if(e==null)return;const{arrayFormulaRange:a,arrayFormulaCellData:n}=e;this._formulaDataModel.setArrayFormulaRange(a),this._formulaDataModel.setArrayFormulaCellData(n),(i=this._configSer