Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions JsonExcel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export default {
type: Object,
required: true
},
// fields format inside the Json Object that you want to export
// if no given, format to fields default
'format':{
type: Object,
default: null
},
// Title for the data
'title':{
default: null
Expand All @@ -55,7 +61,7 @@ export default {
if(!this.data.length){
return
}
let json = this.getProcessedJson(this.data, this.fields)
let json = this.getProcessedJson(this.data, this.fields, this.format)
if(this.type == 'csv'){
return this.export(this.jsonToCSV(json), this.name, "application/csv");
}
Expand Down Expand Up @@ -99,7 +105,7 @@ export default {
data.map(function (item, index) {
xlsData += '<tbody><tr>'
for (let key in item) {
xlsData += '<td>' + item[key] + '</td>'
xlsData += '<td style="' + item[key].style + '">' + item[key].value + '</td>'
}
xlsData += '</tr></tbody>'
})
Expand Down Expand Up @@ -132,7 +138,7 @@ export default {

data.map(function (item) {
for (let key in item) {
let escapedCSV = item[key] + '' // cast Numbers to string
let escapedCSV = item[key].value + '' // cast Numbers to string
if (escapedCSV.match(/[,"\n]/)) {
escapedCSV = '"' + escapedCSV.replace(/\"/g, "\"\"") + '"'
}
Expand All @@ -148,16 +154,21 @@ export default {
---------------
Get only the data to export, if no fields are set return all the data
*/
getProcessedJson: function(data, header){
getProcessedJson: function(data, header, format){
if( format == null ){
format = {}
}
let keys = this.getKeys(data, header)
let newData = []
let _self = this
data.map(function (item, index) {
let newItem = {}
for( let label in keys){
var iii= item;
let property = keys[label]
newItem[label] = _self.getNestedData(property, item)
newItem[label] = {
'value': _self.getNestedData(property, item),
'style': format[property] ? format[property] : ''
}
}
newData.push(newItem)
})
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ OPTIONAL
```js
title = ["user: 000001","USER REPORT", "Title 3"]
```
- json_format: fields format inside the Json Object that you want to export,
if no given, format to fields default http://cosicimiento.blogspot.com.ar/2008/11/styling-excel-cells-with-mso-number.html
```html
<download-excel
...
:format = "json_format"
...>
</download-excel>
```
```js
json_format: {
"numeric": "mso-number-format:'0'",
"float": "mso-number-format:'0.00'",
"date": "mso-number-format:'mm/dd/yyyy'",
"time": "mso-number-format:'Short Time'",
"text": "mso-number-format:'@'"
}
```

## Export CSV
To export JSON to CSV file just add the prop type with value "csv":
Expand Down