Skip to content

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.

fix(sheet): defined name update name (#1917)

Univer5b6e2237a1d1
diff --git a/packages/engine-formula/src/engine/analysis/parser.ts b/packages/engine-formula/src/engine/analysis/parser.ts
index 08445fe92f..67ffae64da 100644
--- a/packages/engine-formula/src/engine/analysis/parser.ts
+++ b/packages/engine-formula/src/engine/analysis/parser.ts
@@ -89,7 +89,8 @@ export class AstTreeBuilder extends Disposable {
const node = this._parse(lexerNode, astNode);
/**
- * If the lexer node has defined names, it means that the current formula contains a reference to the defined name.
+ * If the lexer node has defined names,
+ * it means that the current formula contains a reference to the defined name.
*/
if (lexerNode.hasDefinedNames()) {
node?.setDefinedNames(lexerNode.getDefinedNames());
@@ -184,6 +185,7 @@ export class AstTreeBuilder extends Disposable {
return newLambdaNode;
}
+ // eslint-disable-next-line max-lines-per-function, complexity
private _parse(lexerNode: LexerNode, parent: BaseAstNode): Nullable<BaseAstNode> {
const children = lexerNode.getChildren();
const childrenCount = children.length;
diff --git a/packages/sheets-formula/src/controllers/active-dirty.controller.ts b/packages/sheets-formula/src/controllers/active-dirty.controller.ts
index b73a146676..89bf61a435 100644
--- a/packages/sheets-formula/src/controllers/active-dirty.controller.ts
+++ b/packages/sheets-formula/src/controllers/active-dirty.controller.ts
@@ -34,6 +34,7 @@ import type {
IRemoveColMutationParams,
IRemoveRowsMutationParams,
IRemoveSheetMutationParams,
+ ISetDefinedNameCommandParams,
ISetRangeValuesMutationParams,
} from '@univerjs/sheets';
import {
@@ -44,6 +45,7 @@ import {
RemoveColMutation,
RemoveRowMutation,
RemoveSheetMutation,
+ SetDefinedNameCommand,
SetRangeValuesMutation,
SetStyleCommand,
} from '@univerjs/sheets';
@@ -100,6 +102,27 @@ export class ActiveDirtyController extends Disposable {
},
});
+ this._initialMove();
+
+ this._initialRowAndColumn();
+
+ this._initialSheet();
+
+ this._initialDefinedName();
+
+ /**
+ * Changing the name of a sheet triggers an adjustment of the formula references, but does not trigger formula calculation; therefore, this logic has been removed.
+ */
+ // this._activeDirtyManagerService.register(SetWorksheetNameMutation.id, {
+ // commandId: SetWorksheetNameMutation.id,
+ // getDirtyData: (command: ICommandInfo) => {
+ // const params = command.params as ISetWorksheetNameMutationParams;
+ // return { dirtyNameMap: this._getRemoveSheetMutation(params, params.name) };
+ // },
+ // });
+ }
+
+ private _initialMove() {
this._activeDirtyManagerService.register(MoveRangeMutation.id, {
commandId: MoveRangeMutation.id,
getDirtyData: (command: ICommandInfo) => {
@@ -129,7 +152,9 @@ export class ActiveDirtyController extends Disposable {
};
},
});
+ }
+ private _initialRowAndColumn() {
this._activeDirtyManagerService.register(RemoveRowMutation.id, {
commandId: RemoveRowMutation.id,
getDirtyData: (command: ICommandInfo) => {
@@ -149,7 +174,10 @@ export class ActiveDirtyController extends Disposable {
};
},
});
+ }
+
+ private _initialSheet() {
this._activeDirtyManagerService.register(RemoveSheetMutation.id, {
commandId: RemoveSheetMutation.id,
getDirtyData: (command: ICommandInfo) => {
@@ -165,7 +193,9 @@ export class ActiveDirtyController extends Disposable {
return { dirtyNameMap: this._getInsertSheetMutation(params) };
},
});
+ }
+ private _initialDefinedName() {
this._activeDirtyManagerService.register(SetDefinedNameMutation.id, {
commandId: SetDefinedNameMutation.id,
getDirtyData: (command: ICommandInfo) => {
@@ -182,16 +212,16 @@ export class ActiveDirtyController extends Disposable {
},
});
- /**
- * Changing the name of a sheet triggers an adjustment of the formula references, but does not trigger formula calculation; therefore, this logic has been removed.
- */
- // this._activeDirtyManagerService.register(SetWorksheetNameMutation.id, {
- // commandId: SetWorksheetNameMutation.id,
- // getDirtyData: (command: ICommandInfo) => {
- // const params = command.params as ISetWorksheetNameMutationParams;
- // return { dirtyNameMap: this._getRemoveSheetMutation(params, params.name) };
- // },
- // });
+ this._activeDirtyManagerService.register(SetDefinedNameCommand.id, {
+ commandId: SetDefinedNameCommand.id,
+ getDirtyData: (command: ICommandInfo) => {
+ const params = command.params as ISetDefinedNameCommandParams;
+ const { oldDefinedName, newDefinedName } = params;
+ return { dirtyDefinedNameMap: this._getDefinedNameMutation({ ...newDefinedName, name: oldDefinedName.name }) };
+
+ // return { dirtyDefinedNameMap: this._getDefinedNameMutation(params) };
+ },
+ });
}
private _getDefinedNameMutation(definedName: ISetDefinedNameMutationParam) {
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
index b7b2971f99..a252aca57a 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
@@ -76,7 +76,7 @@ export const DefinedNameContainer = () => {
} else {
const oldDefinedName = definedNamesService.getValueById(unitId, id);
const newDefinedName = { id, unitId, name, formulaOrRefString, comment, localSheetId, hidden };
- commandService.executeCommand(SetDefinedNameCommand.id, { unitId, oldDefinedName, newDefinedName });
+ commandService.executeCommand(SetDefinedNameCommand.id, { unitId, oldDefinedName: { ...oldDefinedName, unitId }, newDefinedName });
}
setEditState(false);
setEditorKey(null);