How to apply the header name in csv. cause when I try to export the csv. the header applying is the key instead the header name.
current output
id, firstName, lastName, sampleLocation
`export default function useReports() {
const COLUMNS = (reportColumns);
const process = async (data = {}) => {
const excel = useExcelExport();
const workbook = excel.workbook;
const sheet = excel.addWorksheet(workbook, 'Sheet 1', false);
const today = format(new Date(), 'yyyyMMdd');
// Page setup
sheet.pageSetup.margins = {
left: 0.25,
right: 0.15,
top: 0.5,
bottom: 0.25,
header: 0.25,
footer: 0.25
};
const cols = COLUMNS.map(a => ({
key: a.field,
header: a.headerName,
width: 15
}));
sheet.columns = cols;
const csvData = [];
data['items'].forEach(item => {
const obj = {};
cols.forEach(col => {
obj[col.key] = item[col.key];
});
dateFields.forEach((field) => {
try {
if (obj[field]) {
obj[field] = obj[field] ? format(new Date(obj[field]), 'dd-MM-yyyy') : ''
}
} catch (e) { }
});
if (data['type'] === 'csv') {
csvData.push(obj);
}
sheet.addRow(obj);
});
if (data['type'] === 'xslx') {
excel.setBorder(sheet, '333333');
excel.saveExcel(workbook, `Report'${today}`)
} else if (data['type'] === 'csv') {
const csv = Papa.unparse(csvData);
excel.saveCSV(csv, `Report'${today}`);
} else {
}
}`
I try to apply this
const csvHeaders = cols.map(col => col.header);
const csv = Papa.unparse({
fields: csvHeaders,
data: csvData
});
but the header was update but no data display. cause the field key not same.