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
Add a ment  | 

1 Answer 1

Reset to default 4

For 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