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(formular): fix define name should not add ( (#5809)

VicKun6b7bef5c968f
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-search.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-search.ts
index a5c19bb091..c91ee7a9ae 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-search.ts
+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-search.ts
@@ -15,9 +15,9 @@
*/
import type { Editor } from '@univerjs/docs-ui';
-import type { ISearchItem } from '@univerjs/sheets-formula';
+import type { ISearchItemWithType } from '@univerjs/sheets-formula';
import type { INode } from './use-formula-token';
-import { matchToken, sequenceNodeType } from '@univerjs/engine-formula';
+import { FunctionType, matchToken, sequenceNodeType } from '@univerjs/engine-formula';
import { IDescriptionService } from '@univerjs/sheets-formula';
import { useDependency } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
@@ -29,7 +29,7 @@ import { useStateRef } from '../hooks/use-state-ref';
export const useFormulaSearch = (isNeed: boolean, nodes: INode[] = [], editor?: Editor) => {
const descriptionService = useDependency(IDescriptionService);
- const [searchList, setSearchList] = useState<ISearchItem[]>([]);
+ const [searchList, setSearchList] = useState<ISearchItemWithType[]>([]);
const [searchText, setSearchText] = useState<string>('');
const indexRef = useRef(-1);
const stateRef = useStateRef({ nodes });
@@ -83,14 +83,14 @@ export const useFormulaSearch = (isNeed: boolean, nodes: INode[] = [], editor?:
}
}, [isNeed]);
- const handlerFormulaReplace = (formulaName: string) => {
+ const handlerFormulaReplace = (formulaName: string, functionType: FunctionType) => {
const cloneNodes = [...stateRef.current.nodes];
if (indexRef.current !== -1) {
const lastNodes = cloneNodes.splice(indexRef.current + 1);
const oldNode = cloneNodes.pop() || '';
let offset = (typeof oldNode === 'string' ? oldNode.length : oldNode.token.length) - formulaName.length;
cloneNodes.push(formulaName);
- if (lastNodes[0] !== matchToken.OPEN_BRACKET) {
+ if (lastNodes[0] !== matchToken.OPEN_BRACKET && functionType !== FunctionType.DefinedName) {
cloneNodes.push(matchToken.OPEN_BRACKET);
offset--;
}
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx b/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
index 93c6fde65a..9f76da98c8 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
@@ -15,7 +15,7 @@
*/
import type { Editor } from '@univerjs/docs-ui';
-import type { ISequenceNode } from '@univerjs/engine-formula';
+import type { FunctionType, ISequenceNode } from '@univerjs/engine-formula';
import { CommandType, DisposableCollection, ICommandService } from '@univerjs/core';
import { borderClassName, clsx, scrollbarClassName } from '@univerjs/design';
import { DeviceInputEventType } from '@univerjs/engine-render';
@@ -51,8 +51,8 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
const [position$] = useEditorPosition(editorId, visible, [searchText, searchList]);
const stateRef = useStateRef({ searchList, active });
- const handleFunctionSelect = (v: string) => {
- const res = handlerFormulaReplace(v);
+ const handleFunctionSelect = (v: string, functionType: FunctionType) => {
+ const res = handlerFormulaReplace(v, functionType);
if (res) {
resetFormulaSearch();
onSelect(res);
@@ -104,7 +104,7 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
case KeyCode.TAB:
case KeyCode.ENTER: {
const item = searchList[active];
- handleFunctionSelect(item.name);
+ handleFunctionSelect(item.name, item.functionType);
break;
}
case KeyCode.ESC: {
@@ -218,7 +218,7 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
onMouseLeave={handleLiMouseLeave}
onMouseMove={debounceResetMouseState}
onClick={() => {
- handleFunctionSelect(item.name);
+ handleFunctionSelect(item.name, item.functionType);
if (editor) {
editor.focus();
}
diff --git a/packages/sheets-formula/src/index.ts b/packages/sheets-formula/src/index.ts
index 7f24928735..e3fabcceee 100644
--- a/packages/sheets-formula/src/index.ts
+++ b/packages/sheets-formula/src/index.ts
@@ -23,7 +23,8 @@ export { TriggerCalculationController } from './controllers/trigger-calculation.
export { UpdateDefinedNameController } from './controllers/update-defined-name.controller';
export { UpdateFormulaController } from './controllers/update-formula.controller';
export { UniverRemoteSheetsFormulaPlugin, UniverSheetsFormulaPlugin } from './plugin';
-export { DescriptionService, IDescriptionService, type ISearchItem } from './services/description.service';
+export { DescriptionService, IDescriptionService } from './services/description.service';
+export type { ISearchItem, ISearchItemWithType } from './services/description.service';
export type { IFormulaInfo, IOtherFormulaResult } from './services/formula-common';
export { FormulaRefRangeService } from './services/formula-ref-range.service';
export type { IRegisterAsyncFunction, IRegisterFunction, ISingleFunctionRegisterParams } from './services/register-function.service';
diff --git a/packages/sheets-formula/src/services/description.service.ts b/packages/sheets-formula/src/services/description.service.ts
index 4a003ba94d..b396e8ef9c 100644
--- a/packages/sheets-formula/src/services/description.service.ts
+++ b/packages/sheets-formula/src/services/description.service.ts
@@ -49,6 +49,10 @@ export interface ISearchItem {
desc: string;
}
+export interface ISearchItemWithType extends ISearchItem {
+ functionType: FunctionType;
+}
+
export interface IDescriptionService {
/**
* get all descriptions
@@ -75,7 +79,7 @@ export interface IDescriptionService {
* @param searchText
* @returns
*/
- getSearchListByNameFirstLetter(searchText: string): ISearchItem[];
+ getSearchListByNameFirstLetter(searchText: string): ISearchItemWithType[];
/**
* get search list by type, if type is -1, return all
@@ -162,13 +166,13 @@ export class DescriptionService implements IDescriptionService, IDisposable {
}
getSearchListByNameFirstLetter(searchText: string) {
- const searchList: ISearchItem[] = [];
+ const searchList: ISearchItemWithType[] = [];
const functionList = this._functionService.getDescriptions();
const _searchText = searchText.toLocaleUpperCase().trim();
functionList.forEach((item) => {
- const { functionName, abstract } = item;
+ const { functionName, abstract, functionType } = item;
if (functionName.toLocaleUpperCase().indexOf(_searchText) === 0) {
- searchList.push({ name: functionName, desc: abstract });
+ searchList.push({ name: functionName, desc: abstract, functionType });
}
});