Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created September 7, 2017 21:55
Show Gist options
  • Save pulkitsinghal/0e8b71936537cd09362b4427a090d76f to your computer and use it in GitHub Desktop.
Save pulkitsinghal/0e8b71936537cd09362b4427a090d76f to your computer and use it in GitHub Desktop.
Replace empty headers with misc. header names in PapaParse
// 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