Skip to content

Instantly share code, notes, and snippets.

@edavis25
Last active March 8, 2017 03:25
Show Gist options
  • Save edavis25/31840724830e7395a07ed22e185b732b to your computer and use it in GitHub Desktop.
Save edavis25/31840724830e7395a07ed22e185b732b to your computer and use it in GitHub Desktop.
Get HTML table data as a CSV formatted string
function tableAsCsv(id) {
// Make sure table exists and contains at least 1 row
if (document.getElementById(id) == null || document.getElementById(id).rows[0] == null) {
return;
}
// Get reference to table & number of rows
var tbl = document.getElementById(id);
var numRows = tbl.rows.length;
// Iterate table and build formatted csv string
var result = '';
for (var i = 0; i < numRows; i++) {
for (var j = 0; j < tbl.rows[i].cells.length; j++) {
result += tbl.rows[i].cells[j].innerHTML;
result += ",";
}
result += "\n";
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment