Created
September 7, 2017 21:55
-
-
Save pulkitsinghal/0e8b71936537cd09362b4427a090d76f to your computer and use it in GitHub Desktop.
Replace empty headers with misc. header names in PapaParse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// References: | |
// https://stackoverflow.com/questions/35113926/change-csv-headers-while-processing-papaparse | |
// https://github.com/mholt/PapaParse/issues/196 | |
// https://github.com/mholt/PapaParse/pull/192/files | |
// https://github.com/mholt/PapaParse/issues/413 | |
beforeFirstChunk: function(chunk) { // replace empty headers with misc. header names | |
var rows = chunk.split( /\r\n|\r|\n/ ); | |
//console.log(rows[0]); | |
var headings = rows[0]; //var headings = '"Item Number","Vend Reorder Number","Description","Item Category","Price-1","Last Cost","Account Code","Attribute-1","","Attribute-2","Attribute-3","Attribute-4","Attribute-5","Attribute-6","Category/Subcategory","Default cost of sale %","Eccomerce Item"," ","Item Type","Long Description","Price Desimal","Quantity Desimal","Regular Price","","Status","Subcategory","Primary Vendor"'; | |
var counter = 1; | |
while (/,"\s*",/.exec(headings)) { | |
headings = headings.replace( /,"\s*",/ , ',"misc'+counter+'",' ); | |
counter++; | |
} | |
rows[0] = headings; | |
//console.log(rows[0]); | |
return rows.join('\r\n'); | |
//return chunk; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment