fix: export with decimal field (#5316)

* fix: export with decimal field

* fix: test
This commit is contained in:
ChengLei Shao 2024-09-26 07:12:03 +08:00 committed by GitHub
parent d74a333f10
commit 786e1e4f25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 2 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;