Last active
December 27, 2015 12:29
-
-
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…
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.