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): union error and null cell (#919)

Univerd59a48992bf4
diff --git a/packages/engine-formula/src/engine/ast-node/union-node.ts b/packages/engine-formula/src/engine/ast-node/union-node.ts
index d9ea68a7ec..4bba4656d5 100644
--- a/packages/engine-formula/src/engine/ast-node/union-node.ts
+++ b/packages/engine-formula/src/engine/ast-node/union-node.ts
@@ -16,24 +16,18 @@
import { ErrorType } from '../../basics/error-type';
import { matchToken } from '../../basics/token';
-import type { BaseFunction } from '../../functions/base-function';
-import { FUNCTION_NAMES_META } from '../../functions/meta/function-names';
import { IFunctionService } from '../../services/function.service';
import { LexerNode } from '../analysis/lexer-node';
import type { BaseReferenceObject, FunctionVariantType } from '../reference-object/base-reference-object';
-import type { BaseValueObject } from '../value-object/base-value-object';
import { ErrorValueObject } from '../value-object/base-value-object';
-import { BaseAstNode, ErrorNode } from './base-ast-node';
+import { BaseAstNode } from './base-ast-node';
import { BaseAstNodeFactory, DEFAULT_AST_NODE_FACTORY_Z_INDEX } from './base-ast-node-factory';
import { NODE_ORDER_MAP, NodeType } from './node-type';
-const UNION_EXECUTOR_NAME = 'UNION';
+// const UNION_EXECUTOR_NAME = 'UNION';
export class UnionNode extends BaseAstNode {
- constructor(
- private _operatorString: string,
- private _functionExecutor: BaseFunction
- ) {
+ constructor(private _operatorString: string) {
super(_operatorString);
}
@@ -43,8 +37,8 @@ export class UnionNode extends BaseAstNode {
override execute() {
const children = this.getChildren();
- let leftNode = children[0].getValue();
- let rightNode = children[1].getValue();
+ const leftNode = children[0].getValue();
+ const rightNode = children[1].getValue();
if (leftNode == null || rightNode == null) {
throw new Error('leftNode and rightNode');
@@ -52,23 +46,38 @@ export class UnionNode extends BaseAstNode {
let result: FunctionVariantType;
if (this._operatorString === matchToken.COLON) {
- if (leftNode.isReferenceObject()) {
- leftNode = (leftNode as BaseReferenceObject).toArrayValueObject();
- }
-
- if (rightNode.isReferenceObject()) {
- rightNode = (rightNode as BaseReferenceObject).toArrayValueObject();
- }
-
- result = this._functionExecutor.calculate(
- leftNode as BaseValueObject,
- rightNode as BaseValueObject
- ) as FunctionVariantType;
+ result = this._unionFunction(leftNode, rightNode) as FunctionVariantType;
} else {
result = ErrorValueObject.create(ErrorType.NAME);
}
this.setValue(result);
}
+
+ private _unionFunction(variant1: FunctionVariantType, variant2: FunctionVariantType) {
+ if (variant1.isError() || variant2.isError()) {
+ return ErrorValueObject.create(ErrorType.REF);
+ }
+
+ if (!variant1.isReferenceObject() || !variant2.isReferenceObject()) {
+ return ErrorValueObject.create(ErrorType.REF);
+ }
+
+ variant1 = variant1 as BaseReferenceObject;
+
+ variant2 = variant2 as BaseReferenceObject;
+
+ if (variant1.isCell() && variant2.isCell()) {
+ return variant1.unionBy(variant2);
+ }
+ if (variant1.isRow() && variant2.isRow()) {
+ return variant1.unionBy(variant2);
+ }
+ if (variant1.isColumn() && variant2.isColumn()) {
+ return variant1.unionBy(variant2);
+ }
+
+ return ErrorValueObject.create(ErrorType.REF);
+ }
}
export class UnionNodeFactory extends BaseAstNodeFactory {
@@ -81,12 +90,7 @@ export class UnionNodeFactory extends BaseAstNodeFactory {
}
override create(param: string): BaseAstNode {
- const functionExecutor = this._functionService.getExecutor(FUNCTION_NAMES_META.UNION);
- if (!functionExecutor) {
- console.error(`No function ${param}`);
- return ErrorNode.create(ErrorType.NAME);
- }
- return new UnionNode(param, functionExecutor);
+ return new UnionNode(param);
}
override checkAndCreateNodeType(param: LexerNode | string) {
diff --git a/packages/engine-formula/src/functions/meta/function-map.ts b/packages/engine-formula/src/functions/meta/function-map.ts
index 343414cdd0..a86bfee1c4 100644
--- a/packages/engine-formula/src/functions/meta/function-map.ts
+++ b/packages/engine-formula/src/functions/meta/function-map.ts
@@ -20,7 +20,6 @@ import { FUNCTION_NAMES_META } from './function-names';
import { Minus } from './minus';
import { Multiply } from './multiply';
import { Plus } from './plus';
-import { Union } from './union';
export const functionMeta = [
[Compare, FUNCTION_NAMES_META.COMPARE],
@@ -28,5 +27,4 @@ export const functionMeta = [
[Minus, FUNCTION_NAMES_META.MINUS],
[Multiply, FUNCTION_NAMES_META.MULTIPLY],
[Plus, FUNCTION_NAMES_META.PLUS],
- [Union, FUNCTION_NAMES_META.UNION],
];
diff --git a/packages/engine-formula/src/functions/meta/union/index.ts b/packages/engine-formula/src/functions/meta/union/index.ts
deleted file mode 100644
index 68980cf35e..0000000000
--- a/packages/engine-formula/src/functions/meta/union/index.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import { ErrorType } from '../../../basics/error-type';
-import { compareToken } from '../../../basics/token';
-import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { BaseFunction } from '../../base-function';
-
-export class Union extends BaseFunction {
- private _compareType: compareToken = compareToken.EQUALS;
-
- setCompareType(token: compareToken) {
- this._compareType = token;
- }
-
- override calculate(variant1: FunctionVariantType, variant2: FunctionVariantType) {
- if (variant1.isError() || variant2.isError()) {
- return ErrorValueObject.create(ErrorType.REF);
- }
-
- if (!variant1.isReferenceObject() || !variant2.isReferenceObject()) {
- return ErrorValueObject.create(ErrorType.REF);
- }
-
- variant1 = variant1 as BaseReferenceObject;
-
- variant2 = variant2 as BaseReferenceObject;
-
- if (variant1.isCell() && variant2.isCell()) {
- return variant1.unionBy(variant2);
- }
- if (variant1.isRow() && variant2.isRow()) {
- return variant1.unionBy(variant2);
- }
- if (variant1.isColumn() && variant2.isColumn()) {
- return variant1.unionBy(variant2);
- }
-
- return ErrorValueObject.create(ErrorType.REF);
- }
-}
diff --git a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts b/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
index 058b19c8cf..3eeb04467a 100644
--- a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
+++ b/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
@@ -25,7 +25,12 @@ import {
ThemeService,
toDisposable,
} from '@univerjs/core';
-import { FormulaDataModel, LexerTreeBuilder, SetFormulaCalculationResultMutation } from '@univerjs/engine-formula';
+import {
+ ErrorType,
+ FormulaDataModel,
+ LexerTreeBuilder,
+ SetFormulaCalculationResultMutation,
+} from '@univerjs/engine-formula';
import { IRenderManagerService } from '@univerjs/engine-render';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import {
@@ -66,7 +71,7 @@ export class FormulaEditorShowController extends Disposable {
this._editorBridgeService.interceptor.getInterceptPoints().BEFORE_CELL_EDIT,
{
handler: (value, context, next) => {
- const { row, col, unitId, subUnitId } = context;
+ const { row, col, unitId, subUnitId, worksheet } = context;
const arrayFormulaMatrixRange = this._formulaDataModel.getArrayFormulaRange();
const arrayFormulaMatrixCell = this._formulaDataModel.getArrayFormulaCellData();
@@ -147,6 +152,12 @@ export class FormulaEditorShowController extends Disposable {
return false;
}
if (row >= startRow && row <= endRow && col >= startColumn && col <= endColumn) {
+ const mainCellValue = worksheet.getCell(startRow, startColumn);
+
+ if (mainCellValue?.v === ErrorType.SPILL) {
+ return;
+ }
+
const formulaDataItem = this._formulaDataModel.getFormulaDataItem(
rowIndex,
columnIndex,
diff --git a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
index 8013e2b067..a068f09898 100644
--- a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
@@ -224,9 +224,15 @@ export class EndEditController extends Disposable {
cellData.v = null;
cellData.p = null;
} else if (isRichText(body)) {
- cellData.p = snapshot;
- cellData.v = null;
- cellData.f = null;
+ if (body.dataStream === '\r\n') {
+ cellData.v = '';
+ cellData.f = null;
+ cellData.p = null;
+ } else {
+ cellData.p = snapshot;
+ cellData.v = null;
+ cellData.f = null;
+ }
} else {
// eslint-disable-next-line
if (newDataStream == cellData.v || (newDataStream == '' && cellData.v == null)) {