-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (47 loc) · 1.82 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let CSV = document.getElementById('csv');
let button = document.getElementById('btn');
CSV.addEventListener('change', (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target.result;
const rows = content.split('\n').map(row => row.split(','));
const table = document.getElementById('table');
table.innerHTML = '';
for (let i = 0; i < rows.length; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < rows[i].length; j++) {
let td = document.createElement('td');
td.textContent = rows[i][j];
tr.appendChild(td); }
table.appendChild(tr); }
CSV.style.display = 'none';
button.style.display = 'block';};
reader.readAsText(file);
});
button.addEventListener('click', () => {
const rows = document.querySelectorAll('#table tr');
let csvContent = '';
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let cols = row.querySelectorAll('td');
let rowContent = '';
for (let j = 0; j < cols.length; j++) {
let col = cols[j];
rowContent += col.textContent + ',';
}
csvContent += rowContent.slice(0, -1) + '\n';
}
const blob = new Blob([csvContent],
{
type: 'text/csv'
});
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'exported_data.csv';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
});