Skip to content

Instantly share code, notes, and snippets.

@felipedaraujo
Created November 23, 2023 23:02
Show Gist options
  • Save felipedaraujo/91be4c4eb740c7a5a739f9f86cf8e597 to your computer and use it in GitHub Desktop.
Save felipedaraujo/91be4c4eb740c7a5a739f9f86cf8e597 to your computer and use it in GitHub Desktop.
Download data from Hasura console into CSV
// Get all table rows with class 'rt-tr'
let rows = document.querySelectorAll('.rt-tr');
// Function to clean and format cell text
let cleanText = (text) => {
return text.trim().replace(/"/g, '""');
};
// Initialize CSV content with header
let csvContent = 'email,first_name,last_name\n';
// Iterate through each row and cell, letruct CSV
rows.forEach((row) => {
let cells = row.querySelectorAll('.rt-td');
if (cells.length === 0) return;
let email = cleanText(cells[0].textContent);
let firstName = cleanText(cells[1].textContent);
let lastName = cleanText(cells[2].textContent);
// Add row data to CSV content
csvContent += `${email},${firstName},${lastName}\n`;
});
// Create a Blob and download CSV file
let blob = new Blob([csvContent], { type: 'text/csv' });
let url = window.URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = url;
a.download = 'table_data.csv';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment