-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_editor.html
211 lines (178 loc) · 4.72 KB
/
csv_editor.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h1 {
text-align: center;
}
input[type="file"] {
display: block;
margin: 20px auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
td {
position: relative;
}
td input {
width: 100%;
box-sizing: border-box;
border: none;
padding: 4px;
}
#editCanvas {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border: 2px solid black;
padding: 20px;
z-index: 1000;
width: 80%;
height: 60%;
}
.hidden {
display: none;
}
textarea {
width: 100%;
height: 80%;
box-sizing: border-box;
}
button {
display: block;
margin: 10px auto;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV Editor</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>CSV Editor</h1>
<input type="file" id="fileInput" />
<button id="downloadBtn">Download CSV</button>
<table id="csvTable"></table>
<!-- Canvas for editing -->
<div id="editCanvas" class="hidden">
<textarea id="editArea"></textarea>
<button id="saveEditBtn">Save</button>
<button id="cancelEditBtn">Cancel</button>
</div>
<script>
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
document.getElementById('downloadBtn').addEventListener('click', downloadCSV);
document.getElementById('saveEditBtn').addEventListener('click', saveEdit);
document.getElementById('cancelEditBtn').addEventListener('click', cancelEdit);
let csvData = [];
let currentCell = { row: null, cell: null };
function handleFileSelect(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
const text = e.target.result;
parseCSV(text);
};
reader.readAsText(file);
}
function parseCSV(text) {
csvData = [];
const rows = text.split('\n');
rows.forEach(row => {
if (row.trim() === '') return; // Skip empty rows
csvData.push(parseCSVRow(row));
});
renderTable();
}
function parseCSVRow(row) {
const cells = [];
let cell = '';
let insideQuotes = false;
for (let i = 0; i < row.length; i++) {
const char = row[i];
if (char === '"') {
insideQuotes = !insideQuotes;
} else if (char === ',' && !insideQuotes) {
cells.push(cell);
cell = '';
} else {
cell += char;
}
}
cells.push(cell);
return cells;
}
function renderTable() {
const table = document.getElementById('csvTable');
table.innerHTML = '';
csvData.forEach((row, rowIndex) => {
const tr = document.createElement('tr');
row.forEach((cell, cellIndex) => {
const td = document.createElement('td');
if (rowIndex === 0) {
td.innerHTML = `<strong>${cell}</strong>`;
} else {
td.innerHTML = `<input type="text" value="${cell}" data-row="${rowIndex}" data-cell="${cellIndex}" />`;
td.querySelector('input').addEventListener('click', () => openEditCanvas(rowIndex, cellIndex));
}
tr.appendChild(td);
});
table.appendChild(tr);
});
}
function openEditCanvas(row, cell) {
currentCell = { row, cell };
const editCanvas = document.getElementById('editCanvas');
const editArea = document.getElementById('editArea');
editArea.value = csvData[row][cell];
editCanvas.classList.remove('hidden');
}
function saveEdit() {
const editArea = document.getElementById('editArea');
csvData[currentCell.row][currentCell.cell] = editArea.value;
renderTable();
closeEditCanvas();
}
function cancelEdit() {
closeEditCanvas();
}
function closeEditCanvas() {
const editCanvas = document.getElementById('editCanvas');
editCanvas.classList.add('hidden');
}
function downloadCSV() {
let csvContent = csvData.map(row => stringifyCSVRow(row)).join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'edited.csv';
link.click();
}
function stringifyCSVRow(row) {
return row.map(cell => {
if (cell.includes('"') || cell.includes(',') || cell.includes('\n')) {
return `"${cell.replace(/"/g, '""')}"`;
}
return cell;
}).join(',');
}
</script>
</body>
</html>