Skip to content

Instantly share code, notes, and snippets.

@met
Created January 24, 2024 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save met/71006b6b0e8a35d246bcb02914cd984e to your computer and use it in GitHub Desktop.
Save met/71006b6b0e8a35d246bcb02914cd984e to your computer and use it in GitHub Desktop.
Export 50languages page to CSV containing language data for study
// This is a modified script from https://www.geeksforgeeks.org/how-to-export-html-table-to-csv-using-javascript/
function tableToCSV() {
// Variable to store the final csv data
let csv_data = [];
let table = document.getElementById('table1');
// Get each row data
let rows = table.getElementsByTagName('tr');
for (let i = 0; i < rows.length; i++) {
// Get each column data
let cols = rows[i].querySelectorAll('td,th');
if (cols.length <= 1) continue; // skip rows a spacer
// Stores each csv row data
let csvrow = [];
csvrow.push(cols[0].innerText.trim());
if (cols[1].querySelectorAll('span').length <= 0) {
// headers line
csvrow.push(cols[1].innerText.trim());
}
else
{
csvrow.push(cols[1].querySelectorAll('span')[1].innerText.trim()); // use only second span with full foreign text
}
/*
for (let j = 0; (j < cols.length) && (j < 2); j++) {
// Get the text data of each cell
// of a row and push it to csvrow
csvrow.push(cols[j].innerText.trim());
}
*/
// Combine each column value with comma
csv_data.push(csvrow.join(";"));
}
// Combine each row data with new line character
csv_data = csv_data.join('\n');
// Call this function to download csv file
downloadCSVFile(csv_data);
}
function downloadCSVFile(csv_data) {
// Create CSV file object and feed
// our csv_data into it
CSVFile = new Blob([csv_data], {
type: "text/csv"
});
// Create to temporary link to initiate
// download process
let temp_link = document.createElement('a');
// Download csv file
//temp_link.download = "GfG.csv";
// Generate file name according to number in URL
temp_link.download = document.location.toString().match(/\b\d+\b/)[0] + ".csv";
let url = window.URL.createObjectURL(CSVFile);
temp_link.href = url;
// This link should not be displayed
temp_link.style.display = "none";
document.body.appendChild(temp_link);
// Automatically click the link to
// trigger download
temp_link.click();
document.body.removeChild(temp_link);
}
document.getElementById("display-hide-all").click(); tableToCSV();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment