Node.js 如何在将Json转换为CSV时在json2csv库中提供自定义字段
我正在使用Node.js 如何在将Json转换为CSV时在json2csv库中提供自定义字段,node.js,csv,express,Node.js,Csv,Express,我正在使用json2csv库将Json转换为CSV,这样做很成功 status = Utils.getKey(InvoiceStatus, invoice.status); if (InvoiceStatus.isOverDue(invoice.status, invoice.dueDate)) status = 'overdue'; const fields = [ 'id', // 'inv
json2csv
库将Json转换为CSV,这样做很成功
status = Utils.getKey(InvoiceStatus, invoice.status);
if (InvoiceStatus.isOverDue(invoice.status, invoice.dueDate))
status = 'overdue';
const fields = [
'id',
// 'invoicePrefix + ' - ' + invoiceNo',
'invoiceDate',
'dueDate',
'customerId',
'customerName',
{
label: 'status',
value: status,
},
// 'status',
'itemDesc',
'invoiceAmount',
'dueAmount'
];
const opts = { fields };
try {
const parser = new Json2csvParser(opts);
const csv = parser.parse(invoices);
res.send(csv);
} catch (err) {
console.error(err);
}
}
但是问题出现在这里,我想把两列
'invoicePrefix+'-'+invoiceNo'
并显示它,但我不知道怎么做
还有一件事,你们可以在变量字段中看到这一行
{
label: 'status',
value: status,
},
正如您所看到的,status是存储在变量中的某个值,我正在为value键提供变量状态,但它没有在CSV中显示变量值,因此是否有任何方法可以解决此问题,以便它也打印该变量值。您需要将输入json转换为所需格式,以便库使用正确的json和convert没有任何错误 转换为如下格式
[
{
"Invoice Number": invoicePrefix + ' - ' + invoiceNo,
"Status": "paid"
}
]
这将导致
Invoice Number, Status
ABC123, paid
根据文件,
将转换为
price
1000
2000
请仔细阅读文档您需要将输入的json转换为所需的格式,以便库使用正确的json,并且转换时不会出现任何错误 转换为如下格式
[
{
"Invoice Number": invoicePrefix + ' - ' + invoiceNo,
"Status": "paid"
}
]
这将导致
Invoice Number, Status
ABC123, paid
根据文件,
将转换为
price
1000
2000
请仔细阅读文档使用正确的语法:
const fields = [
'id',
// We set the header label and we provide a function to fill each cell with row data
{
label: invoicePrefix + ' - ' + invoiceNo,
value: (row, field) => row.invoicePrefix + ' - ' + row.invoiceNo
},
'invoiceDate',
'dueDate',
'customerId',
'customerName',
// We set the header label and we provide a function to fill each cell with row data, in this case the same for all rows
{
label: 'status',
value: () => status,
},
// 'status',
'itemDesc',
'invoiceAmount',
'dueAmount'
];
使用正确的语法:
const fields = [
'id',
// We set the header label and we provide a function to fill each cell with row data
{
label: invoicePrefix + ' - ' + invoiceNo,
value: (row, field) => row.invoicePrefix + ' - ' + row.invoiceNo
},
'invoiceDate',
'dueDate',
'customerId',
'customerName',
// We set the header label and we provide a function to fill each cell with row data, in this case the same for all rows
{
label: 'status',
value: () => status,
},
// 'status',
'itemDesc',
'invoiceAmount',
'dueAmount'
];
您还可以使用tilde操作符获取状态的实际值。 下面允许您直接使用json2csv下载生成的csv,重命名标题字段并展平嵌套对象结构
const fields = [
'_id',
{
label : 'product_title',
value: `title`,
},
{
label : 'product_details',
value: `description`,
},
'price',
{
label : 'availability',
value: `inventory.qty`,
}
];
const {Parser} = require('json2csv');
const jsonToCsv = new Parser({fields: fields, unwind: 'inventory', unwindBlank: true});
try{
const csv = jsonToCsv.parse(products);
response.attachment('products.csv')
response.status(200).send(csv);
} catch (error) {
console.log('error:', error.message)
response.status(500).send(error.message)
}
您还可以使用tilde操作符获取状态的实际值。 下面允许您直接使用json2csv下载生成的csv,重命名标题字段并展平嵌套对象结构
const fields = [
'_id',
{
label : 'product_title',
value: `title`,
},
{
label : 'product_details',
value: `description`,
},
'price',
{
label : 'availability',
value: `inventory.qty`,
}
];
const {Parser} = require('json2csv');
const jsonToCsv = new Parser({fields: fields, unwind: 'inventory', unwindBlank: true});
try{
const csv = jsonToCsv.parse(products);
response.attachment('products.csv')
response.status(200).send(csv);
} catch (error) {
console.log('error:', error.message)
response.status(500).send(error.message)
}