Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CSV headers while processing papaparse

Is it possible to change the headers of a "csv" file to uppercase during the process performed by papa.parse using JavaScript?!

Thanks in advance.

like image 849
Biryukov Pavel Avatar asked Jan 31 '16 12:01

Biryukov Pavel


People also ask

Is Papa parse free?

PapaParse - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers.

What is Papaparse?

Papa Parse is the fastest in-browser CSV (or delimited text) parser for JavaScript. It is reliable and correct according to RFC 4180, and it comes with these features: Easy to use. Parse CSV files directly (local or over the network) Fast mode (is really fast)

How do I import Papaparse into react?

To get started with Papa Parse, first import the library. import Papa from 'papaparse'; When using the parse function, you can either choose to parse a string of delimited text, a local file, or a remote file URL. For this example, we will be parsing a local file that we received from React Dropzone in part one.


2 Answers

beforeFirstChunk: function(chunk) {
                    var rows = chunk.split( /\r\n|\r|\n/ );
                    var headings = rows[0].toUpperCase();
                    rows[0] = headings;
                    return rows.join("\r\n");
                },

supporting 'beforeFirstChunk' solved me this issue, nevertheless if you use 'worker: true' inside your configuration it will trigger an exception, looks like a known bug.

like image 55
Biryukov Pavel Avatar answered Oct 01 '22 19:10

Biryukov Pavel


As of version 5.0, PapaParse now has transformHeader, which is

A function to apply on each header. Requires header to be true. The function receives the header as its first argument. Only available starting with version 5.0.

You can see an example at Using PapaParse transformHeader to remove whitespace from headers?.

like image 21
Jason Avatar answered Oct 01 '22 18:10

Jason