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 of digital precision display (#5755)

wpxp1234563af613122d5e
diff --git a/packages/sheets-numfmt-ui/package.json b/packages/sheets-numfmt-ui/package.json
index 84da9b217b..0145b57430 100644
--- a/packages/sheets-numfmt-ui/package.json
+++ b/packages/sheets-numfmt-ui/package.json
@@ -70,6 +70,7 @@
"dependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
+ "@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/icons": "^0.4.6",
"@univerjs/sheets": "workspace:*",
@@ -79,7 +80,6 @@
},
"devDependencies": {
"@univerjs-infra/shared": "workspace:*",
- "@univerjs/engine-formula": "workspace:*",
"postcss": "^8.5.6",
"react": "18.3.1",
"rxjs": "^7.8.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 969c31c662..5be9531278 100644
--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
@@ -26,6 +26,7 @@ import {
Disposable,
Inject,
Injector,
+ isRealNum,
isTextFormat,
IUniverInstanceService,
Optional,
@@ -33,6 +34,7 @@ import {
UniverInstanceType,
willLoseNumericPrecision,
} from '@univerjs/core';
+import { stripErrorMargin } from '@univerjs/engine-formula';
import {
AFTER_CELL_EDIT,
BEFORE_CELL_EDIT,
@@ -112,6 +114,9 @@ export class NumfmtEditorController extends Disposable {
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)) {
+ cell.v = stripErrorMargin(Number(cell.v));
+ }
return next && next(cell);
}
case 'percent':
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 f35ef96ec0..2b5b9d062e 100644
--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
@@ -24,6 +24,7 @@ import {
IConfigService,
Inject,
InterceptorEffectEnum,
+ isDefaultFormat,
isTextFormat,
IUniverInstanceService,
LocaleService,
@@ -110,15 +111,15 @@ export class SheetsNumfmtCellContentController extends Disposable {
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
- // eslint-disable-next-line complexity
+ // eslint-disable-next-line max-lines-per-function, complexity
handler: (cell, location, next) => {
+ if (!cell || cell.v === undefined || cell.v === null) {
+ return next(cell);
+ }
+
const unitId = location.unitId;
const sheetId = location.subUnitId;
let numfmtValue;
- const originCellValue = cell;
- if (!originCellValue || originCellValue.v === undefined || originCellValue.v === null) {
- return next(cell);
- }
if (cell?.s) {
const style = location.workbook.getStyles().get(cell.s);
@@ -133,25 +134,26 @@ export class SheetsNumfmtCellContentController extends Disposable {
// If the cell is not formatted, or the format is 'General', do not process it
// e.g. { v: '001', t: 1, s: { n: { pattern: 'General' } } } should display as '001'
- if (!numfmtValue || numfmtValue.pattern === 'General') {
+ if (isDefaultFormat(numfmtValue?.pattern)) {
return next(cell);
}
// If the cell not specified number type, then check the cell value type
- if (originCellValue.t !== CellValueType.NUMBER) {
- const type = checkCellValueType(originCellValue.v);
+ if (cell.t !== CellValueType.NUMBER) {
+ const type = checkCellValueType(cell.v);
// just handle number or number string
if (type !== CellValueType.NUMBER) {
return next(cell);
}
}
+ const originCellValue = cell;
if (!cell || cell === location.rawData) {
cell = { ...location.rawData };
}
// Add error marker to text format number
- if (isTextFormat(numfmtValue.pattern)) {
+ if (isTextFormat(numfmtValue?.pattern)) {
// If the user has disabled the text format mark, do not show it
if (this._configService.getConfig<IUniverSheetsNumfmtConfig>(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY)?.disableTextFormatMark) {
cell.t = CellValueType.STRING;
@@ -165,11 +167,11 @@ export class SheetsNumfmtCellContentController extends Disposable {
let numfmtRes: string = '';
const cache = renderCache.getValue(location.row, location.col);
- if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue.pattern}`) {
+ if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue?.pattern}`) {
return next({ ...cell, ...cache.result });
}
- const info = getPatternPreviewIgnoreGeneral(numfmtValue.pattern, Number(originCellValue.v), this.locale);
+ const info = getPatternPreviewIgnoreGeneral(numfmtValue?.pattern as string, Number(originCellValue.v), this.locale);
numfmtRes = info.result;
if (!numfmtRes) {
return next(cell);
@@ -186,7 +188,7 @@ export class SheetsNumfmtCellContentController extends Disposable {
renderCache.setValue(location.row, location.col, {
result: res,
- parameters: `${originCellValue.v}_${numfmtValue.pattern}`,
+ parameters: `${originCellValue.v}_${numfmtValue?.pattern}`,
});
Object.assign(cell, res);
return next(cell);
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index bb626dab59..58edbcdd3a 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -16,7 +16,7 @@
import type { INumfmtLocaleTag } from '@univerjs/core';
import type { FormatType } from '@univerjs/sheets';
-import { numfmt } from '@univerjs/core';
+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';
@@ -44,7 +44,7 @@ export const getPatternPreview = (pattern: string, value: number, locale: INumfm
};
export const getPatternPreviewIgnoreGeneral = (pattern: string, value: number, locale?: INumfmtLocaleTag): IPatternPreview => {
- if (pattern === 'General') {
+ if (pattern === DEFAULT_NUMBER_FORMAT) {
return {
result: String(stripErrorMargin(value)), // In Excel, the default General format also needs to handle numeric precision.
};
diff --git a/packages/sheets/src/controllers/number-cell.controller.ts b/packages/sheets/src/controllers/number-cell.controller.ts
index f05cdb92a3..74d25d0b90 100644
--- a/packages/sheets/src/controllers/number-cell.controller.ts
+++ b/packages/sheets/src/controllers/number-cell.controller.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { CellValueType, Disposable, Inject, InterceptorEffectEnum, isRealNum } from '@univerjs/core';
+import { CellValueType, Disposable, Inject, InterceptorEffectEnum, isDefaultFormat, isRealNum } from '@univerjs/core';
import { stripErrorMargin } from '@univerjs/engine-formula';
import { INTERCEPTOR_POINT } from '../services/sheet-interceptor/interceptor-const';
import { SheetInterceptorService } from '../services/sheet-interceptor/sheet-interceptor.service';
@@ -38,9 +38,13 @@ export class NumberCellDisplayController extends Disposable {
priority: 11,
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
handler: (cell, location, next) => {
+ if (!cell) {
+ return next(cell);
+ }
+
// Skip if the cell contains a numfmt pattern
const style = location.workbook.getStyles().getStyleByCell(cell);
- if (style?.n?.pattern) {
+ if (!isDefaultFormat(style?.n?.pattern)) {
return next(cell);
}
diff --git a/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts
index 512fb842f4..1504602775 100644
--- a/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts
+++ b/packages/sheets/src/controllers/permission/sheet-permission-view-model.controller.ts
@@ -40,7 +40,7 @@ export class SheetPermissionViewModelController extends Disposable {
// permissions are placed at a high level to prioritize whether to filter subsequent renderings.
priority: 999,
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
- handler: (cell = {}, context, next) => {
+ handler: (cell, context, next) => {
const { unitId, subUnitId, row, col } = context;
const selectionProtection = this._rangeProtectionCache.getCellInfo(unitId, subUnitId, row, col);
@@ -69,7 +69,7 @@ export class SheetPermissionViewModelController extends Disposable {
// permissions are placed at a high level to prioritize whether to filter subsequent renderings.
priority: 999,
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
- handler: (cell = {}, context, next) => {
+ handler: (cell, context, next) => {
const { unitId, subUnitId } = context;
const worksheetRule = this._worksheetProtectionRuleModel.getRule(unitId, subUnitId);
if (worksheetRule?.permissionId) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 96ad7258c4..a352eb8afd 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2750,6 +2750,9 @@ importers:
'@univerjs/design':
specifier: workspace:*
version: link:../design
+ '@univerjs/engine-formula':
+ specifier: workspace:*
+ version: link:../engine-formula
'@univerjs/engine-render':
specifier: workspace:*
version: link:../engine-render
@@ -2772,9 +2775,6 @@ importers:
'@univerjs-infra/shared':
specifier: workspace:*
version: link:../../common/shared
- '@univerjs/engine-formula':
- specifier: workspace:*
- version: link:../engine-formula
postcss:
specifier: ^8.5.6
version: 8.5.6