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(sheets-formula): other formula register (#4073)
zhangw403381bd5a54
diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.tsindex 1c445f4939..120f91fd3d 100644--- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts+++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts@@ -41,6 +41,7 @@ import {SetStyleCommand,} from '@univerjs/sheets';import { BehaviorSubject } from 'rxjs';+import { RegisterOtherFormulaService } from '../services/register-other-formula.service';import { CalculationMode, PLUGIN_CONFIG_KEY_BASE } from './config.schema';/**@@ -140,7 +141,8 @@ export class TriggerCalculationController extends Disposable {@ILogService private readonly _logService: ILogService,@IConfigService private readonly _configService: IConfigService,@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,- @Inject(LocaleService) private readonly _localeService: LocaleService+ @Inject(LocaleService) private readonly _localeService: LocaleService,+ @Inject(RegisterOtherFormulaService) private readonly _registerOtherFormulaService: RegisterOtherFormulaService) {super();@@ -510,6 +512,8 @@ export class TriggerCalculationController extends Disposable {private _initialExecuteFormula() {const params = this._getDiryDataByCalculationMode(this._calculationMode);this._commandService.executeCommand(SetFormulaCalculationStartMutation.id, params, lo);++ this._registerOtherFormulaService.calculateStarted$.next(true);}private _getDiryDataByCalculationMode(calculationMode: CalculationMode): IFormulaDirtyData {diff --git a/packages/sheets-formula/src/services/register-other-formula.service.ts b/packages/sheets-formula/src/services/register-other-formula.service.tsindex 32d399db4c..d6def09aab 100644--- a/packages/sheets-formula/src/services/register-other-formula.service.ts+++ b/packages/sheets-formula/src/services/register-other-formula.service.ts@@ -17,9 +17,9 @@import type { IRange, Nullable } from '@univerjs/core';import type { IRemoveOtherFormulaMutationParams, ISetFormulaCalculationResultMutation, ISetOtherFormulaMutationParams } from '@univerjs/engine-formula';import type { IOtherFormulaMarkDirtyParams } from '../commands/mutations/formula.mutation';-import { Disposable, ICommandService, Inject, LifecycleService, LifecycleStages, ObjectMatrix, Tools } from '@univerjs/core';+import { Disposable, ICommandService, Inject, LifecycleService, ObjectMatrix, Tools } from '@univerjs/core';import { IActiveDirtyManagerService, RemoveOtherFormulaMutation, SetFormulaCalculationResultMutation, SetOtherFormulaMutation } from '@univerjs/engine-formula';-import { bufferWhen, filter, Subject } from 'rxjs';+import { BehaviorSubject, bufferWhen, filter, Subject } from 'rxjs';import { OtherFormulaMarkDirty } from '../commands/mutations/formula.mutation';import { FormulaResultStatus, type IOtherFormulaResult } from './formula-common';@@ -34,6 +34,8 @@ export class RegisterOtherFormulaService extends Disposable {private _formulaResult$ = new Subject<Record<string, Record<string, IOtherFormulaResult[]>>>();public formulaResult$ = this._formulaResult$.asObservable();+ public calculateStarted$ = new BehaviorSubject(false);+constructor(@ICommandService private readonly _commandService: ICommandService,@IActiveDirtyManagerService private _activeDirtyManagerService: IActiveDirtyManagerService,@@ -49,6 +51,7 @@ export class RegisterOtherFormulaService extends Disposable {this._formulaChangeWithRange$.complete();this._formulaResult$.complete();+ this.calculateStarted$.complete();}private _ensureCacheMap(unitId: string, subUnitId: string) {@@ -113,16 +116,15 @@ export class RegisterOtherFormulaService extends Disposable {});};- // Wait until the stage is steady, then register the formula that needs to be marked dirty with formula and range listthis.disposeWithMe(this._formulaChangeWithRange$- .pipe(bufferWhen(() => this._lifecycleService.lifecycle$.pipe(filter((stage) => stage === LifecycleStages.Steady))))+ .pipe(bufferWhen(() => this.calculateStarted$.pipe(filter((calculateStarted) => calculateStarted)))).subscribe((options) => options.forEach(handleRegister)));this.disposeWithMe(this._formulaChangeWithRange$- .pipe(filter(() => this._lifecycleService.stage >= LifecycleStages.Steady))+ .pipe(filter(() => this.calculateStarted$.getValue())).subscribe(handleRegister));}@@ -153,12 +155,12 @@ export class RegisterOtherFormulaService extends Disposable {continue;}- if (!item?.result) {+ if (!item.result) {item.result = {};}const resultMatrix = new ObjectMatrix(current);- const resultObject = new ObjectMatrix(item?.result);+ const resultObject = new ObjectMatrix(item.result);resultMatrix.forValue((row, col, value) => {resultObject.setValue(row, col, value);