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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: fix the issue of applying number format when the cell type is string or force string (#5798)
wpxp1234568ebc55de8d75
diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.tsindex 5be9531278..51541149a6 100644--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts@@ -147,6 +147,10 @@ export class NumfmtEditorController extends Disposable {this._sheetInterceptorService.writeCellInterceptor.intercept(AFTER_CELL_EDIT, {// eslint-disable-next-line complexityhandler: (value, context, next) => {+ if (!value?.v && !value?.p) {+ return next(value);+ }+// clear the effectthis._collectEffectMutation.clean();const currentNumfmtValue = this._numfmtService.getValue(@@ -155,20 +159,8 @@ export class NumfmtEditorController extends Disposable {context.row,context.col);- // const currentNumfmtType = (currentNumfmtValue && getPatternType(currentNumfmtValue.pattern)) ?? '';- // const clean = () => {- // currentNumfmtValue &&- // this._collectEffectMutation.add(- // context.unitId,- // context.subUnitId,- // context.row,- // context.col,- // null- // );- // };- if (!value?.v && !value?.p) {- return next(value);- }++ const originCell = context.worksheet.getCellRaw(context.row, context.col);// if the cell is text format or force string, do not convert the valueif (isTextFormat(currentNumfmtValue?.pattern) || value.t === CellValueType.FORCE_STRING) {@@ -193,10 +185,18 @@ export class NumfmtEditorController extends Disposable {}if (numfmtInfo) {+ // If the content is parsed to a number format or the origin cell has a number format, do not need judge whether it will lose precision.+ // If the cell type is string or force string, do not need judge whether it will lose precision.// If the numeric string will lose precision when converted to a number, set the cell type to force string// e.g. 123456789123456789// e.g. 1212121212121212.2345- if (!numfmtInfo.z && willLoseNumericPrecision(content)) {+ if (+ !numfmtInfo.z &&+ !currentNumfmtValue?.pattern &&+ originCell?.t !== CellValueType.STRING &&+ originCell?.t !== CellValueType.FORCE_STRING &&+ willLoseNumericPrecision(content)+ ) {return next({...value,p: undefined,@@ -221,9 +221,7 @@ export class NumfmtEditorController extends Disposable {return next({ ...value, p: undefined, v, t: CellValueType.NUMBER });}- // else if (['date', 'time', 'datetime', 'percent'].includes(currentNumfmtType) || !isNumeric(content)) {- // clean();- // }+return next(value);},})diff --git a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.tsindex 2b5b9d062e..f2483243d9 100644--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts@@ -113,7 +113,9 @@ export class SheetsNumfmtCellContentController extends Disposable {// eslint-disable-next-line max-lines-per-function, complexityhandler: (cell, location, next) => {- if (!cell || cell.v === undefined || cell.v === null) {+ // If the cell is empty, or the cell type is boolean or force_string, do not process it.+ // Cell type is number or number string, it will be applied number format.+ if (!cell || cell.v === undefined || cell.v === null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) {return next(cell);}@@ -140,7 +142,7 @@ export class SheetsNumfmtCellContentController extends Disposable {// If the cell not specified number type, then check the cell value typeif (cell.t !== CellValueType.NUMBER) {- const type = checkCellValueType(cell.v);+ const type = checkCellValueType(cell.v, cell.t);// just handle number or number stringif (type !== CellValueType.NUMBER) {return next(cell);diff --git a/packages/sheets/src/basics/cell-type.ts b/packages/sheets/src/basics/cell-type.tsindex bbc476bc2f..f821fb29bf 100644--- a/packages/sheets/src/basics/cell-type.ts+++ b/packages/sheets/src/basics/cell-type.ts@@ -88,10 +88,11 @@ export function checkCellValueType(v: Nullable<CellValue>, type: Nullable<CellVareturn CellValueType.BOOLEAN;}+ // If the cell type is string or force string, do not need judge whether it will lose precision.// If the numeric string will lose precision when converted to a number, set the cell type to force string// e.g. 123456789123456789// e.g. 1212121212121212.2345- if (willLoseNumericPrecision(v)) {+ if (type !== CellValueType.STRING && type !== CellValueType.FORCE_STRING && willLoseNumericPrecision(v)) {return CellValueType.FORCE_STRING;}