From 165cba0ac51e5b9b62be306d4c815afb5a71326f Mon Sep 17 00:00:00 2001 From: wpxp123456 Date: Mon, 28 Oct 2024 19:58:07 +0800 Subject: [PATCH 1/3] fix(cell): fix cell string type --- .../controllers/numfmt.editor.controller.ts | 7 +++- packages/sheets-ui/package.json | 1 + .../editor/editing.render-controller.ts | 19 ++++++++-- pnpm-lock.yaml | 38 +++++++++---------- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts index 79917180be4..94d83a685c8 100644 --- a/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts +++ b/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts @@ -32,6 +32,7 @@ import { toDisposable, UniverInstanceType, } from '@univerjs/core'; +import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt'; import { factoryRemoveNumfmtUndoMutation, factorySetNumfmtUndoMutation, @@ -42,8 +43,8 @@ import { SheetInterceptorService, transformCellsToRange, } from '@univerjs/sheets'; -import { IEditorBridgeService } from '@univerjs/sheets-ui'; +import { IEditorBridgeService } from '@univerjs/sheets-ui'; import { getPatternType } from '../utils/pattern'; const createCollectEffectMutation = () => { @@ -167,7 +168,9 @@ export class NumfmtEditorController extends Disposable { null ); }; - if (!value?.v) { + + // if the value is empty or the current numfmt is text format, return the value directly + if (!value?.v || currentNumfmtValue?.pattern === DEFAULT_TEXT_FORMAT) { return next(value); } diff --git a/packages/sheets-ui/package.json b/packages/sheets-ui/package.json index dfaaa462722..4700dfe31f2 100644 --- a/packages/sheets-ui/package.json +++ b/packages/sheets-ui/package.json @@ -70,6 +70,7 @@ "@univerjs/docs": "workspace:*", "@univerjs/docs-ui": "workspace:*", "@univerjs/engine-formula": "workspace:*", + "@univerjs/engine-numfmt": "workspace:*", "@univerjs/engine-render": "workspace:*", "@univerjs/icons": "^0.1.84", "@univerjs/protocol": "0.1.39-alpha.36", 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 71d8e535285..9d283f1a14f 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -16,7 +16,7 @@ /* eslint-disable max-lines-per-function */ -import type { DocumentDataModel, ICellData, ICommandInfo, IDisposable, IDocumentBody, IDocumentData, IStyleData, Nullable, UnitModel, Workbook } from '@univerjs/core'; +import type { DocumentDataModel, ICellData, ICommandInfo, IDisposable, IDocumentBody, IDocumentData, IStyleData, Nullable, Styles, UnitModel, Workbook } from '@univerjs/core'; import type { IRichTextEditingMutationParams } from '@univerjs/docs'; import type { IRenderContext, IRenderModule } from '@univerjs/engine-render'; import type { WorkbookSelections } from '@univerjs/sheets'; @@ -49,12 +49,13 @@ import { } from '@univerjs/docs'; import { VIEWPORT_KEY as DOC_VIEWPORT_KEY, DocSelectionRenderService, IEditorService, MoveCursorOperation, MoveSelectionOperation } from '@univerjs/docs-ui'; import { IFunctionService, LexerTreeBuilder, matchToken } from '@univerjs/engine-formula'; +import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt'; + import { convertTextRotation, DeviceInputEventType, IRenderManagerService, } from '@univerjs/engine-render'; - import { COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetActivateCommand, SetWorksheetActiveOperation, SheetsSelectionsService } from '@univerjs/sheets'; import { KeyCode, SetEditorResizeOperation } from '@univerjs/ui'; import { distinctUntilChanged, filter } from 'rxjs'; @@ -557,7 +558,8 @@ export class EditingRenderController extends Disposable implements IRenderModule this._lexerTreeBuilder, (model) => model.getSnapshot(), this._localService, - this._functionService + this._functionService, + workbook.getStyles() ); if (!cellData) { @@ -724,13 +726,15 @@ export class EditingRenderController extends Disposable implements IRenderModule } } +// eslint-disable-next-line export function getCellDataByInput( cellData: ICellData, documentDataModel: Nullable, lexerTreeBuilder: LexerTreeBuilder, getSnapshot: (data: DocumentDataModel) => IDocumentData, localeService: LocaleService, - functionService: IFunctionService + functionService: IFunctionService, + styles: Styles ) { cellData = Tools.deepClone(cellData); @@ -787,6 +791,13 @@ 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; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7bf0f24810f..7e1421c6adf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,7 +10,7 @@ importers: devDependencies: '@antfu/eslint-config': specifier: 3.7.3 - version: 3.7.3(@eslint-react/eslint-plugin@1.14.3(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@vue/compiler-sfc@3.4.38)(eslint-plugin-format@0.1.2(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-hooks@5.0.0(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-refresh@0.4.12(eslint@9.12.0(jiti@1.21.6)))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(happy-dom@15.0.0)(jsdom@24.1.1)(less@4.2.0)(sass@1.77.5)(terser@5.31.6)) + version: 3.7.3(@eslint-react/eslint-plugin@1.14.3(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@vue/compiler-sfc@3.4.38)(eslint-plugin-format@0.1.2(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-hooks@5.0.0(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-refresh@0.4.12(eslint@9.12.0(jiti@1.21.6)))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(less@4.2.0)(sass@1.77.5)(terser@5.31.6)) '@commitlint/cli': specifier: ^19.5.0 version: 19.5.0(@types/node@22.7.5)(typescript@5.6.3) @@ -28,7 +28,7 @@ importers: version: 4.2.0(release-it@17.8.2(typescript@5.6.3)) '@release-it/conventional-changelog': specifier: ^8.0.2 - version: 8.0.2(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0)(release-it@17.8.2(typescript@5.6.3)) + version: 8.0.2(release-it@17.8.2(typescript@5.6.3)) '@storybook/react': specifier: 8.3.5 version: 8.3.5(@storybook/test@8.3.5(storybook@8.3.5))(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(storybook@8.3.5)(typescript@5.6.3) @@ -2802,6 +2802,9 @@ importers: '@univerjs/engine-formula': specifier: workspace:* version: link:../engine-formula + '@univerjs/engine-numfmt': + specifier: workspace:* + version: link:../engine-numfmt '@univerjs/engine-render': specifier: workspace:* version: link:../engine-render @@ -5227,14 +5230,14 @@ packages: '@univerjs/icons-svg@0.1.78': resolution: {integrity: sha512-9T9V6wtDkLJIJVCCKiOTgCyU8d9oQVjlK8saDxzyKEK8bWFVgSWIr7swIww0gHjVp0tJXiBRQNhcXMqJvCt2ZQ==} - '@univerjs/icons@0.1.84': - resolution: {integrity: sha512-fB13Qbn54xHHsX6p2QjI7QjvnLVP143h/4eTlI8BFUtoRA0Wp8Vo1R5mcZyU5sOtlD/nUNlyP27s7q2g8T89cw==} + '@univerjs/icons@0.1.80': + resolution: {integrity: sha512-6lR/dhGkGWbahgpdDdf/xEpDJxcRllIQTnknR9pNyo+4VdO/QrVaI3a2NgebY+0VQTD2rX5CuKKJhbkAdzXQdA==} peerDependencies: react: '*' react-dom: '*' - '@univerjs/icons@0.1.80': - resolution: {integrity: sha512-6lR/dhGkGWbahgpdDdf/xEpDJxcRllIQTnknR9pNyo+4VdO/QrVaI3a2NgebY+0VQTD2rX5CuKKJhbkAdzXQdA==} + '@univerjs/icons@0.1.84': + resolution: {integrity: sha512-fB13Qbn54xHHsX6p2QjI7QjvnLVP143h/4eTlI8BFUtoRA0Wp8Vo1R5mcZyU5sOtlD/nUNlyP27s7q2g8T89cw==} peerDependencies: react: '*' react-dom: '*' @@ -10558,7 +10561,7 @@ snapshots: dependencies: '@babel/runtime': 7.25.0 - '@antfu/eslint-config@3.7.3(@eslint-react/eslint-plugin@1.14.3(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@vue/compiler-sfc@3.4.38)(eslint-plugin-format@0.1.2(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-hooks@5.0.0(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-refresh@0.4.12(eslint@9.12.0(jiti@1.21.6)))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(happy-dom@15.0.0)(jsdom@24.1.1)(less@4.2.0)(sass@1.77.5)(terser@5.31.6))': + '@antfu/eslint-config@3.7.3(@eslint-react/eslint-plugin@1.14.3(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(@vue/compiler-sfc@3.4.38)(eslint-plugin-format@0.1.2(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-hooks@5.0.0(eslint@9.12.0(jiti@1.21.6)))(eslint-plugin-react-refresh@0.4.12(eslint@9.12.0(jiti@1.21.6)))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(less@4.2.0)(sass@1.77.5)(terser@5.31.6))': dependencies: '@antfu/install-pkg': 0.4.1 '@clack/prompts': 0.7.0 @@ -10567,7 +10570,7 @@ snapshots: '@stylistic/eslint-plugin': 2.8.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3) '@typescript-eslint/eslint-plugin': 8.7.0(@typescript-eslint/parser@8.8.1(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3) '@typescript-eslint/parser': 8.8.1(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3) - '@vitest/eslint-plugin': 1.1.4(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(happy-dom@15.0.0)(jsdom@24.1.1)(less@4.2.0)(sass@1.77.5)(terser@5.31.6)) + '@vitest/eslint-plugin': 1.1.4(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(less@4.2.0)(sass@1.77.5)(terser@5.31.6)) eslint: 9.12.0(jiti@1.21.6) eslint-config-flat-gitignore: 0.3.0(eslint@9.12.0(jiti@1.21.6)) eslint-flat-config-utils: 0.4.0 @@ -10891,13 +10894,10 @@ snapshots: '@types/conventional-commits-parser': 5.0.0 chalk: 5.3.0 - '@conventional-changelog/git-client@1.0.1(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0)': + '@conventional-changelog/git-client@1.0.1': dependencies: '@types/semver': 7.5.8 semver: 7.6.3 - optionalDependencies: - conventional-commits-filter: 4.0.0 - conventional-commits-parser: 5.0.0 '@dprint/formatter@0.3.0': {} @@ -11593,12 +11593,12 @@ snapshots: walk-sync: 2.2.0 yaml: 2.4.5 - '@release-it/conventional-changelog@8.0.2(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0)(release-it@17.8.2(typescript@5.6.3))': + '@release-it/conventional-changelog@8.0.2(release-it@17.8.2(typescript@5.6.3))': dependencies: concat-stream: 2.0.0 conventional-changelog: 5.1.0 conventional-recommended-bump: 9.0.0 - git-semver-tags: 8.0.0(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0) + git-semver-tags: 8.0.0 release-it: 17.8.2(typescript@5.6.3) semver: 7.6.3 transitivePeerDependencies: @@ -12459,12 +12459,12 @@ snapshots: '@univerjs/icons-svg@0.1.78': {} - '@univerjs/icons@0.1.84(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@univerjs/icons@0.1.80(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: react: 18.3.1 react-dom: 18.3.1(react@18.3.1) - '@univerjs/icons@0.1.80(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@univerjs/icons@0.1.84(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: react: 18.3.1 react-dom: 18.3.1(react@18.3.1) @@ -12506,7 +12506,7 @@ snapshots: transitivePeerDependencies: - supports-color - '@vitest/eslint-plugin@1.1.4(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(happy-dom@15.0.0)(jsdom@24.1.1)(less@4.2.0)(sass@1.77.5)(terser@5.31.6))': + '@vitest/eslint-plugin@1.1.4(@typescript-eslint/utils@8.7.0(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3))(eslint@9.12.0(jiti@1.21.6))(typescript@5.6.3)(vitest@2.1.2(@types/node@22.7.5)(less@4.2.0)(sass@1.77.5)(terser@5.31.6))': dependencies: eslint: 9.12.0(jiti@1.21.6) optionalDependencies: @@ -14934,9 +14934,9 @@ snapshots: meow: 12.1.1 semver: 7.6.3 - git-semver-tags@8.0.0(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0): + git-semver-tags@8.0.0: dependencies: - '@conventional-changelog/git-client': 1.0.1(conventional-commits-filter@4.0.0)(conventional-commits-parser@5.0.0) + '@conventional-changelog/git-client': 1.0.1 meow: 13.2.0 transitivePeerDependencies: - conventional-commits-filter From 4f2595c5f45138c1e5984919b15c7151cf7462d3 Mon Sep 17 00:00:00 2001 From: wpxp123456 Date: Tue, 29 Oct 2024 11:40:31 +0800 Subject: [PATCH 2/3] fix(cell): fix cell string type --- .../__tests__/end-edit.controller.spec.ts | 77 ++++++++++++++++++- .../services/__tests__/numfmt.service.test.ts | 12 +-- .../src/services/numfmt/numfmt.service.ts | 14 ++-- 3 files changed, 88 insertions(+), 15 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 cc4189dbba5..a68d70dd574 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 @@ -15,6 +15,7 @@ */ import type { ICellData, IDocumentData, Injector, Univer, Workbook } from '@univerjs/core'; +import type { IFunctionService } from '@univerjs/engine-formula'; import { CellValueType, IConfigService, IContextService, IResourceLoaderService, LocaleService, LocaleType, Tools } from '@univerjs/core'; import { LexerTreeBuilder } from '@univerjs/engine-formula'; import { SpreadsheetSkeleton } from '@univerjs/engine-render'; @@ -127,8 +128,16 @@ describe('Test EndEditController', () => { if (!documentLayoutObject) { throw new Error('documentLayoutObject is undefined'); } - // @ts-ignore - return getCellDataByInput(cell, documentLayoutObject.documentModel, lexerTreeBuilder, (model) => model.getSnapshot(), localeService, get(IMockFunctionService)); + + return getCellDataByInput( + cell, + documentLayoutObject.documentModel, + lexerTreeBuilder, + (model) => model.getSnapshot(), + localeService, + get(IMockFunctionService) as IFunctionService, + workbook.getStyles() + ); }; normalizeStringByLexer = (str: string) => { @@ -158,6 +167,70 @@ describe('Test EndEditController', () => { ...target, }); }); + it('Text cell input 001', () => { + const cell: ICellData = { + s: { + n: { + pattern: '@@@', + }, + }, + t: null, + }; + + const inputCell = { + v: '001', + }; + + const cellData = getCellDataByInputCell(cell, inputCell); + const target = { + v: '001', + t: CellValueType.STRING, + s: { + n: { + pattern: '@@@', + }, + }, + f: null, + si: null, + p: null, + }; + + expect(cellData).toEqual({ + ...target, + }); + }); + it('Text cell input 2024-10-28', () => { + const cell: ICellData = { + s: { + n: { + pattern: '@@@', + }, + }, + t: null, + }; + + const inputCell = { + v: '2024-10-28', + }; + + const cellData = getCellDataByInputCell(cell, inputCell); + const target = { + v: '2024-10-28', + 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/src/services/__tests__/numfmt.service.test.ts b/packages/sheets/src/services/__tests__/numfmt.service.test.ts index 6bd546a6bc4..f6c790ba362 100644 --- a/packages/sheets/src/services/__tests__/numfmt.service.test.ts +++ b/packages/sheets/src/services/__tests__/numfmt.service.test.ts @@ -15,11 +15,11 @@ */ import type { Injector, Styles, Univer, Workbook, Worksheet } from '@univerjs/core'; -import { cellToRange, CellValueType, ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core'; import type { IRemoveNumfmtMutationParams, ISetNumfmtMutationParams } from '@univerjs/sheets'; -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; - +import { cellToRange, CellValueType, ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core'; import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt'; + +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { RemoveNumfmtMutation, SetNumfmtMutation } from '../../commands/mutations/numfmt-mutation'; import { NumfmtService } from '../numfmt/numfmt.service'; import { INumfmtService } from '../numfmt/type'; @@ -118,10 +118,12 @@ describe('test numfmt service', () => { const cell = sheet.getCellRaw(0, 5); const numfmtId = cell?.s; expect(styles.get(numfmtId)?.n).toEqual({ pattern: DEFAULT_TEXT_FORMAT }); - expect(cell).toStrictEqual({ v: '1', t: CellValueType.STRING, s: numfmtId }); + expect(cell).toStrictEqual({ v: 1, t: CellValueType.STRING, s: numfmtId }); }); it('model set, text format contains number, to number format', () => { + // text format set to percentage format, value is not changed, t is not changed, only style is changed + // Re-enter a number so that the cell Only then display the percentage const params: ISetNumfmtMutationParams = { unitId, subUnitId, @@ -139,7 +141,7 @@ describe('test numfmt service', () => { const cell = sheet.getCellRaw(0, 6); const numfmtId = cell?.s; expect(styles.get(numfmtId)?.n).toEqual({ pattern: '0%' }); - expect(cell).toStrictEqual({ v: 1, t: CellValueType.NUMBER, s: numfmtId }); + expect(cell).toStrictEqual({ v: '001', t: CellValueType.STRING, s: numfmtId }); }); it('model set, text format contains text, to number format', () => { diff --git a/packages/sheets/src/services/numfmt/numfmt.service.ts b/packages/sheets/src/services/numfmt/numfmt.service.ts index a78a3f27f6d..95b6b29a422 100644 --- a/packages/sheets/src/services/numfmt/numfmt.service.ts +++ b/packages/sheets/src/services/numfmt/numfmt.service.ts @@ -15,17 +15,17 @@ */ import type { IRange } from '@univerjs/core'; +import type { INumfmtService } from './type'; + import { + CellValueType, Disposable, ILogService, IResourceManagerService, IUniverInstanceService, Range, } from '@univerjs/core'; - -import { getCellTypeByPattern } from '../../basics/cell-type'; -import { getCellValue } from '../../basics/cell-value'; -import type { INumfmtService } from './type'; +import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt'; export class NumfmtService extends Disposable implements INumfmtService { constructor( @@ -114,10 +114,8 @@ export class NumfmtService extends Disposable implements INumfmtService { cell.s = styleId; // Setting the text format for a cell will set the CellValueType to text - const type = getCellTypeByPattern(cell, value.pattern); - if (cell.v !== undefined) { - cell.t = type; - cell.v = getCellValue(type, cell); + if (value.pattern === DEFAULT_TEXT_FORMAT) { + cell.t = CellValueType.STRING; } } }); From c9b4dfc928e3bb3290996eb2ccb11a15ccae9cd5 Mon Sep 17 00:00:00 2001 From: wpxp123456 Date: Tue, 29 Oct 2024 14:27:43 +0800 Subject: [PATCH 3/3] 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;