Skip to content

Commit

Permalink
fix(cell): fix cell string type
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 29, 2024
1 parent 165cba0 commit 4f2595c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions packages/sheets/src/services/__tests__/numfmt.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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', () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/sheets/src/services/numfmt/numfmt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
});
Expand Down

0 comments on commit 4f2595c

Please sign in to comment.