From c9b4dfc928e3bb3290996eb2ccb11a15ccae9cd5 Mon Sep 17 00:00:00 2001 From: wpxp123456 Date: Tue, 29 Oct 2024 14:27:43 +0800 Subject: [PATCH] fix(formula): fix cell string type --- .../__tests__/end-edit.controller.spec.ts | 32 +++++++++++++++++++ .../editor/editing.render-controller.ts | 17 +++++----- 2 files changed, 41 insertions(+), 8 deletions(-) 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 a68d70dd574..1fac1af46a6 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 @@ -231,6 +231,38 @@ describe('Test EndEditController', () => { ...target, }); }); + it('Text cell input formula', () => { + const cell: ICellData = { + s: { + n: { + pattern: '@@@', + }, + }, + t: null, + }; + + const inputCell = { + v: '=SUM(1)', + }; + + const cellData = getCellDataByInputCell(cell, inputCell); + const target = { + v: '=SUM(1)', + t: CellValueType.STRING, + s: { + n: { + pattern: '@@@', + }, + }, + f: null, + si: null, + p: null, + }; + + 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 9d283f1a14f..b0e34164e94 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -758,7 +758,15 @@ export function getCellDataByInput( const currentLocale = localeService.getCurrentLocale(); newDataStream = normalizeString(newDataStream, lexerTreeBuilder, currentLocale, functionService); - if (isFormulaString(newDataStream)) { + // Text format ('@@@') has the highest priority + if (cellData.s && styles?.get(cellData.s)?.n?.pattern === DEFAULT_TEXT_FORMAT) { + // If the style is text format ('@@@'), the data should be set as a string. + cellData.v = newDataStream; + cellData.f = null; + cellData.si = null; + cellData.p = null; + cellData.t = CellValueType.STRING; + } else if (isFormulaString(newDataStream)) { if (cellData.f === newDataStream) { return null; } @@ -791,13 +799,6 @@ export function getCellDataByInput( cellData.f = null; cellData.si = null; } - } else if (cellData.s && styles?.get(cellData.s)?.n?.pattern === DEFAULT_TEXT_FORMAT) { - // If the style is text format ('@@@'), the data should be set as a string. - cellData.v = newDataStream; - cellData.f = null; - cellData.si = null; - cellData.p = null; - cellData.t = CellValueType.STRING; } else if (numfmt.parseDate(newDataStream) || numfmt.parseNumber(newDataStream) || numfmt.parseTime(newDataStream)) { // If it can be converted to a number and is not forced to be a string, then the style should keep prev style. cellData.v = newDataStream;