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
chore(formula): defined name cache for getValueByName (#4675)
Dushusir1f6bcd770e7c
diff --git a/packages/engine-formula/src/services/defined-names.service.ts b/packages/engine-formula/src/services/defined-names.service.tsindex f0ee5b5954..c3508cc0ba 100644--- a/packages/engine-formula/src/services/defined-names.service.ts+++ b/packages/engine-formula/src/services/defined-names.service.ts@@ -76,10 +76,10 @@ export interface IDefinedNamesService {getWorksheetByRef(unitId: string, ref: string): Nullable<Worksheet>;}-export class DefinedNamesService extends Disposable implements IDefinedNamesService {// 18.2.6 definedNames (Defined Names)private _definedNameMap: IDefinedNameMap = {};+ private _nameCacheMap: { [unitId: string]: { [name: string]: IDefinedNamesServiceParam } } = {}; // Cache for name-to-definition mappingprivate readonly _update$ = new Subject();readonly update$ = this._update$.asObservable();@@ -101,11 +101,11 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServconstructor(@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();- // this.registerDefinedName('workbook-01', { id: 'test1', name: 'name-01', formulaOrRefString: '=sum(A1:B10)', comment: 'this is comment', localSheetId: 'sheet-0011', hidden: false });}override dispose(): void {this._definedNameMap = {};+ this._nameCacheMap = {}; // Clear cache}getWorksheetByRef(unitId: string, ref: string) {@@ -115,7 +115,7 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServfocusRange(unitId: string, id: string) {const item = this.getValueById(unitId, id);- if (item == null) {+ if (item === undefined) {return;}@@ -137,27 +137,31 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServregisterDefinedNames(unitId: string, params: IDefinedNameMapItem) {this._definedNameMap[unitId] = params;+ this._updateCache(unitId);this._update();}registerDefinedName(unitId: string, param: IDefinedNamesServiceParam) {const unitMap = this._definedNameMap[unitId];- if (unitMap == null) {+ if (unitMap === undefined) {this._definedNameMap[unitId] = {};}this._definedNameMap[unitId][param.id] = param;+ this._updateCache(unitId);this._update();}removeDefinedName(unitId: string, id: string) {delete this._definedNameMap[unitId]?.[id];+ this._updateCache(unitId);this._update();}removeUnitDefinedName(unitId: string) {delete this._definedNameMap[unitId];+ this._updateCache(unitId);this._update();}@@ -166,13 +170,33 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ}getValueByName(unitId: string, name: string) {+ // Check cache first+ const cachedMap = this._nameCacheMap[unitId];+ if (cachedMap) {+ return cachedMap[name] || null;+ }++ // If not in cache, traverse the nameMapconst nameMap = this._definedNameMap[unitId];- if (nameMap == null) {+ if (nameMap === undefined) {return null;}- return Array.from(Object.values(nameMap)).filter((value) => {- return value.name === name;- })?.[0];++ let result = null;+ for (const item of Object.values(nameMap)) {+ if (item.name === name) {+ result = item;+ break;+ }+ }++ // Cache the result if found+ if (result) {+ this._nameCacheMap[unitId] = this._nameCacheMap[unitId] || {};+ this._nameCacheMap[unitId][name] = result;+ }++ return result;}getValueById(unitId: string, id: string) {@@ -180,7 +204,7 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ}hasDefinedName(unitId: string) {- if (this._definedNameMap[unitId] == null) {+ if (this._definedNameMap[unitId] === undefined) {return false;}const size = Array.from(Object.values(this._definedNameMap[unitId])).length || 0;@@ -190,6 +214,21 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServprivate _update() {this._update$.next(null);}++ private _updateCache(unitId: string) {+ const nameMap = this._definedNameMap[unitId];+ if (nameMap === undefined) {+ delete this._nameCacheMap[unitId];+ return;+ }++ this._nameCacheMap[unitId] = {};++ // Cache all name mappings for this unitId+ for (const item of Object.values(nameMap)) {+ this._nameCacheMap[unitId][item.name] = item;+ }+ }}export const IDefinedNamesService = createIdentifier<DefinedNamesService>('univer.formula.defined-names.service');