Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(formula): feature calculation update (#2838)
Univerf66e5a7485e9
diff --git a/packages/engine-formula/src/commands/mutations/set-feature-calculation.mutation.ts b/packages/engine-formula/src/commands/mutations/set-feature-calculation.mutation.tsindex d133e26197..254687a7fe 100644--- a/packages/engine-formula/src/commands/mutations/set-feature-calculation.mutation.ts+++ b/packages/engine-formula/src/commands/mutations/set-feature-calculation.mutation.ts@@ -33,7 +33,13 @@ export const SetFeatureCalculationMutation: IMutation<ISetFeatureCalculationMutahandler: () => true,};-export const RemoveFeatureCalculationMutation: IMutation<{ featureId: string }> = {+export interface IRemoveFeatureCalculationMutationParam {+ featureIds: string[];+ unitId: string;+ subUnitId: string;+}++export const RemoveFeatureCalculationMutation: IMutation<IRemoveFeatureCalculationMutationParam> = {id: 'formula.mutation.remove-feature-calculation',type: CommandType.MUTATION,handler: () => true,diff --git a/packages/engine-formula/src/controller/set-dependency.controller.ts b/packages/engine-formula/src/controller/set-dependency.controller.tsindex a93453b1dd..7ea41fd019 100644--- a/packages/engine-formula/src/controller/set-dependency.controller.ts+++ b/packages/engine-formula/src/controller/set-dependency.controller.ts@@ -17,6 +17,9 @@import type { ICommandInfo } from '@univerjs/core';import { Disposable, ICommandService, LifecycleStages, ObjectMatrix, OnLifecycle } from '@univerjs/core';+import type {+ IRemoveFeatureCalculationMutationParam,+ ISetFeatureCalculationMutation } from '../commands/mutations/set-feature-calculation.mutation';import {RemoveFeatureCalculationMutation,SetFeatureCalculationMutation,@@ -33,7 +36,8 @@ export class SetDependencyController extends Disposable {constructor(@ICommandService private readonly _commandService: ICommandService,@IFeatureCalculationManagerService- @IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService+ @IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService,+ @IFeatureCalculationManagerService private readonly _featureCalculationManagerService: IFeatureCalculationManagerService) {super();@@ -42,18 +46,37 @@ export class SetDependencyController extends Disposable {private _initialize(): void {this._commandExecutedListener();++ this._featureCalculationManagerServiceListener();+ }++ private _featureCalculationManagerServiceListener() {+ this.disposeWithMe(+ this._featureCalculationManagerService.onChanged$.subscribe((params) => {+ const { unitId, subUnitId, featureIds } = params;+ this._dependencyManagerService.removeFeatureFormulaDependency(unitId, subUnitId, featureIds);+ })+ );}private _commandExecutedListener() {this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {- if (command.id === RemoveFeatureCalculationMutation.id || command.id === SetFeatureCalculationMutation.id) {- const params = command.params as { featureId: string };+ if (command.id === RemoveFeatureCalculationMutation.id) {+ const params = command.params as IRemoveFeatureCalculationMutationParam;+ if (params == null) {+ return;+ }+ const { featureIds, unitId, subUnitId } = params;+ this._dependencyManagerService.removeFeatureFormulaDependency(unitId, subUnitId, featureIds);+ } else if (command.id === SetFeatureCalculationMutation.id) {+ const params = command.params as ISetFeatureCalculationMutation;if (params == null) {return;}- const { featureId } = params;- this._dependencyManagerService.removeFeatureFormulaDependency(featureId);+ const { featureId, calculationParam } = params;+ const { unitId, subUnitId } = calculationParam;+ this._dependencyManagerService.removeFeatureFormulaDependency(unitId, subUnitId, [featureId]);} else if (command.id === RemoveOtherFormulaMutation.id) {const params = command.params as IRemoveOtherFormulaMutationParams;if (params == null) {diff --git a/packages/engine-formula/src/controller/set-feature-calculation.controller.ts b/packages/engine-formula/src/controller/set-feature-calculation.controller.tsindex a28e9aa4af..4fb88aa227 100644--- a/packages/engine-formula/src/controller/set-feature-calculation.controller.ts+++ b/packages/engine-formula/src/controller/set-feature-calculation.controller.ts@@ -17,8 +17,10 @@import type { ICommandInfo } from '@univerjs/core';import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';+import type {+ IRemoveFeatureCalculationMutationParam,+ ISetFeatureCalculationMutation } from '../commands/mutations/set-feature-calculation.mutation';import {- type ISetFeatureCalculationMutation,RemoveFeatureCalculationMutation,SetFeatureCalculationMutation,} from '../commands/mutations/set-feature-calculation.mutation';@@ -49,14 +51,15 @@ export class SetFeatureCalculationController extends Disposable {return;}const { featureId, calculationParam } = params;- this._featureCalculationManagerService.register(featureId, calculationParam);+ const { unitId, subUnitId } = calculationParam;+ this._featureCalculationManagerService.register(unitId, subUnitId, featureId, calculationParam);} else if (command.id === RemoveFeatureCalculationMutation.id) {- const params = command.params as { featureId: string };+ const params = command.params as IRemoveFeatureCalculationMutationParam;if (params == null) {return;}- const { featureId } = params;- this._featureCalculationManagerService.remove(featureId);+ const { featureIds, unitId, subUnitId } = params;+ this._featureCalculationManagerService.remove(unitId, subUnitId, featureIds);}}));diff --git a/packages/engine-formula/src/engine/dependency/dependency-tree.ts b/packages/engine-formula/src/engine/dependency/dependency-tree.tsindex 7744470c3a..86d17fc4dc 100644--- a/packages/engine-formula/src/engine/dependency/dependency-tree.ts+++ b/packages/engine-formula/src/engine/dependency/dependency-tree.ts@@ -69,8 +69,10 @@ export class FormulaDependencyTree extends Disposable {featureId: Nullable<string>;+ isPassive: boolean = true;+getDirtyData: Nullable<- (dirtyData: IFormulaDirtyData, runtimeData: IAllRuntimeData) => {+ (tree: FormulaDependencyTree, dirtyData: IFormulaDirtyData, runtimeData: IAllRuntimeData) => {runtimeCellData: IRuntimeUnitDataType;dirtyRanges: IFeatureDirtyRangeType;}diff --git a/packages/engine-formula/src/engine/dependency/formula-dependency.ts b/packages/engine-formula/src/engine/dependency/formula-dependency.tsindex 86ac9148d4..fd897eb822 100644--- a/packages/engine-formula/src/engine/dependency/formula-dependency.ts+++ b/packages/engine-formula/src/engine/dependency/formula-dependency.ts@@ -203,35 +203,69 @@ export class FormulaDependencyGenerator extends Disposable {* which can determine the execution timing of the external application* registration Executor based on the dependency relationship.*/- this._featureCalculationManagerService.getReferenceExecutorMap().forEach((params, featureId) => {- const { unitId, subUnitId, dependencyRanges, getDirtyData } = params;- if (this._dependencyManagerService.hasFeatureFormulaDependency(unitId, subUnitId, featureId)) {- return true;- }+ const featureMap = this._featureCalculationManagerService.getReferenceExecutorMap();+ featureMap.forEach((subUnitMap, _) => {+ subUnitMap.forEach((featureMap, _) => {+ featureMap.forEach((params, featureId) => {+ const { unitId, subUnitId, dependencyRanges, getDirtyData } = params;++ if (this._dependencyManagerService.hasFeatureFormulaDependency(unitId, subUnitId, featureId)) {+ this._makePassiveDirtyForFeatureFormulas(this._dependencyManagerService.getFeatureFormulaDependency(unitId, subUnitId, featureId)!);+ return true;+ }++ const FDtree = new FormulaDependencyTree();- const FDtree = new FormulaDependencyTree();+ FDtree.unitId = unitId;+ FDtree.subUnitId = subUnitId;- FDtree.unitId = unitId;- FDtree.subUnitId = subUnitId;+ FDtree.getDirtyData = getDirtyData;- FDtree.getDirtyData = getDirtyData;+ FDtree.featureId = featureId;- FDtree.featureId = featureId;+ FDtree.rangeList = dependencyRanges.map((range) => {+ return {+ gridRange: range,+ token: serializeRangeToRefString({ ...range, sheetName: this._currentConfigService.getSheetName(range.unitId, range.sheetId) }),+ };+ });- FDtree.rangeList = dependencyRanges.map((range) => {- return {- gridRange: range,- token: serializeRangeToRefString({ ...range, sheetName: this._currentConfigService.getSheetName(range.unitId, range.sheetId) }),- };- });+ this._dependencyManagerService.addFeatureFormulaDependency(unitId, subUnitId, featureId, FDtree);- this._dependencyManagerService.addFeatureFormulaDependency(featureId, FDtree);+ this._makePassiveDirtyForFeatureFormulas(FDtree);- treeList.push(FDtree);+ treeList.push(FDtree);+ });+ });});}+ private _makePassiveDirtyForFeatureFormulas(tree: FormulaDependencyTree) {+ const featureId = tree.featureId;++ if (featureId == null) {+ tree.isPassive = true;+ return;+ }++ const featureMap = this._currentConfigService.getDirtyUnitFeatureMap();++ if (featureMap == null) {+ tree.isPassive = true;+ return;+ }++ const featureState = featureMap[tree.unitId]?.[tree.subUnitId]?.[featureId];++ if (featureState == null) {+ tree.isPassive = true;+ return;+ }++ tree.isPassive = false;+ }+private _registerOtherFormulas(otherFormulaData: IOtherFormulaData, otherFormulaDataKeys: string[], treeList: FormulaDependencyTree[]) {/*** Register formulas in doc, slide, and other types of applications.diff --git a/packages/engine-formula/src/plugin.ts b/packages/engine-formula/src/plugin.tsindex 11075a3d67..478d420f2d 100644--- a/packages/engine-formula/src/plugin.ts+++ b/packages/engine-formula/src/plugin.ts@@ -82,7 +82,6 @@ export class UniverFormulaEnginePlugin extends Plugin {const dependencies: Dependency[] = [// Services[IFunctionService, { useClass: FunctionService }],- [IFeatureCalculationManagerService, { useClass: FeatureCalculationManagerService }],[IDefinedNamesService, { useClass: DefinedNamesService }],[IActiveDirtyManagerService, { useClass: ActiveDirtyManagerService }],[ISuperTableService, { useClass: SuperTableService }],@@ -100,7 +99,6 @@ export class UniverFormulaEnginePlugin extends Plugin {useFactory: () => this._injector.createInstance(FormulaController, this._config?.function),},],- [SetFeatureCalculationController],[SetDefinedNameController],[SetSuperTableController],];@@ -114,11 +112,13 @@ export class UniverFormulaEnginePlugin extends Plugin {[IFormulaRuntimeService, { useClass: FormulaRuntimeService }],[IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }],[IDependencyManagerService, { useClass: DependencyManagerService }],+ [IFeatureCalculationManagerService, { useClass: FeatureCalculationManagerService }],//Controller[CalculateController],[SetOtherFormulaController],[SetDependencyController],+ [SetFeatureCalculationController],// Calculation engine[FormulaDependencyGenerator],diff --git a/packages/engine-formula/src/services/calculate-formula.service.ts b/packages/engine-formula/src/services/calculate-formula.service.tsindex 274ec30cfe..7a2f31ad06 100644--- a/packages/engine-formula/src/services/calculate-formula.service.ts+++ b/packages/engine-formula/src/services/calculate-formula.service.ts@@ -286,7 +286,7 @@ export class CalculateFormulaService extends Disposable {* and return the dirty area marked by the feature,* so as to allow the formulas depending on the dirty area to continue the calculation.*/- const { runtimeCellData, dirtyRanges } = getDirtyData(this._currentConfigService.getDirtyData(), this._runtimeService.getAllRuntimeData());+ const { runtimeCellData, dirtyRanges } = getDirtyData(tree, this._currentConfigService.getDirtyData(), this._runtimeService.getAllRuntimeData());this._runtimeService.setRuntimeFeatureCellData(tree.featureId, runtimeCellData);diff --git a/packages/engine-formula/src/services/dependency-manager.service.ts b/packages/engine-formula/src/services/dependency-manager.service.tsindex f80d673c8c..3caff5390c 100644--- a/packages/engine-formula/src/services/dependency-manager.service.ts+++ b/packages/engine-formula/src/services/dependency-manager.service.ts@@ -23,6 +23,10 @@ export interface IOtherFormulaDependencyParam {[unitId: string]: Nullable<{ [sheetId: string]: { [formulaId: string]: Nullable<FormulaDependencyTree> } }>;}+export interface IFeatureFormulaDependencyParam {+ [unitId: string]: Nullable<{ [sheetId: string]: { [featureId: string]: Nullable<FormulaDependencyTree> } }>;+}+export interface IFormulaDependencyParam {[unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<Nullable<FormulaDependencyTree>> }>;}@@ -44,9 +48,11 @@ export interface IDependencyManagerService {hasOtherFormulaDependency(unitId: string, sheetId: string, formulaId: string): boolean;- addFeatureFormulaDependency(featureId: string, dependencyTree: FormulaDependencyTree): void;+ addFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string, dependencyTree: FormulaDependencyTree): void;++ removeFeatureFormulaDependency(unitId: string, sheetId: string, featureIds: string[]): void;- removeFeatureFormulaDependency(featureId: string): void;+ getFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string): Nullable<FormulaDependencyTree>;hasFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string): boolean;@@ -68,13 +74,13 @@ export interface IDependencyManagerService {export class DependencyManagerService extends Disposable implements IDependencyManagerService {private _otherFormulaData: IOtherFormulaDependencyParam = {};- private _featureFormulaData: Map<string, FormulaDependencyTree> = new Map();+ private _featureFormulaData: IFeatureFormulaDependencyParam = {};private _formulaData: IFormulaDependencyParam = {};override dispose(): void {this._otherFormulaData = {};- this._featureFormulaData.clear();+ this._featureFormulaData = {};this._formulaData = {};}@@ -99,11 +105,18 @@ export class DependencyManagerService extends Disposable implements IDependencyM});});- this._featureFormulaData.forEach((feature) => {- if (feature) {- feature.resetState();- trees.push(feature);+ Object.values(this._featureFormulaData).forEach((unit) => {+ if (unit == null) {+ return true;}+ Object.values(unit).forEach((sheet) => {+ Object.values(sheet).forEach((feature) => {+ if (feature) {+ feature.resetState();+ trees.push(feature);+ }+ });+ });});Object.values(this._formulaData).map((unit) => {@@ -210,7 +223,7 @@ export class DependencyManagerService extends Disposable implements IDependencyMreset() {this._otherFormulaData = {};- this._featureFormulaData.clear();+ this._featureFormulaData = {};this._formulaData = {};}@@ -234,22 +247,40 @@ export class DependencyManagerService extends Disposable implements IDependencyM}}+ getOtherFormulaDependency(unitId: string, sheetId: string, formulaId: string) {+ return this._otherFormulaData[unitId]?.[sheetId]?.[formulaId];+ }+hasOtherFormulaDependency(unitId: string, sheetId: string, formulaId: string) {return this._otherFormulaData[unitId]?.[sheetId]?.[formulaId] != null;}- addFeatureFormulaDependency(featureId: string, dependencyTree: FormulaDependencyTree) {- this._featureFormulaData.set(featureId, dependencyTree);+ addFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string, dependencyTree: FormulaDependencyTree) {+ if (!this._featureFormulaData[unitId]) {+ this._featureFormulaData[unitId] = {};+ }+ if (!this._featureFormulaData[unitId]![sheetId]) {+ this._featureFormulaData[unitId]![sheetId] = {};+ }+ this._featureFormulaData[unitId]![sheetId][featureId] = dependencyTree;+ }++ removeFeatureFormulaDependency(unitId: string, sheetId: string, featureIds: string[]) {+ if (this._featureFormulaData[unitId] && this._featureFormulaData[unitId]![sheetId]) {+ featureIds.forEach((featureId) => {+ const deleteTree = this._featureFormulaData[unitId]![sheetId][featureId];+ this.clearDependencyForTree(deleteTree);+ delete this._featureFormulaData[unitId]![sheetId][featureId];+ });+ }}- removeFeatureFormulaDependency(featureId: string) {- const deleteTree = this._featureFormulaData.get(featureId);- this.clearDependencyForTree(deleteTree);- this._featureFormulaData.delete(featureId);+ getFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string) {+ return this._featureFormulaData[unitId]?.[sheetId]?.[featureId];}- hasFeatureFormulaDependency(featureId: string) {- return this._featureFormulaData.has(featureId);+ hasFeatureFormulaDependency(unitId: string, sheetId: string, featureId: string) {+ return this._featureFormulaData[unitId]?.[sheetId]?.[featureId] != null;}addFormulaDependency(unitId: string, sheetId: string, row: number, column: number, dependencyTree: FormulaDependencyTree) {diff --git a/packages/engine-formula/src/services/feature-calculation-manager.service.ts b/packages/engine-formula/src/services/feature-calculation-manager.service.tsindex d04b81812f..083518a3bd 100644--- a/packages/engine-formula/src/services/feature-calculation-manager.service.ts+++ b/packages/engine-formula/src/services/feature-calculation-manager.service.ts@@ -17,7 +17,11 @@import type { IUnitRange, Nullable } from '@univerjs/core';import { createIdentifier, Disposable } from '@univerjs/core';+import type { Observable } from 'rxjs';+import { Subject } from 'rxjs';import type { IFeatureDirtyRangeType, IRuntimeUnitDataType } from '../basics/common';+import type { IRemoveFeatureCalculationMutationParam } from '../commands/mutations/set-feature-calculation.mutation';+import type { FormulaDependencyTree } from '../engine/dependency/dependency-tree';import type { IAllRuntimeData } from './runtime.service';import type { IFormulaDirtyData } from './current-data.service';@@ -25,7 +29,7 @@ export interface IFeatureCalculationManagerParam {unitId: string;subUnitId: string;dependencyRanges: IUnitRange[];- getDirtyData: (dirtyData: IFormulaDirtyData, runtimeData: IAllRuntimeData) => {+ getDirtyData: (tree: FormulaDependencyTree, dirtyData: IFormulaDirtyData, runtimeData: IAllRuntimeData) => {runtimeCellData: IRuntimeUnitDataType;dirtyRanges: IFeatureDirtyRangeType;};@@ -34,15 +38,17 @@ export interface IFeatureCalculationManagerParam {export interface IFeatureCalculationManagerService {dispose(): void;- remove(featureId: string): void;+ remove(unitId: string, subUnitId: string, featureIds: string[]): void;- get(featureId: string): Nullable<IFeatureCalculationManagerParam>;+ get(unitId: string, subUnitId: string, featureId: string): Nullable<IFeatureCalculationManagerParam>;- has(featureId: string): boolean;+ has(unitId: string, subUnitId: string, featureId: string): boolean;- register(featureId: string, referenceExecutor: IFeatureCalculationManagerParam): void;+ register(unitId: string, subUnitId: string, featureId: string, referenceExecutor: IFeatureCalculationManagerParam): void;- getReferenceExecutorMap(): Map<string, IFeatureCalculationManagerParam>;+ getReferenceExecutorMap(): Map<string, Map<string, Map<string, IFeatureCalculationManagerParam>>>;++ onChanged$: Observable<IRemoveFeatureCalculationMutationParam>;}/**@@ -52,26 +58,57 @@ export interface IFeatureCalculationManagerService {* thereby completing the calculation of the entire dependency tree.*/export class FeatureCalculationManagerService extends Disposable implements IFeatureCalculationManagerService {- private _referenceExecutorMap: Map<string, IFeatureCalculationManagerParam> = new Map();+ private _referenceExecutorMap: Map<string, Map<string, Map<string, IFeatureCalculationManagerParam>>> = new Map(); // unitId -> subUnitId -> featureId -> IFeatureCalculationManagerParam++ private _onChanged$ = new Subject<IRemoveFeatureCalculationMutationParam>();+ readonly onChanged$ = this._onChanged$.asObservable();override dispose(): void {this._referenceExecutorMap.clear();}- remove(featureId: string) {- this._referenceExecutorMap.delete(featureId);+ remove(unitId: string, subUnitId: string, featureIds: string[]) {+ featureIds.forEach((featureId) => {+ this._referenceExecutorMap.get(unitId)?.get(subUnitId)?.delete(featureId);+ });++ this._onChanged$.next({+ unitId,+ subUnitId,+ featureIds,+ });}- get(featureId: string) {- return this._referenceExecutorMap.get(featureId);+ get(unitId: string, subUnitId: string, featureId: string) {+ return this._referenceExecutorMap.get(unitId)?.get(subUnitId)?.get(featureId);}- has(featureId: string) {- return this._referenceExecutorMap.has(featureId);+ has(unitId: string, subUnitId: string, featureId: string): boolean {+ return this._referenceExecutorMap.get(unitId)?.get(subUnitId)?.has(featureId) !== null;}- register(featureId: string, referenceExecutor: IFeatureCalculationManagerParam) {- this._referenceExecutorMap.set(featureId, referenceExecutor);+ register(unitId: string, subUnitId: string, featureId: string, referenceExecutor: IFeatureCalculationManagerParam) {+ let unitMap = this._referenceExecutorMap.get(unitId);++ if (!unitMap) {+ unitMap = new Map();+ this._referenceExecutorMap.set(unitId, unitMap);+ }++ let subUnitMap = unitMap.get(subUnitId);++ if (!subUnitMap) {+ subUnitMap = new Map();+ unitMap.set(subUnitId, subUnitMap);+ }++ this._onChanged$.next({+ unitId,+ subUnitId,+ featureIds: [featureId],+ });++ subUnitMap.set(featureId, referenceExecutor);}getReferenceExecutorMap() {