Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created March 17, 2025 04:21
Show Gist options
  • Save wesleybliss/69ae614fde44b6dc8436ed596bfec762 to your computer and use it in GitHub Desktop.
Save wesleybliss/69ae614fde44b6dc8436ed596bfec762 to your computer and use it in GitHub Desktop.
Converts a basic HTML table into a CSV
(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