Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(cell): fix cell string type (#3872)

wpxp123456bc6587630447
diff --git a/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.editor.controller.ts
index 79917180be..94d83a685c 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 dfaaa46272..4700dfe31f 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/__tests__/end-edit.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
index cc4189dbba..1fac1af46a 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,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,
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 71d8e53528..b0e34164e9 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<DocumentDataModel>,
lexerTreeBuilder: LexerTreeBuilder,
getSnapshot: (data: DocumentDataModel) => IDocumentData,
localeService: LocaleService,
- functionService: IFunctionService
+ functionService: IFunctionService,
+ styles: Styles
) {
cellData = Tools.deepClone(cellData);
@@ -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;
}
diff --git a/packages/sheets/src/services/__tests__/numfmt.service.test.ts b/packages/sheets/src/services/__tests__/numfmt.service.test.ts
index 6bd546a6bc..f6c790ba36 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 a78a3f27f6..95b6b29a42 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;
}
}
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 7bf0f24810..7e1421c6ad 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -10,7 +10,7 @@ importers:
devDependencies:
'@antfu/eslint-config':
specifier: 3.7.3
'@commitlint/cli':
specifier: ^19.5.0
version: 19.5.0(@types/[email protected])([email protected])
@@ -28,7 +28,7 @@ importers:
'@release-it/conventional-changelog':
specifier: ^8.0.2
'@storybook/react':
specifier: 8.3.5
@@ -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/[email protected]':
resolution: {integrity: sha512-9T9V6wtDkLJIJVCCKiOTgCyU8d9oQVjlK8saDxzyKEK8bWFVgSWIr7swIww0gHjVp0tJXiBRQNhcXMqJvCt2ZQ==}
- '@univerjs/[email protected]':
- resolution: {integrity: sha512-fB13Qbn54xHHsX6p2QjI7QjvnLVP143h/4eTlI8BFUtoRA0Wp8Vo1R5mcZyU5sOtlD/nUNlyP27s7q2g8T89cw==}
+ '@univerjs/[email protected]':
+ resolution: {integrity: sha512-6lR/dhGkGWbahgpdDdf/xEpDJxcRllIQTnknR9pNyo+4VdO/QrVaI3a2NgebY+0VQTD2rX5CuKKJhbkAdzXQdA==}
peerDependencies:
react: '*'
react-dom: '*'
- '@univerjs/[email protected]':
- resolution: {integrity: sha512-6lR/dhGkGWbahgpdDdf/xEpDJxcRllIQTnknR9pNyo+4VdO/QrVaI3a2NgebY+0VQTD2rX5CuKKJhbkAdzXQdA==}
+ '@univerjs/[email protected]':
+ resolution: {integrity: sha512-fB13Qbn54xHHsX6p2QjI7QjvnLVP143h/4eTlI8BFUtoRA0Wp8Vo1R5mcZyU5sOtlD/nUNlyP27s7q2g8T89cw==}
peerDependencies:
react: '*'
react-dom: '*'
@@ -10558,7 +10561,7 @@ snapshots:
dependencies:
'@babel/runtime': 7.25.0
dependencies:
'@antfu/install-pkg': 0.4.1
'@clack/prompts': 0.7.0
@@ -10567,7 +10570,7 @@ snapshots:
'@stylistic/eslint-plugin': 2.8.0([email protected]([email protected]))([email protected])
'@typescript-eslint/parser': 8.8.1([email protected]([email protected]))([email protected])
eslint: 9.12.0([email protected])
eslint-config-flat-gitignore: 0.3.0([email protected]([email protected]))
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/[email protected]':
dependencies:
'@types/semver': 7.5.8
semver: 7.6.3
- optionalDependencies:
- conventional-commits-filter: 4.0.0
- conventional-commits-parser: 5.0.0
'@dprint/[email protected]': {}
@@ -11593,12 +11593,12 @@ snapshots:
walk-sync: 2.2.0
yaml: 2.4.5
dependencies:
concat-stream: 2.0.0
conventional-changelog: 5.1.0
conventional-recommended-bump: 9.0.0
- git-semver-tags: 8.0.0([email protected])([email protected])
+ git-semver-tags: 8.0.0
release-it: 17.8.2([email protected])
semver: 7.6.3
transitivePeerDependencies:
@@ -12459,12 +12459,12 @@ snapshots:
'@univerjs/[email protected]': {}
dependencies:
react: 18.3.1
react-dom: 18.3.1([email protected])
dependencies:
react: 18.3.1
react-dom: 18.3.1([email protected])
@@ -12506,7 +12506,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
eslint: 9.12.0([email protected])
optionalDependencies:
@@ -14934,9 +14934,9 @@ snapshots:
meow: 12.1.1
semver: 7.6.3
dependencies:
- '@conventional-changelog/git-client': 1.0.1([email protected])([email protected])
+ '@conventional-changelog/git-client': 1.0.1
meow: 13.2.0
transitivePeerDependencies:
- conventional-commits-filter