Skip to content

Instantly share code, notes, and snippets.

@wonga00
Created April 16, 2017 18:59
Show Gist options
  • Save wonga00/6b7e336574c423314156dd3fadbe97a0 to your computer and use it in GitHub Desktop.
Save wonga00/6b7e336574c423314156dd3fadbe97a0 to your computer and use it in GitHub Desktop.
console code to save data out of a html table
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