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(numfmt): filter some erroneous numfmt pattern conversions (#5709)
wpxp1234566c33280f4d7e
diff --git a/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts b/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.tsindex c192ff31f4..b8fa688550 100644--- a/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts+++ b/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts@@ -431,6 +431,80 @@ describe('test editor', () => {expect(result?.v).toBe(123);expect(result?.t).toBe(CellValueType.NUMBER);});++ it('edit content 5A', () => {+ const sheetInterceptorService = testBed.get(SheetInterceptorService);+ const cellData = { v: '5A' };+ const location = {+ workbook,+ worksheet,+ unitId,+ subUnitId,+ row: 0,+ col: 0,+ origin: cellData,+ };++ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);++ expect(result?.v).toBe('5A');+ });++ it('edit content 5 A', () => {+ const sheetInterceptorService = testBed.get(SheetInterceptorService);+ const cellData = { v: '5 A' };+ const location = {+ workbook,+ worksheet,+ unitId,+ subUnitId,+ row: 0,+ col: 0,+ origin: cellData,+ };++ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);++ expect(result?.v).toBe(0.20833333333333334);+ expect(result?.t).toBe(CellValueType.NUMBER);+ });++ it('edit content 1 23', () => {+ const sheetInterceptorService = testBed.get(SheetInterceptorService);+ const cellData = { v: '1 23' };+ const location = {+ workbook,+ worksheet,+ unitId,+ subUnitId,+ row: 0,+ col: 0,+ origin: cellData,+ };++ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);++ expect(result?.v).toBe('1 23');+ });++ it('edit content 2/3', () => {+ const sheetInterceptorService = testBed.get(SheetInterceptorService);+ const cellData = { v: '2/3' };+ const location = {+ workbook,+ worksheet,+ unitId,+ subUnitId,+ row: 0,+ col: 0,+ origin: cellData,+ };++ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);++ expect(result?.v).toBe(45691);+ expect(result?.t).toBe(CellValueType.NUMBER);+ });});describe('test get cell text/plain', () => {diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.tsindex 6ad0580501..969c31c662 100644--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts@@ -28,7 +28,6 @@ import {Injector,isTextFormat,IUniverInstanceService,- numfmt,Optional,toDisposable,UniverInstanceType,@@ -46,7 +45,7 @@ import {SheetInterceptorService,transformCellsToRange,} from '@univerjs/sheets';-import { getPatternType } from '@univerjs/sheets-numfmt';+import { getNumfmtParseValueFilter, getPatternType } from '@univerjs/sheets-numfmt';import { IEditorBridgeService } from '@univerjs/sheets-ui';const createCollectEffectMutation = () => {@@ -71,8 +70,6 @@ const createCollectEffectMutation = () => {};};-const numfmtIgnorePatterns = new Set(['m/d', 'm d']);-export class NumfmtEditorController extends Disposable {// collect effect mutations when edit end and push this to commands stack in next commands progressprivate _collectEffectMutation = createCollectEffectMutation();@@ -143,7 +140,7 @@ export class NumfmtEditorController extends Disposable {this.disposeWithMe(toDisposable(this._sheetInterceptorService.writeCellInterceptor.intercept(AFTER_CELL_EDIT, {- // eslint-disable-next-line complexity, max-lines-per-function+ // eslint-disable-next-line complexityhandler: (value, context, next) => {// clear the effectthis._collectEffectMutation.clean();@@ -175,12 +172,7 @@ export class NumfmtEditorController extends Disposable {const body = value.p?.body;const content = value?.p?.body?.dataStream ? value.p.body.dataStream.replace(/\r\n$/, '') : String(value.v);- let numfmtInfo = numfmt.parseDate(content);- if (!numfmtInfo) {- numfmtInfo = numfmt.parseTime(content) || numfmt.parseNumber(content);- } else if (numfmtIgnorePatterns.has(numfmtInfo.z!)) {- numfmtInfo = numfmt.parseTime(content) || numfmt.parseNumber(content);- }+ const numfmtInfo = getNumfmtParseValueFilter(content);if (body) {if (!canConvertRichTextToNumfmt(body)) {diff --git a/packages/sheets-numfmt/src/index.ts b/packages/sheets-numfmt/src/index.tsindex e37a2ded8e..683213e84e 100644--- a/packages/sheets-numfmt/src/index.ts+++ b/packages/sheets-numfmt/src/index.ts@@ -28,4 +28,4 @@ export { UniverSheetsNumfmtPlugin } from './plugin';export { getCurrencyType } from './utils/currency';export { getDecimalFromPattern, getDecimalString, isPatternHasDecimal, setPatternDecimal } from './utils/decimal';export { getCurrencyFormatOptions, getCurrencyOptions, getDateFormatOptions, getNumberFormatOptions } from './utils/options';-export { getPatternPreview, getPatternPreviewIgnoreGeneral, getPatternType } from './utils/pattern';+export { getNumfmtParseValueFilter, getPatternPreview, getPatternPreviewIgnoreGeneral, getPatternType } from './utils/pattern';diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.tsindex d5e60857ba..bb626dab59 100644--- a/packages/sheets-numfmt/src/utils/pattern.ts+++ b/packages/sheets-numfmt/src/utils/pattern.ts@@ -51,3 +51,35 @@ export const getPatternPreviewIgnoreGeneral = (pattern: string, value: number, l}return getPatternPreview(pattern, value, locale);};++const ignoreCommonPatterns = new Set(['m d']);+const ignoreAMPMPatterns = new Set(['h:mm AM/PM', 'hh:mm AM/PM']);++/**+ * Get the numfmt parse value, and filter out the parse error.+ */+export const getNumfmtParseValueFilter = (value: string): numfmt.ParseData | null => {+ const parseData = numfmt.parseDate(value) ?? numfmt.parseTime(value) ?? numfmt.parseNumber(value);++ if (!parseData) return null;++ const { z } = parseData;++ if (z) {+ /**+ * '1 23' => 'm d' ----- error+ * '2/3' => 'm/d' ----- This is supported by Excel+ */+ if (ignoreCommonPatterns.has(z)) return null;++ /**+ * If the pattern is 'h:mm AM/PM' or 'hh:mm AM/PM', we need to check if the value ends with ' A', ' P', ' AM', or ' PM'.+ * '5A' => 'h:mm AM/PM' ----- error+ * '5 A' => 'h:mm AM/PM' ----- This is supported by Excel+ * '5:00 AM' => 'h:mm AM/PM' ----- correct+ */+ if (ignoreAMPMPatterns.has(z) && !/\s(A|AM|P|PM)$/i.test(value)) return null;+ }++ return parseData;+};