我正在尝试将 Object 值转换为 CSV,但 join 方法将我的数组值分隔到 Excel 中的不同列中。关于如何避免这种情况的任何想法?
功能:
window.downloadCsv = function(records) {
console.log(records);
const array = [Object.keys(records)].concat(records);
console.log(array);
let result = array.map(it => {
let objectValues = Object.values(it);
for (let i = 0; i < objectValues.length; i++) {
if (Array.isArray(objectValues[i])) {
//Keep it as array
}
}
return objectValues;
}).join('\n');
console.log(result);
let hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(result);
hiddenElement.target = '_blank';
hiddenElement.download = 'records.csv';
hiddenElement.click();
};
可能的记录输入:
id: "5e8468e2db05ff589ca61b30"
title: "Example Application Number 1"
status: "Preparing Documents"
principalInvestigator: "Mr. Harry Styles"
coInvestigators: ["Niall Horan, Liam Payne, Zayn Malik, Louis Tomilson"]
partners: null
funder: "EPSRC Standard research"
researchGroup: "MedEng"
scheme: "Travel Grant"
requestedAmount: null
estimatedAmount: 1234
submissionDate: "2020-03-23T00:00:00.000+01:00"
startDate: "2020-03-29T00:00:00.000+01:00"
estimatedDuration: null
endDate: null
facility: null
comments: null
dateCreated: "2020-04-01T12:11:46.783+02:00"
lastUpdated: "2020-04-01T12:11:46.783+02:00"
dateDeleted: null
__proto__: Object
结果的当前输出:
id,title,status,principalInvestigator,coInvestigators,partners,funder,researchGroup,scheme
5e8468e2db05ff589ca61b30,Example Application Number 1,Preparing Documents,Mr. Harry Styles,Niall Horan, Liam Payne, Zayn Malik, Louis Tomilson
期望的输出:
id,title,status,principalInvestigator,coInvestigators,partners,funder,researchGroup,scheme
Excel 格式可能更容易理解。目前,导出后是这样的:https ://imgur.com/2I8h9kl
所需的外观是:https ://imgur.com/bFUKQY2
因此,我几乎希望将数组值保留在同一列中,而不是将它们分成不同的列,这也会在CSV.
慕森卡
弑天下
慕的地10843
相关分类