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 raw values for conditional formats (#7612)

wpxp1234569e8a8be55180
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.ts
index 7cc8df68e8..5d71eb401f 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
@@ -106,6 +106,41 @@ describe('test editor', () => {
expect(result!.t).toEqual(2);
});
+ it('before edit with a conditional text format uses the raw value', () => {
+ const params: ISetNumfmtMutationParams = {
+ unitId,
+ subUnitId,
+ values: {
+ 1: {
+ ranges: [{ startRow: 0, endRow: 0, startColumn: 1, endColumn: 1 }],
+ },
+ },
+ refMap: {
+ 1: {
+ pattern: '[>0]"A";[<0]"B";"B"',
+ },
+ },
+ };
+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = worksheet.getCell(0, 1);
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 1,
+ origin: cellData,
+ };
+
+ expect(cellData!.v).toBe('A');
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
+ expect(result!.v).toBe(1);
+ expect(result!.t).toBe(CellValueType.NUMBER);
+ });
+
it('before edit with data', () => {
const params: ISetNumfmtMutationParams = {
unitId,
diff --git a/packages/sheets-numfmt/src/utils/__tests__/pattern.spec.ts b/packages/sheets-numfmt/src/utils/__tests__/pattern.spec.ts
new file mode 100644
index 0000000000..e8434d3cd5
--- /dev/null
+++ b/packages/sheets-numfmt/src/utils/__tests__/pattern.spec.ts
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { DEFAULT_NUMBER_FORMAT } from '@univerjs/core';
+import { describe, expect, it } from 'vitest';
+import { getPatternPreview, getPatternType } from '../pattern';
+
+describe('getPatternType', () => {
+ it('classifies conditional general formats as number formats', () => {
+ expect(getPatternType(DEFAULT_NUMBER_FORMAT)).toBe('general');
+ expect(getPatternType('[>0]"A";[<0]"B";"B"')).toBe('number');
+ });
+
+ it('classifies two-section conditional text formats as number formats', () => {
+ const pattern = '[>0]"未完成";[<=0]"完成"';
+ expect(getPatternType(pattern)).toBe('number');
+ expect(getPatternPreview(pattern, 1)).toEqual({ result: '未完成' });
+ expect(getPatternPreview(pattern, 0)).toEqual({ result: '完成' });
+ expect(getPatternPreview(pattern, -1)).toEqual({ result: '-完成' });
+ });
+
+ it('classifies three-section conditional text formats as number formats', () => {
+ const pattern = '[>0]"未完成";[<0]"完成";"完成"';
+ expect(getPatternType(pattern)).toBe('number');
+ expect(getPatternPreview(pattern, 1)).toEqual({ result: '未完成' });
+ expect(getPatternPreview(pattern, 0)).toEqual({ result: '完成' });
+ expect(getPatternPreview(pattern, -1)).toEqual({ result: '完成' });
+ });
+});
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index bdca199e20..f8d3a7c4b0 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -19,7 +19,14 @@ import type { FormatType } from '@univerjs/sheets';
import { DEFAULT_NUMBER_FORMAT, numfmt } from '@univerjs/core';
import { stripErrorMargin } from '@univerjs/engine-formula';
-export const getPatternType = (pattern: string): FormatType => numfmt.getFormatInfo(pattern).type || 'unknown';
+export const getPatternType = (pattern: string): FormatType => {
+ const type = numfmt.getFormatInfo(pattern).type || 'unknown';
+ if (type === 'general' && numfmt.tokenize(pattern).some((token) => token.type === numfmt.tokenTypes.CONDITION)) {
+ return 'number';
+ }
+
+ return type;
+};
interface IPatternPreview {
result: string;
color?: string;