Last active
March 8, 2017 03:25
-
-
Save edavis25/31840724830e7395a07ed22e185b732b to your computer and use it in GitHub Desktop.
Get HTML table data as a CSV formatted string
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 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