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; } } });