zheeeng/export-from-json

Making it work with JSpreadsheet CE to export files

onigetoc opened this issue · 1 comments

I'm using JSpreadsheet CE and i want to export the json to XLSX, XLS, CSV.
https://github.com/jspreadsheet/ce

The problem is JSpreadsheet CE give me this object:

[
    [
        "Ticker",
        "Name",
        "Exchange",
        "Category Name",
        "Country",
        "F",
        "G",
        "H",
        "I",
        "J"
    ],
    [
        "OEDV",
        "Osage Exploration and Development, Inc.",
        "PNK",
        "",
        "USA",
        "",
        "",
        "",
        "",
        ""
    ],
    [
        "AAPL",
        "Apple Inc.",
        "NMS",
        "Electronic Equipment",
        "USA",
        "",
        "",
        "",
        "",
        ""
    ]

But when i convert it with export-from-json to CSV it will give me:

0,1,2,3,4,5,6,7,8,9 // WRONG HEADER
Ticker,Name,Exchange,Category Name,Country,F,G,H,I,J // THE REAL RIGHT HEADER
OEDV,"Osage Exploration and Development, Inc.",PNK,,USA,,,,,
AAPL,Apple Inc.,NMS,Electronic Equipment,USA,,,,,

// To get that json i'm using the Jspread function: getHeaders() and getData()

const getHeaders = table.getHeaders('0');
const getDatas = table.getData();
const newArray = [getHeaders].concat(getDatas) // [ 4, 3, 2, 1 ] // I MERGE IT WITH THE DATA
const fileName = 'file'
const exportType =  exportFromJSON.types.csv

download(newArray, fileName, getHeaders, exportType) // THIS FUNCTION WILL CALL:

function download(data, fileName, fields, exportType) {
    console.log(fields)
    fields = 0 ? fields : [] 
// fieds = ["Ticker","Name","Exchange","Category Name","Country","F","G","H","I","J"]
    window.exportFromJSON({ data, fileName, fields, exportType })
}

It's working but exportFromJSON will always add on the first CSV line: 0,1,2,3,4,5,6,7,8,9....

I think it's because in the array of the header (first line) the names are not in the getData() loop and export-From-JSON will think there's no header or is not compatible.
But it's a valid way to do it and JSpreadSheet (or any CSV files readers) will know that the first line is the header. but not the new saved file with exportFromJSON. Because it will add these numbers on the first line: 0,1,2,3,4,5,6,7,8,9 and it will become the new header.

How can i tell exportFromJSON to not do that?
It look like that if exportFromJSON do not find the headers keywords in the datas from the header array (fields) it will add these numbers or it will just not work.

It will also add these numbers in the XLSX file ect. But if it would add the header fields instead it would probably work perfectly to export my CSV or Excel files made with JSpreadSheet-CE.

By default we treat the object's keys and array's indices as col title, you can omit the indices by using beforeTableEncode.

Add such option to EFJ:

beforeTableEncode: rows => rows.map(row => ({ fieldName: '', fieldValues: row.fieldValues }))