Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making it work with JSpreadsheet CE to export files #227

Open
onigetoc opened this issue Oct 21, 2023 · 1 comment
Open

Making it work with JSpreadsheet CE to export files #227

onigetoc opened this issue Oct 21, 2023 · 1 comment

Comments

@onigetoc
Copy link

onigetoc commented Oct 21, 2023

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.

@zheeeng
Copy link
Owner

zheeeng commented Oct 24, 2023

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 }))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants