Skip to content

Instantly share code, notes, and snippets.

@samuraitruong
Created April 8, 2024 01:43
Show Gist options
  • Save samuraitruong/21fe38118ff2cf988b1341cee9847926 to your computer and use it in GitHub Desktop.
Save samuraitruong/21fe38118ff2cf988b1341cee9847926 to your computer and use it in GitHub Desktop.
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