Created
March 17, 2025 04:21
-
-
Save wesleybliss/69ae614fde44b6dc8436ed596bfec762 to your computer and use it in GitHub Desktop.
Converts a basic HTML table into a 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() { | |
const result = [] | |
const tables = [...document.querySelectorAll('table')] | |
const quoteRow = cols => ('"' + cols | |
.map(it => it.textContent || '') | |
.join('","') + '"') | |
for (let i = 0, m = tables.length; i < m; i++) { | |
const table = tables[i] | |
const headers = [...table.querySelectorAll('th')] | |
const rows = [...table.querySelectorAll('tr')] | |
if (headers.length) | |
result.push(quoteRow(headers)) | |
if (!rows?.length) continue | |
for (let j = 0, n = rows.length; j < n; j++) { | |
const row = rows[j] | |
const cols = [...row.querySelectorAll('td')] | |
if (!cols?.length) continue | |
result.push(quoteRow(cols)) | |
} | |
} | |
console.log(result.join('\n')) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment