mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-02 12:18:15 +08:00
fix: export with decimal field (#5316)
* fix: export with decimal field * fix: test
This commit is contained in:
parent
d74a333f10
commit
786e1e4f25
@ -64,10 +64,12 @@ export interface RealFieldOptions extends BaseColumnFieldOptions {
|
|||||||
|
|
||||||
export class DecimalField extends NumberField {
|
export class DecimalField extends NumberField {
|
||||||
get dataType() {
|
get dataType() {
|
||||||
return DataTypes.DECIMAL;
|
return DataTypes.DECIMAL(this.options.precision, this.options.scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DecimalFieldOptions extends BaseColumnFieldOptions {
|
export interface DecimalFieldOptions extends BaseColumnFieldOptions {
|
||||||
type: 'decimal';
|
type: 'decimal';
|
||||||
|
precision: number;
|
||||||
|
scale: number;
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ describe('export to xlsx with preset', () => {
|
|||||||
{ type: 'string', name: 'title' },
|
{ type: 'string', name: 'title' },
|
||||||
{ type: 'integer', name: 'integer' },
|
{ type: 'integer', name: 'integer' },
|
||||||
{ type: 'float', name: 'float' },
|
{ type: 'float', name: 'float' },
|
||||||
|
{ type: 'decimal', name: 'decimal', scale: 3, precision: 12 },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -49,11 +50,13 @@ describe('export to xlsx with preset', () => {
|
|||||||
title: 'p1',
|
title: 'p1',
|
||||||
integer: 123,
|
integer: 123,
|
||||||
float: 123.456,
|
float: 123.456,
|
||||||
|
decimal: 234.567,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'p2',
|
title: 'p2',
|
||||||
integer: 456,
|
integer: 456,
|
||||||
float: 456.789,
|
float: 456.789,
|
||||||
|
decimal: 345.678,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@ -72,6 +75,10 @@ describe('export to xlsx with preset', () => {
|
|||||||
dataIndex: ['float'],
|
dataIndex: ['float'],
|
||||||
defaultTitle: 'float',
|
defaultTitle: 'float',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
dataIndex: ['decimal'],
|
||||||
|
defaultTitle: 'decimal',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -97,6 +104,10 @@ describe('export to xlsx with preset', () => {
|
|||||||
const cellC2 = firstSheet['C2'];
|
const cellC2 = firstSheet['C2'];
|
||||||
expect(cellC2.t).toBe('n');
|
expect(cellC2.t).toBe('n');
|
||||||
expect(cellC2.v).toBe(123.456);
|
expect(cellC2.v).toBe(123.456);
|
||||||
|
|
||||||
|
const cellD2 = firstSheet['D2'];
|
||||||
|
expect(cellD2.t).toBe('n');
|
||||||
|
expect(cellD2.v).toBe(234.567);
|
||||||
} finally {
|
} finally {
|
||||||
fs.unlinkSync(xlsxFilePath);
|
fs.unlinkSync(xlsxFilePath);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ class XlsxExporter {
|
|||||||
for (let r = 1; r <= cellRange.e.r; r++) {
|
for (let r = 1; r <= cellRange.e.r; r++) {
|
||||||
const cell = worksheet[XLSX.utils.encode_cell({ c: colIndex, 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 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';
|
cell.t = 'n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -214,4 +214,8 @@ class XlsxExporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isNumeric(n) {
|
||||||
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||||
|
}
|
||||||
|
|
||||||
export default XlsxExporter;
|
export default XlsxExporter;
|
||||||
|
Loading…
Reference in New Issue
Block a user