Last active
November 7, 2022 21:41
-
-
Save jsullivan5/e7319e55e1effc9a3bf068cf7d0ff943 to your computer and use it in GitHub Desktop.
Browser script to convert HTML tables to CSV
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 () { | |
/** | |
* @instructions | |
* 1. RENAME FILE_NAME TO CHANGE THE NAME OF THE EXPORT FILE | |
* const FILE_NAME = 'whatever_you_want.csv' | |
* | |
* 2. Open the browser console on the page you want to copy (Chrome) | |
* command + option + j | |
* | |
* 3. Copy and paste the entirety of this Gist to download the table | |
* | |
* Reference: | |
* codexworld.com/export-html-table-data-to-csv-using-javascript/ | |
* https://stackoverflow.com/questions/7275650/javascript-replace-command-replace-page-text/7275856#7275856 | |
*/ | |
const FILE_NAME = 'export.csv'; | |
// Removes commas from the document that mess up the comma seperated CSV | |
function removeCommasFromDocument() { | |
// create a TreeWalker of all text nodes | |
const allTextNodes = document.createTreeWalker( | |
document.body, | |
NodeFilter.SHOW_TEXT, | |
); | |
// some temp references for performance | |
let tmptxt; | |
let tmpnode; | |
// compile the RE and cache the replace string, for performance | |
const commaRE = /,/g; | |
const replaceValue = ''; | |
// iterate through all text nodes | |
while (allTextNodes.nextNode()) { | |
tmpnode = allTextNodes.currentNode; | |
tmptxt = tmpnode.nodeValue; | |
tmpnode.nodeValue = tmptxt.replace(commaRE, replaceValue); | |
} | |
} | |
// Creates html download link with the csv data | |
function downloadCSV(csv, filename) { | |
let csvFile; | |
let downloadLink; | |
// CSV file | |
csvFile = new Blob([csv], { type: 'text/csv' }); | |
// Download link | |
downloadLink = document.createElement('a'); | |
// File name | |
downloadLink.download = filename; | |
// Create a link to the file | |
downloadLink.href = window.URL.createObjectURL(csvFile); | |
// Hide download link | |
downloadLink.style.display = 'none'; | |
// Add the link to DOM | |
document.body.appendChild(downloadLink); | |
// Click download link | |
downloadLink.click(); | |
} | |
// Gets all the data from the html table and exports it | |
function exportTableToCSV(filename) { | |
const csv = []; | |
const rows = document.querySelectorAll('table tr'); | |
for (let i = 0; i < rows.length; i++) { | |
const row = []; | |
const cols = rows[i].querySelectorAll('td, th'); | |
for (let j = 0; j < cols.length; j++) | |
row.push(cols[j].innerText); | |
csv.push(row.join(',')); | |
} | |
// Download CSV file | |
downloadCSV(csv.join('\n'), filename); | |
} | |
// Put it all together | |
removeCommasFromDocument(); | |
exportTableToCSV(FILE_NAME); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment