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(formula): support recursive resolution for nested defined names (#6100)

Univerb5f948f4ef4f
diff --git a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
index 948e86e92f..4179b6199a 100644
--- a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
+++ b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
@@ -15,13 +15,15 @@
*/
import type { IRange, Nullable } from '@univerjs/core';
-import type { ISequenceArray, ISequenceNode } from '../utils/sequence';
+import type { IDirtyUnitSheetDefinedNameMap } from '../../basics/common';
+import type { IDefinedNamesServiceParam } from '../../services/defined-names.service';
+import type { ISequenceArray, ISequenceNode } from '../utils/sequence';
import { AbsoluteRefType, Disposable, isValidRange, moveRangeByOffset, Tools } from '@univerjs/core';
import { FormulaAstLRU } from '../../basics/cache-lru';
+
import { ERROR_TYPE_COUNT_ARRAY, ERROR_TYPE_SET, ErrorType } from '../../basics/error-type';
import { isFormulaLexerToken, isTokenCannotBeAtEnd, isTokenCannotPrecedeSuffixToken } from '../../basics/match-token';
-
import { regexTestSingeRange } from '../../basics/regex';
import {
matchToken,
@@ -56,6 +58,12 @@ export const FormulaLexerNodeCache = new FormulaAstLRU<LexerNode>(FORMULA_CACHE_
export const FormulaSequenceNodeCache = new FormulaAstLRU<Array<string | ISequenceNode>>(FORMULA_CACHE_LRU_COUNT);
+interface IInjectDefinedNameParam {
+ unitId: Nullable<string>;
+ getValueByName(unitId: string, name: string): Nullable<IDefinedNamesServiceParam>;
+ getDirtyDefinedNameMap(): IDirtyUnitSheetDefinedNameMap;
+}
+
export class LexerTreeBuilder extends Disposable {
private _currentLexerNode: LexerNode = new LexerNode();
@@ -619,16 +627,11 @@ export class LexerTreeBuilder extends Disposable {
treeBuilder(
formulaString: string,
transformSuffix = true,
- injectDefinedName?: (sequenceArray: ISequenceArray[]) => {
- sequenceString: string;
- hasDefinedName: boolean;
- definedNames: string[];
- },
- simpleCheckDefinedName?: (formulaString: string) => boolean
+ injectDefinedNameParam?: IInjectDefinedNameParam
) {
if (transformSuffix === true) {
const lexerNode = FormulaLexerNodeCache.get(formulaString);
- const simpleCheckDefinedNameResult = simpleCheckDefinedName?.(formulaString);
+ const simpleCheckDefinedNameResult = injectDefinedNameParam && this._simpleCheckDefinedName?.(formulaString, injectDefinedNameParam);
if (lexerNode && !simpleCheckDefinedNameResult) {
return lexerNode;
}
@@ -650,8 +653,8 @@ export class LexerTreeBuilder extends Disposable {
let currentDefinedNames: string[] = [];
- if (injectDefinedName) {
- const { hasDefinedName, sequenceString, definedNames } = injectDefinedName(sequenceArray);
+ if (injectDefinedNameParam) {
+ const { hasDefinedName, sequenceString, definedNames } = this._handleDefinedName(sequenceArray, injectDefinedNameParam);
currentHasDefinedName = hasDefinedName;
currentSequenceString = sequenceString;
currentDefinedNames = definedNames;
@@ -690,6 +693,121 @@ export class LexerTreeBuilder extends Disposable {
return this._currentLexerNode;
}
+ private _handleDefinedName(sequenceArray: ISequenceArray[], param: IInjectDefinedNameParam): {
+ sequenceString: string;
+ hasDefinedName: boolean;
+ definedNames: string[];
+ } {
+ const { unitId, getValueByName } = param;
+
+ if (unitId == null) {
+ return {
+ sequenceString: '',
+ hasDefinedName: false,
+ definedNames: [],
+ };
+ }
+
+ const sequenceNodes = this.getSequenceNode(sequenceArray);
+ let sequenceString = '';
+ let hasDefinedName = false;
+ const definedNames: string[] = [];
+
+ for (let i = 0, len = sequenceNodes.length; i < len; i++) {
+ const node = sequenceNodes[i];
+ if (typeof node === 'string') {
+ sequenceString += node;
+ continue;
+ }
+
+ const { nodeType, token } = node;
+
+ if (nodeType === sequenceNodeType.REFERENCE || nodeType === sequenceNodeType.FUNCTION) {
+ const definedContent = getValueByName(unitId, token);
+ if (definedContent) {
+ const refString = definedContent.formulaOrRefString;
+ // if (refString.substring(0, 1) === operatorToken.EQUALS) {
+ // refString = refString.substring(1);
+ // }
+
+ const nestedDefinedNameParam = this._handleNestedDefinedName(refString, param);
+ if (nestedDefinedNameParam == null || typeof nestedDefinedNameParam !== 'object') {
+ sequenceString += nestedDefinedNameParam || ErrorType.NAME;
+ hasDefinedName = true;
+ definedNames.push(token);
+ } else if (typeof nestedDefinedNameParam === 'object') {
+ const { sequenceString: nestedSequenceString, definedNames: nestedDefinedNames } = nestedDefinedNameParam;
+ sequenceString += nestedSequenceString;
+ nestedDefinedNames.forEach((name) => {
+ definedNames.push(name);
+ });
+ hasDefinedName = true;
+ }
+ } else if (this._checkDefinedNameDirty(token, param)) {
+ sequenceString += ErrorType.NAME;
+ hasDefinedName = true;
+ definedNames.push(token);
+ } else {
+ sequenceString += token;
+ }
+ } else {
+ sequenceString += token;
+ }
+ }
+
+ return {
+ sequenceString,
+ hasDefinedName,
+ definedNames,
+ };
+ }
+
+ private _handleNestedDefinedName(refString: string, param: IInjectDefinedNameParam) {
+ const sequenceArray: ISequenceArray[] = [];
+
+ const state = this._nodeMaker(refString, sequenceArray);
+
+ if (state === ErrorType.VALUE || sequenceArray.length === 0) {
+ return state as string;
+ }
+
+ return this._handleDefinedName(sequenceArray, param);
+ }
+
+ private _simpleCheckDefinedName(formulaString: string, injectDefinedNameParam: IInjectDefinedNameParam) {
+ const { getDirtyDefinedNameMap, unitId } = injectDefinedNameParam;
+ const definedNameMap = getDirtyDefinedNameMap();
+ const executeUnitId = unitId;
+ if (executeUnitId != null && definedNameMap[executeUnitId] != null) {
+ const names = Object.keys(definedNameMap[executeUnitId]!);
+ for (let i = 0, len = names.length; i < len; i++) {
+ const name = names[i];
+ if (formulaString.indexOf(name) > -1) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private _checkDefinedNameDirty(token: string, injectDefinedNameParam: IInjectDefinedNameParam) {
+ const { getDirtyDefinedNameMap, unitId } = injectDefinedNameParam;
+ const definedNameMap = getDirtyDefinedNameMap();
+ const executeUnitId = unitId;
+ if (executeUnitId != null && definedNameMap[executeUnitId] != null) {
+ const names = Object.keys(definedNameMap[executeUnitId]!);
+ for (let i = 0, len = names.length; i < len; i++) {
+ const name = names[i];
+ if (name === token) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
// eslint-disable-next-line complexity
private _suffixExpressionHandler(lexerNode: LexerNode): boolean {
const children = lexerNode.getChildren();
@@ -746,7 +864,7 @@ export class LexerTreeBuilder extends Disposable {
// =()+9, return error
this._processSuffixExpressionCloseBracket(baseStack, symbolStack, children, i);
} else {
- // =(1+3)9, return error
+ // =(1+3)9, return error
if (this._checkCloseBracket(children[i - 1])) {
return false;
}
diff --git a/packages/engine-formula/src/engine/analysis/lexer.ts b/packages/engine-formula/src/engine/analysis/lexer.ts
index 6496051c2e..9b5cee2ed9 100644
--- a/packages/engine-formula/src/engine/analysis/lexer.ts
+++ b/packages/engine-formula/src/engine/analysis/lexer.ts
@@ -14,14 +14,9 @@
* limitations under the License.
*/
-import type { ISequenceArray } from '../utils/sequence';
-
import { Disposable, Inject } from '@univerjs/core';
-import { ErrorType } from '../../basics/error-type';
-import { operatorToken } from '../../basics/token';
import { IFormulaCurrentConfigService } from '../../services/current-data.service';
import { IDefinedNamesService } from '../../services/defined-names.service';
-import { sequenceNodeType } from '../utils/sequence';
import { LexerTreeBuilder } from './lexer-tree-builder';
export class Lexer extends Disposable {
@@ -34,92 +29,10 @@ export class Lexer extends Disposable {
}
treeBuilder(formulaString: string, transformSuffix = true) {
- return this._lexerTreeBuilder.treeBuilder(formulaString, transformSuffix, this._injectDefinedName.bind(this), this._simpleCheckDefinedName.bind(this));
- }
-
- private _simpleCheckDefinedName(formulaString: string) {
- const definedNameMap = this._formulaCurrentConfigService.getDirtyDefinedNameMap();
- const executeUnitId = this._formulaCurrentConfigService.getExecuteUnitId();
- if (executeUnitId != null && definedNameMap[executeUnitId] != null) {
- const names = Object.keys(definedNameMap[executeUnitId]!);
- for (let i = 0, len = names.length; i < len; i++) {
- const name = names[i];
- if (formulaString.indexOf(name) > -1) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- private _checkDefinedNameDirty(token: string) {
- const definedNameMap = this._formulaCurrentConfigService.getDirtyDefinedNameMap();
- const executeUnitId = this._formulaCurrentConfigService.getExecuteUnitId();
- if (executeUnitId != null && definedNameMap[executeUnitId] != null) {
- const names = Object.keys(definedNameMap[executeUnitId]!);
- for (let i = 0, len = names.length; i < len; i++) {
- const name = names[i];
- if (name === token) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- private _injectDefinedName(sequenceArray: ISequenceArray[]) {
- const unitId = this._formulaCurrentConfigService.getExecuteUnitId();
-
- if (unitId == null) {
- return {
- sequenceString: '',
- hasDefinedName: false,
- definedNames: [],
- };
- }
-
- const sequenceNodes = this._lexerTreeBuilder.getSequenceNode(sequenceArray);
- let sequenceString = '';
- let hasDefinedName = false;
- const definedNames: string[] = [];
-
- for (let i = 0, len = sequenceNodes.length; i < len; i++) {
- const node = sequenceNodes[i];
- if (typeof node === 'string') {
- sequenceString += node;
- continue;
- }
-
- const { nodeType, token } = node;
-
- if (nodeType === sequenceNodeType.REFERENCE || nodeType === sequenceNodeType.FUNCTION) {
- const definedContent = this._definedNamesService.getValueByName(unitId, token);
- if (definedContent) {
- let refString = definedContent.formulaOrRefString;
- if (refString.substring(0, 1) === operatorToken.EQUALS) {
- refString = refString.substring(1);
- }
- sequenceString += refString;
- definedNames.push(definedContent.name);
- hasDefinedName = true;
- } else if (this._checkDefinedNameDirty(token)) {
- sequenceString += ErrorType.NAME;
- hasDefinedName = true;
- definedNames.push(token);
- } else {
- sequenceString += token;
- }
- } else {
- sequenceString += token;
- }
- }
-
- return {
- sequenceString,
- hasDefinedName,
- definedNames,
- };
+ return this._lexerTreeBuilder.treeBuilder(formulaString, transformSuffix, {
+ unitId: this._formulaCurrentConfigService.getExecuteUnitId(),
+ getValueByName: this._definedNamesService.getValueByName.bind(this._definedNamesService),
+ getDirtyDefinedNameMap: this._formulaCurrentConfigService.getDirtyDefinedNameMap.bind(this._formulaCurrentConfigService),
+ });
}
}
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
index 3ef893ee5e..c7c6d9df90 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
@@ -209,14 +209,16 @@ export const DefinedNameContainer = () => {
<div
key={index}
className={`
- univer-divide-x-0 univer-divide-y univer-divide-solid univer-divide-gray-200
+ univer-relative univer-w-full univer-divide-x-0 univer-divide-y univer-divide-solid
+ univer-divide-gray-200
dark:!univer-divide-gray-600
`}
>
<div
className={clsx(`
- univer-group univer-relative univer-flex univer-cursor-default univer-select-none
- univer-items-center univer-justify-between univer-rounded-md univer-p-2
+ univer-group univer-relative univer-flex univer-w-full univer-cursor-default
+ univer-select-none univer-items-center univer-justify-between univer-rounded-md
+ univer-p-2
hover:univer-bg-gray-50
dark:hover:!univer-bg-gray-700
`, {
@@ -239,9 +241,11 @@ export const DefinedNameContainer = () => {
</div>
<div
className={`
- univer-my-1 univer-max-h-[100px] univer-overflow-hidden univer-text-ellipsis
- univer-text-xs univer-text-gray-500
+ univer-my-1 univer-max-h-[100px] univer-w-full univer-max-w-[190px]
+ univer-overflow-hidden univer-text-ellipsis univer-text-xs
+ univer-text-gray-500
`}
+ title={definedName.formulaOrRefString}
>
{definedName.formulaOrRefString}
</div>