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.

feat: set defined name in formula bar (#6304)

Roman Sergeevb4857080764d
diff --git a/packages/engine-formula/src/services/defined-names.service.ts b/packages/engine-formula/src/services/defined-names.service.ts
index 798c9c4c8e..496bd64986 100644
--- a/packages/engine-formula/src/services/defined-names.service.ts
+++ b/packages/engine-formula/src/services/defined-names.service.ts
@@ -223,6 +223,16 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ
return this._definedNameMap;
}
+ getDefinedNameByRefString(unitId: string, formulaOrRefString: string) {
+ if (!this._definedNameMap[unitId]) return;
+
+ for (const [_, item] of Object.entries(this._definedNameMap[unitId])) {
+ if (item.formulaOrRefString === formulaOrRefString) {
+ return item;
+ }
+ }
+ }
+
private _update() {
this._update$.next(null);
}
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
index 556a39a444..382a80821e 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
@@ -17,11 +17,11 @@
import type { Workbook, Worksheet } from '@univerjs/core';
import type { ChangeEvent, KeyboardEvent } from 'react';
import type { IScrollToCellCommandParams } from '../../commands/commands/set-scroll.command';
-import { ICommandService, IUniverInstanceService, ThemeService, UniverInstanceType } from '@univerjs/core';
+import { AbsoluteRefType, ICommandService, IUniverInstanceService, ThemeService, UniverInstanceType } from '@univerjs/core';
import { borderRightClassName, clsx, Dropdown } from '@univerjs/design';
-import { deserializeRangeWithSheet, IDefinedNamesService, isReferenceString } from '@univerjs/engine-formula';
+import { deserializeRangeWithSheet, IDefinedNamesService, isReferenceString, LexerTreeBuilder, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { MoreDownIcon } from '@univerjs/icons';
-import { getPrimaryForRange, SetSelectionsOperation } from '@univerjs/sheets';
+import { getPrimaryForRange, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { useDependency } from '@univerjs/ui';
import { useEffect, useState } from 'react';
import { ScrollToCellCommand } from '../../commands/commands/set-scroll.command';
@@ -34,6 +34,9 @@ export function DefinedName({ disable }: { disable: boolean }) {
const definedNamesService = useDependency(IDefinedNamesService);
const commandService = useDependency(ICommandService);
const univerInstanceService = useDependency(IUniverInstanceService);
+ const selectionManagerService = useDependency(SheetsSelectionsService);
+ const lexerTreeBuilder = useDependency(LexerTreeBuilder);
+
const worksheet = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
const unitId = worksheet?.getUnitId();
const subUnitId = worksheet?.getSheetId();
@@ -41,9 +44,21 @@ export function DefinedName({ disable }: { disable: boolean }) {
useEffect(() => {
const subscription = definedNamesService.currentRange$.subscribe(() => {
- const range = definedNamesService.getCurrentRangeForString();
- setRangeString(range);
- setInputValue(range);
+ const selections = selectionManagerService.getCurrentSelections();
+ const formulaOrRefs = selections?.map((selection) => {
+ return serializeRangeWithSheet(worksheet.getName(), selection.range);
+ })?.join(',');
+ const absoluteRef = lexerTreeBuilder.convertRefersToAbsolute(formulaOrRefs, AbsoluteRefType.ALL, AbsoluteRefType.ALL, worksheet.getName());
+ const definedName = definedNamesService.getDefinedNameByRefString(unitId, absoluteRef);
+
+ if (definedName) {
+ setRangeString(definedName.name);
+ setInputValue(definedName.name);
+ } else {
+ const range = definedNamesService.getCurrentRangeForString();
+ setRangeString(range);
+ setInputValue(range);
+ }
});
return () => {