Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cell): fix cell string type #3872

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
toDisposable,
UniverInstanceType,
} from '@univerjs/core';
import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt';
import {
factoryRemoveNumfmtUndoMutation,
factorySetNumfmtUndoMutation,
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions packages/sheets-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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,102 @@ 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('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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -724,13 +726,15 @@ export class EditingRenderController extends Disposable implements IRenderModule
}
}

// eslint-disable-next-line
export function getCellDataByInput(
cellData: ICellData,
documentDataModel: Nullable<DocumentDataModel>,
lexerTreeBuilder: LexerTreeBuilder,
getSnapshot: (data: DocumentDataModel) => IDocumentData,
localeService: LocaleService,
functionService: IFunctionService
functionService: IFunctionService,
styles: Styles
) {
cellData = Tools.deepClone(cellData);

Expand All @@ -754,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;
}
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
Loading
Loading