ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Arrays with numbers are cut off after 5 digits #55

Closed ryanf12 closed 4 years ago

ryanf12 commented 4 years ago

If you run the example file and replace the records with numerical values - it does not seem to return the expected results:

       const csvWriter = createCsvWriter({
        path: '/www/pages/node/socket.io/examples/IR/public/configurations/export/test.csv',
        header: [
            {id: 'name', title: 'NAME'},
            {id: 'lang', title: 'LANGUAGE'}
        ]
    });

    const records = [
        {name: 'Bob',  lang: '400,300,200,100,150,250,400,450'},
        {name: 'Mary', lang: 'English'}
    ];

    csvWriter.writeRecords(records)       
        .then(() => {
            console.log('...Done');
        });

This will output:

NAME LANGUAGE
Bob 400,300,200,100,150,000,000,000
Mary English

Everything after the 5th index is converted to 000. In this example, the 250,400,450 will be converted to 000, 000, 000.

A workaround is to add a string into the data, like this: {name: 'Bob', lang: '400,300,200,100,150,250,400,450, string'}. Is there a way to write an array of numbers to a csv with this without adding in a string to the data?

ryu1kn commented 4 years ago

Hi @ryanf12 , that sounds terribly wrong. Can you share in what environment you ran the script?

I ran the following script and got the result fine.

const createCsvWriter = require('csv-writer').createObjectCsvWriter;

const csvWriter = createCsvWriter({
  path: 'test.csv',
  header: [
      {id: 'name', title: 'NAME'},
      {id: 'lang', title: 'LANGUAGE'}
  ]
});

const records = [
  {name: 'Bob',  lang: '400,300,200,100,150,250,400,450'},
  {name: 'Mary', lang: 'English'}
];

csvWriter.writeRecords(records)
  .then(() => {
      console.log('...Done');
  });
$ node app
...Done

$ cat test.csv
NAME,LANGUAGE
Bob,"400,300,200,100,150,250,400,450"
Mary,English
ryu1kn commented 4 years ago

Hi @ryanf12 , is this still an issue? I'm closing this for now, but if you've got more information to share, feel free to reopen it.