diff --git a/packages/core/database/src/fields/number-field.ts b/packages/core/database/src/fields/number-field.ts index fed7655ef..aa9b90401 100644 --- a/packages/core/database/src/fields/number-field.ts +++ b/packages/core/database/src/fields/number-field.ts @@ -64,10 +64,12 @@ export interface RealFieldOptions extends BaseColumnFieldOptions { export class DecimalField extends NumberField { get dataType() { - return DataTypes.DECIMAL; + return DataTypes.DECIMAL(this.options.precision, this.options.scale); } } export interface DecimalFieldOptions extends BaseColumnFieldOptions { type: 'decimal'; + precision: number; + scale: number; } diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts index a8ce50061..707c31ebe 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts @@ -38,6 +38,7 @@ describe('export to xlsx with preset', () => { { type: 'string', name: 'title' }, { type: 'integer', name: 'integer' }, { type: 'float', name: 'float' }, + { type: 'decimal', name: 'decimal', scale: 3, precision: 12 }, ], }); @@ -49,11 +50,13 @@ describe('export to xlsx with preset', () => { title: 'p1', integer: 123, float: 123.456, + decimal: 234.567, }, { title: 'p2', integer: 456, float: 456.789, + decimal: 345.678, }, ], }); @@ -72,6 +75,10 @@ describe('export to xlsx with preset', () => { dataIndex: ['float'], defaultTitle: 'float', }, + { + dataIndex: ['decimal'], + defaultTitle: 'decimal', + }, ], }); @@ -97,6 +104,10 @@ describe('export to xlsx with preset', () => { const cellC2 = firstSheet['C2']; expect(cellC2.t).toBe('n'); expect(cellC2.v).toBe(123.456); + + const cellD2 = firstSheet['D2']; + expect(cellD2.t).toBe('n'); + expect(cellD2.v).toBe(234.567); } finally { fs.unlinkSync(xlsxFilePath); } diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts index c46043efd..5ff47dda0 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/xlsx-exporter.ts @@ -96,7 +96,7 @@ class XlsxExporter { for (let r = 1; r <= cellRange.e.r; r++) { const cell = worksheet[XLSX.utils.encode_cell({ c: colIndex, r })]; // if cell and cell.v is a number, set cell.t to 'n' - if (cell && typeof cell.v === 'number') { + if (cell && isNumeric(cell.v)) { cell.t = 'n'; } } @@ -214,4 +214,8 @@ class XlsxExporter { } } +function isNumeric(n) { + return !isNaN(parseFloat(n)) && isFinite(n); +} + export default XlsxExporter;