【发布时间】:2019-09-09 14:55:03
【问题描述】:
从 Cloudant 将数据导出到 CSV 时遇到一些小问题。当前使用此处找到的 CSV 函数:https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/
问题是一些数据后来添加了 2-3 个字段。当它下载文档时,它只是将信息一个接一个地放置,并且它不能解释一些旧数据丢失的字段,因此数据会错位。
我尝试创建函数来尝试检测该字段是否存在,以及它是否没有将其设置为空字符串。
这是我尝试过的,它给了我错误:{"error":"compilation_error","reason":"Expression does not eval to a function.
// output HTTP headers
start({
headers: { 'Content-Type': 'text/csv' },
});
// iterate through the result set
while(row = getRow()) {
// get the doc (include_docs=true)
var doc = row.doc;
// if this is the first row
if (first) {
// output column headers
send(Object.keys(doc).join(',') + 'n');
first = false;
}
// build up a line of output
var line = '';
// iterate through each row
//for(var i in doc) {
if (doc.hasOwnProperty('customerNumber')) {
} else {
doc.customerNumber = '';
}
if (doc.hasOwnProperty('affiliateNumber')) {
} else {
doc.affiliateNumber = '';
}
if (doc.hasOwnProperty('dunsNumber')) {
} else {
doc.dunsNumber = '';
}
if (doc.hasOwnProperty('dunsDomestic')) {
} else {
doc.dunsDomestic = '';
}
if (doc.hasOwnProperty('dunsGlobal')) {
} else {
doc.dunsGlobal = '';
}
if (doc.hasOwnProperty('countryCode')) {
} else {
doc.countryCode = '';
}
if (doc.hasOwnProperty('companyName')) {
} else {
doc.companyName = '';
}
if (doc.hasOwnProperty('countryPreapprovedAmt')) {
} else {
doc.countryPreapprovedAmt = '';
}
if (doc.hasOwnProperty('preapprovedAmt')) {
} else {
doc.preapprovedAmt = '';
}
if (doc.hasOwnProperty('currency')) {
} else {
doc.currency = '';
}
if (doc.hasOwnProperty('expirationDate')) {
} else {
doc.expirationDate = '';
}
if (doc.hasOwnProperty('blacklist')) {
} else {
doc.blacklist = '';
}
line += doc.customerNumber + ',' + doc.affiliateNumber + ',' + doc.dunsNumber+ ',' + doc.dunsDomestic+ ',' + doc.dunsGlobal+ ',' + doc.countryCode+ ',' + doc.companyName+ ',' + doc.countryPreapprovedAmt+ ',' + doc.preapprovedAmt+ ',' + doc.currency+ ',' + doc.expirationDate+ ',' + doc.blacklist;
//}
line += 'n';
// send the line
send(line);
}
};
当它遇到不包含所有这些字段的数据时,它会检测到它。分配一个空字符串,以便在下载 csv 时数据对齐。
【问题讨论】:
-
嗨@user3413540。您的问题到底是什么,我们能提供什么帮助?
-
在将 jsons 导出为 csv 时,假设 1 条数据有 10 个字段。它将一个接一个地连续放置每条数据。虽然有一些较旧的数据有 7-8 个字段,但当它一一放置时,它在 csv 文件中不对齐。我正在寻找一种方法来检测是否缺少字段,用空格填充它们以正确对齐所有内容。
-
我认为格林的回答应该符合这个描述。
标签: javascript csv couchdb cloudant