-
-
Save samuraitruong/21fe38118ff2cf988b1341cee9847926 to your computer and use it in GitHub Desktop.
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 tableToCSV(table) { | |
let rows = table.querySelectorAll('tr'); | |
let csv = []; | |
// Iterate through rows | |
rows.forEach((row) => { | |
let csvRow = []; | |
let cells = row.querySelectorAll('td, th'); | |
// Iterate through cells | |
cells.forEach((cell) => { | |
csvRow.push(cell.innerText); | |
}); | |
csv.push(csvRow.join(',')); | |
}); | |
return csv.join('\n'); | |
} | |
// Find the table by its ID | |
let table = document.getElementById('myTable'); | |
if (table) { | |
// Convert table to CSV | |
let csvContent = tableToCSV(table); | |
// Print CSV content to console | |
console.log(csvContent); | |
} else { | |
console.error('Table not found'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment