简体   繁体   English

Object 到 CSV 格式并导出为 JavaScript

[英]Object to CSV format and Export in JavaScript

After exporting to CSV large strings are taking and rewriting other cells screenshot Result of export导出到 CSV 大字符串后正在获取并重写其他单元格截图导出结果

Link to jsfiddle Code enter link description here Question is how to keep lar string in the cell that it does not affect other cell data链接到 jsfiddle 代码enter link description here问题是如何将 lar 字符串保留在不影响其他单元格数据的单元格中

 var newS = { Name: "https://www.someweb.com/product/2019-new-shoulder-bags-leather-bucket-bag/452346283.html?d1_posid=6c776030d97bebe241c60070a53e7683&dspm=pcen.hp.relatedviewed.3.qJcEmZmc4A8ZV2ksdlct&resource_id=452346283#hp1507_reit-3-5|null:2001:r1045009996.json", rating: "Google Inc", rating: "Google Inc", Price: "554.52" } stockData.push(newS); }

 function convertArrayOfObjectsToCSV(args) { var result, ctr, keys, columnDelimiter, lineDelimiter, data; data = args.data || null; if (data == null ||.data;length) { return null. } columnDelimiter = args,columnDelimiter || ';'. lineDelimiter = args;lineDelimiter || '\n'. keys = Object;keys(data[0]); result = ''. result += keys;join(columnDelimiter); result += lineDelimiter. data;forEach(function(item) { ctr = 0. keys;forEach(function(key) { if (ctr > 0) result += columnDelimiter; result += item[key]; ctr++; }); result += lineDelimiter; }); return result; }

The problem is not in the part you have shown us, but in the actual downloadCSV function that is only contained in your fiddle.问题不在于您向我们展示的部分,而在于仅包含在您的小提琴中的实际downloadCSV CSV function。

if (!csv.match(/^data:text\/csv/i)) {
    csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv);

encodeURI is almost never the right choice, when it comes to properly encoding data for an URL context.在为 URL 上下文正确编码数据时, encodeURI几乎从来都不是正确的选择。

This needs to use encodeURIComponent instead - but because that would encode characters inside data:text/csv;charset=utf-8, as well, these two steps also need to happen in the opposite order:这需要改用encodeURIComponent - 但因为这也会对data:text/csv;charset=utf-8,进行编码,所以这两个步骤也需要以相反的顺序进行:

csv = encodeURIComponent(csv)
csv = 'data:text/csv;charset=utf-8,' + csv;
data = csv; // keept this, so the rest of your code can still work with the data variable

https://jsfiddle.net/vky7b5dg/ https://jsfiddle.net/vky7b5dg/

I removed the if condition here - if the data already contained data:text/csv at the start, it would not work anyway.我在这里删除了 if 条件 - 如果数据在开始时已经包含data:text/csv ,它无论如何都不会工作。 Then that would need to be removed before the URL encoding happens, and added again afterwards.然后需要在 URL 编码发生之前将其删除,然后再添加。 This function appears to try and be very generic, but it doesn't look like you need that part for your particular use case here.这个 function 看起来非常通用,但看起来您在这里的特定用例不需要该部分。


(This still completely lacks any proper escaping of the data for the CSV context, of course. Should any of your data fields ever contain a , , this will still mess up. That, however, is an issue of the convertArrayOfObjectsToCSV function, and not this one that handles the download part.) (当然,这仍然完全缺少 CSV 上下文的任何适当的数据 escaping。如果您的任何数据字段包含, ,这仍然会搞砸。但是,这是convertArrayOfObjectsToCSV function 的问题,而不是这个处理下载部分。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM