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: fix the issue when editing percent number format cells (#6605)

wpxp123456c1b9d9fc803e
diff --git a/packages/core/src/shared/generate.ts b/packages/core/src/shared/generate.ts
index 289a7a95cd..7c4611f395 100644
--- a/packages/core/src/shared/generate.ts
+++ b/packages/core/src/shared/generate.ts
@@ -14,12 +14,15 @@
* limitations under the License.
*/
+import type { CellValue } from '../sheets/typedef';
+import type { Nullable } from './types';
+
/**
* Determine whether it is a pure number, "12" and "12e+3" are both true
* @param val The number or string to be judged
* @returns Result
*/
-export function isRealNum(val: string | number | boolean): boolean {
+export function isRealNum(val: Nullable<CellValue>): boolean {
if (val === null || val === undefined) {
return false;
}
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 55f032844c..5fca4dd24c 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
@@ -105,6 +105,7 @@ describe('test editor', () => {
expect(result!.v).toEqual(0);
expect(result!.t).toEqual(2);
});
+
it('before edit with data', () => {
const params: ISetNumfmtMutationParams = {
unitId,
@@ -137,6 +138,44 @@ describe('test editor', () => {
// The data format needs to be entered in the editor with data string, not with real values
expect(result!.v).toEqual(cellData!.v);
});
+
+ it('before edit with percent', () => {
+ const params: ISetNumfmtMutationParams = {
+ unitId,
+ subUnitId,
+ values: {
+ 1: {
+ ranges: [{ startRow: 10, endRow: 10, startColumn: 0, endColumn: 0 }],
+ },
+ },
+ refMap: {
+ 1: {
+ pattern: '0.00%',
+ },
+ },
+ };
+ commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = worksheet.getCell(10, 0);
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 10,
+ col: 0,
+ origin: cellData,
+ };
+
+ expect(cellData!.v).toEqual('100.12%');
+ expect(cellData!.t).toEqual(2);
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
+ // The currency format needs to be entered in the editor with real values, not with currency symbols
+ expect(result!.v).toEqual('100.1234567%');
+ expect(result!.t).toEqual(2);
+ });
+
it('after edit with data', () => {
const params: ISetNumfmtMutationParams = {
unitId,
@@ -474,7 +513,7 @@ describe('test editor', () => {
const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
- expect(result?.v).toBe(0.20833333333333334);
+ expect(result?.v).toBe(0.2083333333333333);
expect(result?.t).toBe(CellValueType.NUMBER);
});
diff --git a/packages/sheets-numfmt-ui/src/controllers/__tests__/test.util.ts b/packages/sheets-numfmt-ui/src/controllers/__tests__/test.util.ts
index a9a5992650..b1c4f3995f 100644
--- a/packages/sheets-numfmt-ui/src/controllers/__tests__/test.util.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/__tests__/test.util.ts
@@ -37,6 +37,12 @@ const TEST_WORKBOOK_DATA_DEMO: () => IWorkbookData = () => ({
1: { 0: { v: 0, t: 2 }, 1: { v: 1, t: 2 }, 2: { v: 2, t: 2 }, 3: { v: 3, t: 2 }, 4: { v: 4, t: 2 } },
2: { 0: { v: 0, t: 2 }, 1: { v: 1, t: 2 }, 2: { v: 2, t: 2 }, 3: { v: 3, t: 2 }, 4: { v: 4, t: 2 } },
3: { 0: { v: 0, t: 2 }, 1: { v: 1, t: 2 }, 2: { v: 2, t: 2 }, 3: { v: 3, t: 2 }, 4: { v: 4, t: 2 } },
+ 10: {
+ 0: {
+ v: 1.001234567,
+ t: 2,
+ },
+ },
},
},
},
diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
index cb6343701c..21cebb2120 100644
--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IDocumentBody, IRange, Nullable, Workbook } from '@univerjs/core';
+import type { ICellData, IDocumentBody, IRange, Nullable, Workbook } from '@univerjs/core';
import type {
INumfmtItemWithCache,
IRemoveNumfmtMutationParams,
@@ -99,6 +99,10 @@ export class NumfmtEditorController extends Disposable {
toDisposable(
this._sheetInterceptorService.writeCellInterceptor.intercept(BEFORE_CELL_EDIT, {
handler: (value, context, next) => {
+ /**
+ * This value is get by `worksheet.getCell()`, it has been processed by cell content interceptor, and used to display in cell render, so it should be the final value after all the processing of number format.
+ * But the editor has different requirement for different number format type, so we need to get the raw cell value and number format value to determine the final value for editor.
+ */
const row = context.row;
const col = context.col;
const numfmtCell = this._numfmtService.getValue(
@@ -110,17 +114,34 @@ export class NumfmtEditorController extends Disposable {
if (numfmtCell) {
const type = getPatternType(numfmtCell.pattern);
switch (type) {
+ /**
+ * For scientific, currency, grouped and number format, the editor should display the raw number value without format, unlike the cell render which display the formatted value.
+ */
case 'scientific':
case 'currency':
case 'grouped':
case 'number': {
- const cell = context.worksheet.getCellRaw(row, col);
- if (cell?.t === CellValueType.NUMBER && cell?.v !== undefined && cell.v !== null && isRealNum(cell.v)) {
+ const cell: Nullable<ICellData> = { ...context.worksheet.getCellRaw(row, col) };
+ if (cell?.t === CellValueType.NUMBER && isRealNum(cell.v)) {
cell.v = stripErrorMargin(Number(cell.v));
}
return next && next(cell);
}
- case 'percent':
+ /**
+ * For percent format, the editor should display the full percent value, unlike the cell render which display the limited decimal places.
+ * e.g. { v: 1.001234567, t: 2, s: { n: { pattern: '0.00%' } } } should display as '100.12%' in cell render, but when edit this cell, the editor should display '100.1234567%' rather than '100.12%'.
+ * If the editor also display '100.12%', will lose precision when before edit.
+ */
+ case 'percent': {
+ const cell: Nullable<ICellData> = { ...context.worksheet.getCellRaw(row, col) };
+ if (cell?.t === CellValueType.NUMBER && isRealNum(cell.v)) {
+ cell.v = `${stripErrorMargin(Number(cell.v) * 100)}%`;
+ }
+ return next && next(cell);
+ }
+ /**
+ * For date, time and datetime format, the editor should display the formatted value like the cell render.
+ */
case 'date':
case 'time':
case 'datetime':
@@ -146,7 +167,7 @@ export class NumfmtEditorController extends Disposable {
this.disposeWithMe(
toDisposable(
this._sheetInterceptorService.writeCellInterceptor.intercept(AFTER_CELL_EDIT, {
- // eslint-disable-next-line complexity
+ // eslint-disable-next-line max-lines-per-function,complexity
handler: (value, context, next) => {
if (!value?.v && !value?.p) {
return next(value);
@@ -206,7 +227,11 @@ export class NumfmtEditorController extends Disposable {
});
}
- if (numfmtInfo.z) {
+ /**
+ * Only when the content has number format pattern but the current cell has no pattern, or the pattern type is different, need to update the number format.
+ * Different currency symbols also should not be updated in Excel.
+ */
+ if (numfmtInfo.z && (!currentNumfmtValue?.pattern || getPatternType(numfmtInfo.z) !== getPatternType(currentNumfmtValue.pattern))) {
this._collectEffectMutation.add(
context.unitId,
context.subUnitId,
@@ -218,7 +243,7 @@ export class NumfmtEditorController extends Disposable {
);
}
- const v = Number(numfmtInfo.v);
+ const v = stripErrorMargin(Number(numfmtInfo.v), 16);
return next({ ...value, p: undefined, v, t: CellValueType.NUMBER });
}
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 24423543a8..bd7abe5da6 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -642,7 +642,17 @@ export class EditingRenderController extends Disposable {
this._removeComposedCellStyleInCellData(cellData, worksheet.getComposedCellStyleWithoutSelf(row, column));
const finalCell = this._sheetInterceptorService.onWriteCell(workbook, worksheet, row, column, cellData) as ICellData;
- if (Tools.diffValue(cleanCellDataObject(finalCell), cleanCellDataObject(worksheet.getCellRaw(row, column)))) {
+
+ // If the cell data after interceptor is the same as the raw cell data, there is no need to execute setRangeValue command, just return directly.
+ const finalCellCleaned = cleanCellDataObject(finalCell);
+ if (finalCellCleaned?.s) {
+ finalCellCleaned.s = workbook.getStyles().get(finalCellCleaned.s);
+ }
+ const rawCellCleaned = cleanCellDataObject(worksheet.getCellRaw(row, column));
+ if (rawCellCleaned?.s) {
+ rawCellCleaned.s = workbook.getStyles().get(rawCellCleaned.s);
+ }
+ if (Tools.diffValue(finalCellCleaned, rawCellCleaned)) {
return true;
}