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-ui): numfmt display NaN if undefind is given (#5096)

ZhangWeid3b1734d31e8
diff --git a/mockdata/src/sheets/demo/default-workbook-data-demo.ts b/mockdata/src/sheets/demo/default-workbook-data-demo.ts
index 08978083d2..9649633b6b 100644
--- a/mockdata/src/sheets/demo/default-workbook-data-demo.ts
+++ b/mockdata/src/sheets/demo/default-workbook-data-demo.ts
@@ -15,7 +15,7 @@
*/
import type { IDataValidationRule, IDocumentData, IWorkbookData } from '@univerjs/core';
-import { BooleanNumber, DataValidationErrorStyle, DataValidationOperator, DataValidationType, LocaleType } from '@univerjs/core';
+import { BooleanNumber, CellValueType, DataValidationErrorStyle, DataValidationOperator, DataValidationType, LocaleType } from '@univerjs/core';
import { DATA_VALIDATION_PLUGIN_NAME } from '@univerjs/sheets-data-validation';
import { PAGE5_RICHTEXT_1 } from '../../slides/rich-text/page5-richtext1';
@@ -13991,6 +13991,11 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
s: '1',
v: 'A Schedule of Items',
},
+ 1: {
+ v: undefined,
+ t: CellValueType.NUMBER,
+ s: { n: { pattern: '#,##0.00' } },
+ },
10: {
v: 10,
},
@@ -14593,8 +14598,9 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
cellData: {
5: {
1: {
- v: '1',
- t: 2,
+ v: undefined,
+ t: CellValueType.NUMBER,
+ s: { n: { pattern: '#,##0.00' } },
},
2: {
v: '12',
diff --git a/packages/sheets-data-validation/src/validators/date-validator.ts b/packages/sheets-data-validation/src/validators/date-validator.ts
index a43403f92d..2ae8df0b29 100644
--- a/packages/sheets-data-validation/src/validators/date-validator.ts
+++ b/packages/sheets-data-validation/src/validators/date-validator.ts
@@ -81,8 +81,9 @@ export class DateValidator extends BaseDataValidator {
override async isValidType(info: IValidatorCellInfo): Promise<boolean> {
const { interceptValue, value } = info;
+
if (typeof value === 'number' && typeof interceptValue === 'string') {
- return true;
+ return Boolean(numfmt.parseDate(interceptValue));
}
if (typeof interceptValue === 'string') {
diff --git a/packages/sheets-data-validation/src/validators/decimal-validator.ts b/packages/sheets-data-validation/src/validators/decimal-validator.ts
index c968e32dc4..81d460cdd3 100644
--- a/packages/sheets-data-validation/src/validators/decimal-validator.ts
+++ b/packages/sheets-data-validation/src/validators/decimal-validator.ts
@@ -28,15 +28,6 @@ import { FORMULA1, FORMULA2 } from './const';
import { getTransformedFormula } from './util';
export function getCellValueNumber(cellValue: CellValue) {
- let str = cellValue;
- if (typeof cellValue === 'string') {
- if (cellValue.startsWith('¥') || cellValue.startsWith('$')) {
- str = cellValue.slice(1);
- }
-
- return +str;
- }
-
return +cellValue;
}
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 886a7af963..49b0001dee 100644
--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
@@ -106,6 +106,8 @@ export class SheetsNumfmtCellContentController extends Disposable {
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
+
+ // eslint-disable-next-line complexity
handler: (cell, location, next) => {
const unitId = location.unitId;
const sheetId = location.subUnitId;
@@ -161,7 +163,9 @@ export class SheetsNumfmtCellContentController extends Disposable {
if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue.pattern}`) {
return next({ ...cell, ...cache.result });
}
-
+ if (originCellValue.v === undefined || originCellValue.v === null) {
+ return next(cell);
+ }
const info = getPatternPreviewIgnoreGeneral(numfmtValue.pattern, Number(originCellValue.v), this.local);
numfmtRes = info.result;
if (!numfmtRes) {
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index 76b9cf797e..ec14950e3d 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -28,6 +28,7 @@ interface IPatternPreview {
export const getPatternPreview = (pattern: string, value: number, locale: INumfmtLocalTag = 'en'): IPatternPreview => {
const info = numfmt.getInfo(pattern);
const negInfo = info._partitions[1];
+
const result = numfmt.format(pattern, value, { locale, throws: false });
if (value < 0) {
return {