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): preserve empty format results (#7383)

Univer55ce087a6619
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 96041592fc..99718ff09f 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
@@ -269,6 +269,24 @@ describe('SheetsNumfmtCellContentController', () => {
expect(previewSpy).toHaveBeenCalledTimes(previewCallsAfterFirstRender + 2);
});
+ it('applies and caches a legitimate empty number-format result', () => {
+ const worksheet = workbook.getSheetBySheetId('sheet1')!;
+ const previewSpy = vi.spyOn(patternUtils, 'getPatternPreviewIgnoreGeneral');
+
+ numfmtService.setValues('test', 'sheet1', [{ pattern: ';;;', ranges: [cellToRange(1, 1)] }]);
+
+ expect(getInterceptedCell(worksheet, workbook, 1, 1, get)).toMatchObject({
+ v: '',
+ t: CellValueType.NUMBER,
+ coverable: false,
+ });
+ expect(worksheet.getCellRaw(1, 1)).toMatchObject({ v: '1234.5', t: CellValueType.STRING });
+
+ const previewCallsAfterFirstRender = previewSpy.mock.calls.length;
+ getInterceptedCell(worksheet, workbook, 1, 1, get);
+ expect(previewSpy).toHaveBeenCalledTimes(previewCallsAfterFirstRender);
+ });
+
it('resets cached rendering when active sheet changes and supports locale override', () => {
const sheet1 = workbook.getSheetBySheetId('sheet1')!;
const sheet2 = workbook.getSheetBySheetId('sheet2')!;
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 3d9b5bf21d..f8052e06e2 100644
--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
@@ -185,19 +185,17 @@ export class SheetsNumfmtCellContentController extends Disposable {
return next(cell);
}
- let numfmtRes: string = '';
const cache = renderCache.getValue(location.row, location.col);
if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue?.pattern}`) {
return next({ ...cell, ...cache.result });
}
const info = getPatternPreviewIgnoreGeneral(numfmtValue?.pattern as string, Number(originCellValue.v), this.locale);
- numfmtRes = info.result;
- if (!numfmtRes) {
- return next(cell);
- }
-
+ const numfmtRes = info.result;
const res: ICellDataForSheetInterceptor = { v: numfmtRes, t: CellValueType.NUMBER };
+ if (numfmtRes === '') {
+ res.coverable = false;
+ }
if (info.color) {
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
diff --git a/packages/sheets-numfmt/src/facade/__tests__/f-range.spec.ts b/packages/sheets-numfmt/src/facade/__tests__/f-range.spec.ts
index 04eb40a07f..d14430f2de 100644
--- a/packages/sheets-numfmt/src/facade/__tests__/f-range.spec.ts
+++ b/packages/sheets-numfmt/src/facade/__tests__/f-range.spec.ts
@@ -16,7 +16,7 @@
import type { FUniver } from '@univerjs/core/facade';
import { ICommandService } from '@univerjs/core';
-import { RemoveNumfmtMutation, SetNumfmtMutation, SetRangeValuesMutation } from '@univerjs/sheets';
+import { RemoveNumfmtMutation, SetNumfmtMutation, SetRangeValuesCommand, SetRangeValuesMutation } from '@univerjs/sheets';
import { beforeEach, describe, expect, it } from 'vitest';
import { SetNumfmtCommand, SheetsNumfmtCellContentController } from '../../index';
import { createFacadeTestBed } from './create-test-bed';
@@ -31,10 +31,12 @@ describe('Test FRange', () => {
const commandService = testBed.injector.get(ICommandService);
[
SetRangeValuesMutation,
+ SetRangeValuesCommand,
SetNumfmtMutation,
RemoveNumfmtMutation,
SetNumfmtCommand,
].forEach((command) => commandService.registerCommand(command));
+ testBed.injector.get(SheetsNumfmtCellContentController);
univerAPI = testBed.univerAPI;
});
@@ -61,4 +63,16 @@ describe('Test FRange', () => {
const workbook = univerAPI.getActiveWorkbook()!;
expect(workbook.setNumfmtLocal('fr')).toBe(workbook);
});
+
+ it('hides a formatted value without changing its raw value', () => {
+ const activeSheet = univerAPI.getActiveWorkbook()!.getActiveSheet();
+ const cell = activeSheet.getRange('A1');
+
+ cell.setValue(233628);
+ cell.setNumberFormat(';;;');
+
+ expect(cell.getDisplayValue()).toBe('');
+ expect(cell.getRawValue()).toBe(233628);
+ expect(cell.getNumberFormat()).toBe(';;;');
+ });
});