Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Last active December 27, 2015 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AABoyles/7325642 to your computer and use it in GitHub Desktop.
Save AABoyles/7325642 to your computer and use it in GitHub Desktop.
*Don't use this Gist!* If you're looking to work with CSVs in Javascript, give [Tabular.js](http://aaboyles.com/Tabular.js/) a try. It was designed after this gist to provide a more complete solution to CSV handling in Javascript. Given a table element (or jQuery-selected table), returns a string which can be used as download URL for the table i…
function table2CSV(table, delim){
var csv = "data:application/csv;charset=utf-8,";
if(table instanceof HTMLElement){
table = $(table);
}
if(table instanceof jQuery){
var regex = /,/g;
if(delim){
regex = new RegExp(delim, 'g');
delim = encodeURIComponent(delim);
} else {
delim = '%2C';
}
var rows = table.find("tr");
rows.each(function(index, row){
var cells = [];
$(row).find("th, td").each(function(index2, cell){
cells.push($(cell).text().replace(regex, ''));
});
csv += cells.join(delim) + '%0A';
});
}
return csv;
}
@AABoyles
Copy link
Author

AABoyles commented Jan 2, 2014

Don't use this Gist! If you're looking to work with CSVs in Javascript, give Tabular.js a try. It was designed after this gist to provide a more complete solution to CSV handling in Javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment