Created
April 16, 2017 18:59
-
-
Save wonga00/6b7e336574c423314156dd3fadbe97a0 to your computer and use it in GitHub Desktop.
console code to save data out of a html table
This file contains 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 saveToFile(text, filename) { | |
let hiddenElement = document.createElement('a'); | |
hiddenElement.href = 'data:attachment/text,' + encodeURI(text); | |
hiddenElement.target = '_blank'; | |
hiddenElement.download = filename; | |
hiddenElement.click(); | |
} | |
function saveTable(selector, filename) { | |
let table = document.querySelector(selector), | |
data = ""; | |
table.querySelectorAll('tr').forEach(tr => { | |
let contents = []; | |
for (let i = 0, max = tr.children.length; i < max; i++) { | |
contents.push(tr.children[i].textContent.replace(/"/g, "\"\"").trim()); | |
} | |
data += contents.map(c => "\"" + c + "\"").join(',') + '\n'; | |
}); | |
saveToFile(data, filename) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment