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(sheets-numfmt): keep text-format numbers numeric (#7991)

wpxp1234560a42f7856144
diff --git a/packages/sheets-numfmt/src/controllers/__tests__/numfmt-cell-content.controller.spec.ts b/packages/sheets-numfmt/src/controllers/__tests__/numfmt-cell-content.controller.spec.ts
index b2147ee4e8..e0516cc6aa 100644
--- a/packages/sheets-numfmt/src/controllers/__tests__/numfmt-cell-content.controller.spec.ts
+++ b/packages/sheets-numfmt/src/controllers/__tests__/numfmt-cell-content.controller.spec.ts
@@ -82,6 +82,7 @@ function createWorkbookData(
2: { v: -12.3, t: CellValueType.NUMBER },
3: { v: '20%', t: CellValueType.STRING, s: 'textStyle' },
4: { v: 0, t: CellValueType.NUMBER },
+ 5: { v: 0.009999999999999787, t: CellValueType.NUMBER, s: 'textStyle' },
},
},
},
@@ -214,6 +215,18 @@ describe('SheetsNumfmtCellContentController', () => {
},
});
+ expect(getInterceptedCell(worksheet, workbook, 1, 5, get)).toEqual({
+ v: 0.01,
+ t: CellValueType.NUMBER,
+ s: 'textStyle',
+ markers: {
+ tl: {
+ size: 6,
+ color: '#409f11',
+ },
+ },
+ });
+
get(IConfigService).setConfig(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, { disableTextFormatMark: true });
expect(getInterceptedCell(worksheet, workbook, 1, 0, get)).toEqual({
@@ -226,6 +239,11 @@ describe('SheetsNumfmtCellContentController', () => {
t: CellValueType.STRING,
s: 'textStyle',
});
+ expect(getInterceptedCell(worksheet, workbook, 1, 5, get)).toEqual({
+ v: 0.01,
+ t: CellValueType.NUMBER,
+ s: 'textStyle',
+ });
});
it('formats numeric values and clears render cache on numfmt or value mutations', async () => {
diff --git a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
index cf81253be0..7269ecdaee 100644
--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
@@ -26,6 +26,7 @@ import {
Inject,
InterceptorEffectEnum,
isDefaultFormat,
+ isRealNum,
isTextFormat,
IUniverInstanceService,
LocaleService,
@@ -34,6 +35,7 @@ import {
ThemeService,
UniverInstanceType,
} from '@univerjs/core';
+import { stripErrorMargin } from '@univerjs/engine-formula';
import {
checkCellValueType,
InterceptCellContentPriority,
@@ -144,11 +146,7 @@ export class SheetsNumfmtCellContentController extends Disposable {
const formattedCell = cell === location.rawData ? { ...cell } : cell;
if (isTextFormat(pattern)) {
- formattedCell.t = CellValueType.STRING;
- if (!this._configService.getConfig<IUniverSheetsNumfmtConfig>(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY)?.disableTextFormatMark) {
- formattedCell.markers = { ...formattedCell.markers, ...TEXT_FORMAT_MARK };
- }
- return formattedCell;
+ return this._formatTextCell(cell, formattedCell);
}
const cacheParameters = `${unitId}_${sheetId}_${cell.v}_${pattern}_${locale}_${dateSystem}`;
@@ -174,6 +172,25 @@ export class SheetsNumfmtCellContentController extends Disposable {
return formattedCell;
}
+ private _formatTextCell(
+ cell: ICellDataForSheetInterceptor,
+ formattedCell: ICellDataForSheetInterceptor
+ ): ICellDataForSheetInterceptor {
+ // The cell stays text-formatted (keep its marker), but a numeric result
+ // must remain numeric and go through General's precision cleanup.
+ if (cell.t === CellValueType.NUMBER && isRealNum(formattedCell.v)) {
+ formattedCell.v = stripErrorMargin(Number(formattedCell.v));
+ } else {
+ formattedCell.t = CellValueType.STRING;
+ }
+
+ if (!this._configService.getConfig<IUniverSheetsNumfmtConfig>(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY)?.disableTextFormatMark) {
+ formattedCell.markers = { ...formattedCell.markers, ...TEXT_FORMAT_MARK };
+ }
+
+ return formattedCell;
+ }
+
private _isNumericCell(cell: ICellData, pattern: string | undefined, locale: INumfmtLocaleTag, dateSystem: DateSystem): boolean {
return cell.t === CellValueType.NUMBER || checkCellValueType(cell.v, cell.t) === CellValueType.NUMBER || (
isTextFormat(pattern) &&