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(cell): fix cell format of rich text (#4261)

wpxp12345691c201aa0aae
diff --git a/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts b/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
index a7bcbb453f..3217350b23 100644
--- a/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
+++ b/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
@@ -39,6 +39,7 @@ import { Choose } from '../lookup/choose';
import { FUNCTION_NAMES_LOOKUP } from '../lookup/function-names';
import { Xlookup } from '../lookup/xlookup';
import { Xmatch } from '../lookup/xmatch';
+import { Fact } from '../math/fact';
import { FUNCTION_NAMES_MATH } from '../math/function-names';
import { Product } from '../math/product';
import { Sum } from '../math/sum';
@@ -268,6 +269,52 @@ const getFunctionsTestWorkbookData = (): IWorkbookData => {
t: 4,
},
},
+ 12: {
+ 0: {
+ v: '10',
+ t: 1,
+ p: {
+ id: '__INTERNAL_EDITOR__DOCS_NORMAL',
+ documentStyle: {
+ pageSize: {
+ width: 73,
+ height: undefined,
+ },
+ marginTop: 1,
+ marginBottom: 2,
+ marginRight: 2,
+ marginLeft: 2,
+ renderConfig: {
+ horizontalAlign: 0,
+ verticalAlign: 0,
+ centerAngle: 0,
+ vertexAngle: 0,
+ wrapStrategy: 0,
+ },
+ },
+ body: {
+ dataStream: '10\r\n',
+ textRuns: [
+ { ts: { ff: 'Arial', fs: 11 }, st: 0, ed: 1 },
+ { st: 1, ed: 2, ts: { ff: 'Arial', fs: 11, cl: { rgb: '#B20000' } } },
+ ],
+ paragraphs: [
+ { startIndex: 2, paragraphStyle: { horizontalAlign: 0 } },
+ ],
+ sectionBreaks: [
+ { startIndex: 3 },
+ ],
+ customRanges: [],
+ customDecorations: [],
+ },
+ drawings: {},
+ drawingsOrder: [],
+ settings: {
+ zoomRatio: 1,
+ },
+ },
+ },
+ },
},
},
},
@@ -345,7 +392,8 @@ describe('Test nested functions', () => {
new Choose(FUNCTION_NAMES_LOOKUP.CHOOSE),
new Len(FUNCTION_NAMES_TEXT.LEN),
new Divided(FUNCTION_NAMES_META.DIVIDED),
- new Product(FUNCTION_NAMES_MATH.PRODUCT)
+ new Product(FUNCTION_NAMES_MATH.PRODUCT),
+ new Fact(FUNCTION_NAMES_MATH.FACT)
);
calculate = (formula: string) => {
@@ -458,5 +506,15 @@ describe('Test nested functions', () => {
result = calculate('=PRODUCT("2")');
expect(result).toBe(2);
});
+
+ it('value is rich text', () => {
+ let result = calculate('=SUM(A13)');
+ expect(result).toStrictEqual(0);
+
+ result = calculate('=FACT(A13)');
+ expect(result).toStrictEqual([
+ [3628800],
+ ]);
+ });
});
});
diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
index 1dd5e06d5f..dcf5059631 100644
--- a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
+++ b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
@@ -261,6 +261,118 @@ describe('Test EndEditController', () => {
...target,
});
});
+ it('Text cell input richText', () => {
+ const cell: ICellData = {
+ s: {
+ n: {
+ pattern: '@@@',
+ },
+ },
+ t: 1,
+ v: '10',
+ };
+
+ const inputCell = {
+ p: {
+ id: '__INTERNAL_EDITOR__DOCS_NORMAL',
+ documentStyle: {
+ pageSize: {
+ width: 73,
+ height: undefined,
+ },
+ marginTop: 1,
+ marginBottom: 2,
+ marginRight: 2,
+ marginLeft: 2,
+ renderConfig: {
+ horizontalAlign: 0,
+ verticalAlign: 0,
+ centerAngle: 0,
+ vertexAngle: 0,
+ wrapStrategy: 0,
+ },
+ },
+ body: {
+ dataStream: '10\r\n',
+ textRuns: [
+ { ts: { ff: 'Arial', fs: 11 }, st: 0, ed: 1 },
+ { st: 1, ed: 2, ts: { ff: 'Arial', fs: 11, cl: { rgb: '#B20000' } } },
+ ],
+ paragraphs: [
+ { startIndex: 2, paragraphStyle: { horizontalAlign: 0 } },
+ ],
+ sectionBreaks: [
+ { startIndex: 3 },
+ ],
+ customRanges: [],
+ customDecorations: [],
+ },
+ drawings: {},
+ drawingsOrder: [],
+ settings: {
+ zoomRatio: 1,
+ },
+ },
+ };
+
+ const cellData = getCellDataByInputCell(cell, inputCell);
+ const target = {
+ v: '10',
+ t: CellValueType.STRING,
+ s: {
+ n: {
+ pattern: '@@@',
+ },
+ },
+ f: null,
+ si: null,
+ p: {
+ id: '__INTERNAL_EDITOR__DOCS_NORMAL',
+ documentStyle: {
+ pageSize: {
+ width: Infinity,
+ height: Infinity,
+ },
+ marginTop: 0,
+ marginBottom: 2,
+ marginRight: 2,
+ marginLeft: 2,
+ renderConfig: {
+ horizontalAlign: 0,
+ verticalAlign: 0,
+ centerAngle: 0,
+ vertexAngle: 0,
+ wrapStrategy: 0,
+ zeroWidthParagraphBreak: 1,
+ },
+ },
+ body: {
+ dataStream: '10\r\n',
+ textRuns: [
+ { ts: { ff: 'Arial', fs: 11 }, st: 0, ed: 1 },
+ { st: 1, ed: 2, ts: { ff: 'Arial', fs: 11, cl: { rgb: '#B20000' } } },
+ ],
+ paragraphs: [
+ { startIndex: 2, paragraphStyle: { horizontalAlign: 0 } },
+ ],
+ sectionBreaks: [
+ { startIndex: 3 },
+ ],
+ customRanges: [],
+ customDecorations: [],
+ },
+ drawings: {},
+ drawingsOrder: [],
+ settings: {
+ zoomRatio: 1,
+ },
+ },
+ };
+
+ expect(cellData).toEqual({
+ ...target,
+ });
+ });
it('Rich text cell', () => {
const cell = {
v: 1,
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 33db40272e..d4f8459e66 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -728,6 +728,7 @@ export class EditingRenderController extends Disposable implements IRenderModule
}
}
+// eslint-disable-next-line
export function getCellDataByInput(
cellData: ICellData,
documentDataModel: Nullable<DocumentDataModel>,
@@ -774,6 +775,17 @@ export function getCellDataByInput(
cellData.si = null;
cellData.p = null;
cellData.t = CellValueType.STRING;
+
+ // Text format supports rich text
+ if (isRichText(body) && body.dataStream !== '\r\n') {
+ cellData.p = snapshot;
+ }
+
+ // If the style length in textRun.ts is equal to the content length, it should be set as the cell style
+ const style = getCellStyleBySnapshot(snapshot);
+ if (style) {
+ cellData.s = style;
+ }
} else if (isFormulaString(newDataStream)) {
if (cellData.f === newDataStream) {
return null;