admin管理员组文章数量:1420536
I'm exporting json files to csv using Filesaver.js and json-export-excel.js. The ma separator is causing the columns to shift when it sees a ma in the string.
Plunker Demo
How can i ignore mas found within string?
<button ng-json-export-excel data="data" report-fields="{name: 'Name', quote: 'Quote'}" filename ="'famousQuote'" separator="," class="purple_btn btn">Export to Excel</button>
JS File:
$scope.data = [
{
name: "Jane Austen",
quote: "It isn\'t what we say or think that defines us, but what we do.",
},
{
name: "Stephen King",
quote: "Quiet people have the loudest minds.",
},
]
Current CSV output (Not Desired): (Note: |
marks the columns in csv file)
Name Quote
Jane Austen | It isn't what we say or think that defines us| but what we do.|
Stephen King| Quiet people have the loudest minds. | |
Desired CSV output:
Name Quote
Jane Austen | It isn't what we say or think that defines us, but what we do.|
Stephen King| Quiet people have the loudest minds. |
I'm exporting json files to csv using Filesaver.js and json-export-excel.js. The ma separator is causing the columns to shift when it sees a ma in the string.
Plunker Demo
How can i ignore mas found within string?
<button ng-json-export-excel data="data" report-fields="{name: 'Name', quote: 'Quote'}" filename ="'famousQuote'" separator="," class="purple_btn btn">Export to Excel</button>
JS File:
$scope.data = [
{
name: "Jane Austen",
quote: "It isn\'t what we say or think that defines us, but what we do.",
},
{
name: "Stephen King",
quote: "Quiet people have the loudest minds.",
},
]
Current CSV output (Not Desired): (Note: |
marks the columns in csv file)
Name Quote
Jane Austen | It isn't what we say or think that defines us| but what we do.|
Stephen King| Quiet people have the loudest minds. | |
Desired CSV output:
Name Quote
Jane Austen | It isn't what we say or think that defines us, but what we do.|
Stephen King| Quiet people have the loudest minds. |
Share
Improve this question
edited Dec 21, 2016 at 21:36
jashuang1130
asked Dec 21, 2016 at 15:24
jashuang1130jashuang1130
4297 silver badges21 bronze badges
1
- 1 did you tried to escape the separator? – Hitmands Commented Dec 21, 2016 at 20:53
1 Answer
Reset to default 4For Excel, you need to wrap values in quotation marks. See this question.
In json-export-excel.js
you'll see that the _objectToString
method wraps the output in quotes but because the fieldValue
variable isn't an object this is never called for this example.
function _objectToString(object) {
var output = '';
angular.forEach(object, function(value, key) {
output += key + ':' + value + ' ';
});
return '"' + output + '"';
}
var fieldValue = data !== null ? data : ' ';
if fieldValue !== undefined && angular.isObject(fieldValue)) {
fieldValue = _objectToString(fieldValue);
}
If you add an else statement
to this to wrap the value in quotes, the CSV opens in Excel as desired.
} else if (typeof fieldValue === "string") {
fieldValue = '"' + fieldValue + '"';
}
Plunker
本文标签: javascriptignore commas within string in JSON when exporting to csvStack Overflow
版权声明:本文标题:javascript - ignore commas within string in JSON when exporting to csv - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744573612a2613487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论