Skip to content

Commit

Permalink
fix(formula): formula dependency (#4224)
Browse files Browse the repository at this point in the history
  • Loading branch information
DR-Univer authored Dec 6, 2024
1 parent 96efb3d commit 6dd4b16
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
14 changes: 12 additions & 2 deletions packages/engine-formula/src/engine/analysis/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ export class AstTreeBuilder extends Disposable {

let astNode: Nullable<BaseAstNode> = null;
if (item instanceof LexerNode) {
if (item.getToken() === DEFAULT_TOKEN_TYPE_PARAMETER && item.getChildren().length === 0) {
// =trim( ) is #NAME?
const children = item.getParent()?.getChildren();
if (children && children.length === 1) {
return ErrorNode.create(ErrorType.NAME);
}
}

astNode = this._parse(item, currentAstNode);
if (astNode === currentAstNode) {
continue;
Expand All @@ -282,9 +290,11 @@ export class AstTreeBuilder extends Disposable {
}

astNode = getAstNodeTopParent(astNode);
if (astNode == null) {
return;

if (astNode == null || astNode?.nodeType === NodeType.ERROR) {
return astNode;
}

// console.log('bugfix1', astNode, astNode.nodeType, currentAstNode, lexerNode);
switch (astNode.nodeType) {
// case NodeType.ERROR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class FunctionNode extends BaseAstNode {
private _definedNamesService: IDefinedNamesService,
private _formulaDataModel: FormulaDataModel
) {
super('');
super(token);

if (this._functionExecutor.isAsync()) {
this.setAsync();
Expand Down
23 changes: 0 additions & 23 deletions packages/engine-formula/src/engine/utils/value-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,7 @@ export enum ReferenceObjectType {
ROW,
}

const referenceObjectFromCache: Map<string, BaseReferenceObject> = new Map();

export function getReferenceObjectFromCache(trimToken: string, type: ReferenceObjectType) {
const o = referenceObjectFromCache.get(trimToken);
if (o) {
return o;
}

let referenceObject: BaseReferenceObject;
switch (type) {
case ReferenceObjectType.CELL:
Expand All @@ -322,34 +315,18 @@ export function getReferenceObjectFromCache(trimToken: string, type: ReferenceOb
throw new Error('Unknown reference object type');
}

referenceObjectFromCache.set(trimToken, referenceObject);

return referenceObject;
}

export function getRangeReferenceObjectFromCache(variant1: BaseReferenceObject, variant2: BaseReferenceObject) {
const key = `${variant1.getToken()}:${variant2.getToken()}`;
const o = referenceObjectFromCache.get(key);
if (o) {
const { x, y } = variant1.getRefOffset();
o.setRefOffset(x, y);
return o;
}
let referenceObject: FunctionVariantType = ErrorValueObject.create(ErrorType.NAME);
if (variant1.isCell() && variant2.isCell()) {
referenceObject = variant1.unionBy(variant2) as BaseReferenceObject;
referenceObjectFromCache.set(key, referenceObject as BaseReferenceObject);
} else if (variant1.isRow() && variant2.isRow()) {
referenceObject = variant1.unionBy(variant2) as BaseReferenceObject;
referenceObjectFromCache.set(key, referenceObject as BaseReferenceObject);
} else if (variant1.isColumn() && variant2.isColumn()) {
referenceObject = variant1.unionBy(variant2) as BaseReferenceObject;
referenceObjectFromCache.set(key, referenceObject as BaseReferenceObject);
}

return referenceObject;
}

export function clearReferenceObjectCache() {
referenceObjectFromCache.clear();
}
4 changes: 2 additions & 2 deletions packages/engine-formula/src/functions/lookup/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { ErrorType } from '../../../basics/error-type';
import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NullValueObject, NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
Expand Down
3 changes: 1 addition & 2 deletions packages/engine-formula/src/services/runtime.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { getRuntimeFeatureCell } from '../engine/utils/get-runtime-feature-cell'

import { clearNumberFormatTypeCache, clearStringToNumberPatternCache } from '../engine/utils/numfmt-kit';
import { clearReferenceToRangeCache } from '../engine/utils/reference-cache';
import { clearReferenceObjectCache, objectValueToCellValue } from '../engine/utils/value-object';
import { objectValueToCellValue } from '../engine/utils/value-object';
import { type BaseValueObject, ErrorValueObject } from '../engine/value-object/base-value-object';
import { IFormulaCurrentConfigService } from './current-data.service';

Expand Down Expand Up @@ -374,7 +374,6 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
clearNumberFormatTypeCache();
clearStringToNumberPatternCache();
clearReferenceToRangeCache();
clearReferenceObjectCache();
}

setCurrent(row: number, column: number, rowCount: number, columnCount: number, sheetId: string, unitId: string) {
Expand Down

0 comments on commit 6dd4b16

Please sign in to comment.