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): get rich text data stream as content (#1305)

Dushusir51ae0e1e7cd8
diff --git a/packages/engine-formula/src/engine/reference-object/base-reference-object.ts b/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
index 7975b6f100..9cceaf27ca 100644
--- a/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
+++ b/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
@@ -29,6 +29,7 @@ import {
NumberValueObject,
StringValueObject,
} from '../value-object/primitive-object';
+import { getCellValue } from '../utils/cell';
export type NodeValueType = BaseValueObject | BaseReferenceObject | AsyncObject | AsyncArrayObject;
@@ -332,7 +333,7 @@ export class BaseReferenceObject extends ObjectClassType {
}
getCellValueObject(cell: ICellData) {
- const value = cell.v || 0;
+ const value = getCellValue(cell);
if (ERROR_TYPE_SET.has(value as ErrorType)) {
return new ErrorValueObject(value as ErrorType);
}
diff --git a/packages/engine-formula/src/engine/utils/__tests__/cell.spec.ts b/packages/engine-formula/src/engine/utils/__tests__/cell.spec.ts
new file mode 100644
index 0000000000..f29c86e8ac
--- /dev/null
+++ b/packages/engine-formula/src/engine/utils/__tests__/cell.spec.ts
@@ -0,0 +1,46 @@
+/**
+ * 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 { describe, expect, it } from 'vitest';
+
+import type { ICellData } from '@univerjs/core';
+import { getCellValue } from '../cell';
+
+describe('Test cell', () => {
+ it('Function getCellValue', () => {
+ const cell1: ICellData = {
+ p: {
+ id: 'p',
+ body: {
+ dataStream: 'test\r\n',
+ },
+ documentStyle: {},
+ },
+ };
+
+ const cell2 = {
+ v: 2,
+ };
+
+ const cell3 = {
+ f: '',
+ };
+
+ expect(getCellValue(cell1)).toBe('test');
+ expect(getCellValue(cell2)).toBe(2);
+ expect(getCellValue(cell3)).toBe(0);
+ });
+});
diff --git a/packages/engine-formula/src/engine/utils/cell.ts b/packages/engine-formula/src/engine/utils/cell.ts
new file mode 100644
index 0000000000..fc539ee864
--- /dev/null
+++ b/packages/engine-formula/src/engine/utils/cell.ts
@@ -0,0 +1,40 @@
+/**
+ * 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 { DEFAULT_EMPTY_DOCUMENT_VALUE } from '@univerjs/core';
+import type { ICellData, Nullable } from '@univerjs/core';
+
+export function getCellValue(cell: Nullable<ICellData>) {
+ if (cell === null) {
+ return 0;
+ }
+
+ if (cell?.p) {
+ const body = cell?.p.body;
+
+ if (body == null) {
+ return 0;
+ }
+
+ const data = body.dataStream;
+ const lastString = data.substring(data.length - 2, data.length);
+ const newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;
+
+ return newDataStream;
+ }
+
+ return cell?.v || 0;
+}
diff --git a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
index 58d16ce2e8..a36fd9d8a1 100644
--- a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
+++ b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
@@ -181,6 +181,30 @@ describe('Test insert function operation', () => {
const cell = {};
expect(isNumberCell(cell)).toBeFalsy();
});
+ it('should return true when cell is rich text number', () => {
+ const cell = {
+ p: {
+ id: 'rich1',
+ documentStyle: {},
+ body: {
+ dataStream: '111/r/n',
+ },
+ },
+ };
+ expect(isNumberCell(cell)).toBeTruthy();
+ });
+ it('should return true when cell is rich text', () => {
+ const cell = {
+ p: {
+ id: 'rich1',
+ documentStyle: {},
+ body: {
+ dataStream: 'rich/r/n',
+ },
+ },
+ };
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
});
describe('function isSingleCell', () => {
diff --git a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
index a0304d710e..4020db3cf6 100644
--- a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
+++ b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
@@ -18,8 +18,10 @@ import type { ICellData, ICommand, IRange, Nullable, ObjectMatrix } from '@unive
import {
CellValueType,
CommandType,
+ DEFAULT_EMPTY_DOCUMENT_VALUE,
getCellValueType,
ICommandService,
+ isRealNum,
IUniverInstanceService,
Rectangle,
} from '@univerjs/core';
@@ -235,6 +237,19 @@ function findStartColumn(cellMatrix: ObjectMatrix<Nullable<ICellData>>, row: num
}
export function isNumberCell(cell: Nullable<ICellData>) {
+ if (cell?.p) {
+ const body = cell?.p.body;
+
+ if (body == null) {
+ return false;
+ }
+
+ const data = body.dataStream;
+ const lastString = data.substring(data.length - 2, data.length);
+ const newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;
+
+ return isRealNum(newDataStream);
+ }
return cell && (cell.t === CellValueType.NUMBER || getCellValueType(cell) === CellValueType.NUMBER);
}