-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
163 lines (145 loc) · 5.54 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Excel to Typst Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen p-6">
<div class="max-w-4xl mx-auto bg-white shadow-lg rounded-lg p-8">
<h1 class="text-3xl font-bold text-center text-gray-800 mb-6">
Excel to Typst Converter
</h1>
<p class="text-gray-700 text-lg mb-6">
Select an Excel file and click the "Convert" button to generate Typst code. Jump to:
<a
href="/excel-to-typst/paste"
class="text-blue-500 font-semibold mb-6"
>
Clipboard to Typst Converter
</a>
</p>
<div class="flex items-center justify-center">
<input
type="file"
id="file-input"
accept=".xlsx"
class="block w-full text-sm text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-violet-50 file:text-violet-700 hover:file:bg-violet-100"
onchange="if (this.files.length > 0) handleUpload()"
/>
<select
id="sheet-select"
class="ml-4 w-full text-sm text-slate-500 border border-gray-300 rounded-lg py-2 px-4"
></select>
</div>
<button
onclick="handleConvert()"
class="w-full bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded transition duration-300 mt-4"
>
Convert
</button>
<div>
<textarea
id="typst-code"
class="w-full h-[500px] p-4 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:outline-none resize-y mt-4"
></textarea>
</div>
</div>
<script>
function handleUpload() {
const fileInput = document.getElementById("file-input");
const file = fileInput.files[0];
if (!file || !file.name.endsWith(".xlsx")) {
alert("Please select an Excel file");
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: "array" });
const sheetNames = workbook.SheetNames;
const sheetSelect = document.getElementById("sheet-select");
sheetSelect.innerHTML = "";
sheetNames.forEach((sheetName) => {
const option = document.createElement("option");
option.value = sheetName;
option.text = sheetName;
sheetSelect.appendChild(option);
});
};
reader.readAsArrayBuffer(file);
}
function handleConvert() {
const fileInput = document.getElementById("file-input");
const file = fileInput.files[0];
if (!file || !file.name.endsWith(".xlsx")) {
alert("Please select an Excel file");
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: "array" });
const sheetName = document.getElementById("sheet-select").value;
const worksheet = workbook.Sheets[sheetName];
const typstCode = convertToTypst(worksheet);
document.getElementById("typst-code").value = typstCode;
};
reader.readAsArrayBuffer(file);
}
function convertToTypst(worksheet) {
const range = XLSX.utils.decode_range(worksheet["!ref"]);
const columns = range.e.c - range.s.c + 1;
const merges = worksheet["!merges"] || [];
let typstCode = `#table(columns: ${columns},\n`;
for (let row = range.s.r; row <= range.e.r; row++) {
typstCode += " ";
for (let col = range.s.c; col <= range.e.c; col++) {
const cellAddress = XLSX.utils.encode_cell({ r: row, c: col });
const mergeInfo = findMergeInfo(merges, row, col);
if (mergeInfo) {
if (mergeInfo.s.r === row && mergeInfo.s.c === col) {
const cell = worksheet[cellAddress];
const value = cell ? cell.v : "";
const rowspan = mergeInfo.e.r - mergeInfo.s.r + 1;
const colspan = mergeInfo.e.c - mergeInfo.s.c + 1;
typstCode += "table.cell(";
if (rowspan > 1) {
typstCode += `rowspan: ${rowspan}, `;
}
if (colspan > 1) {
typstCode += `colspan: ${colspan}, `;
}
typstCode = typstCode.replace(/,\s*$/, "");
typstCode += `)[${value}], `;
}
} else {
const cell = worksheet[cellAddress];
const value = cell ? cell.v : "";
typstCode += `[${value}], `;
}
}
typstCode += "\n";
}
typstCode += ")";
return typstCode;
}
function findMergeInfo(merges, row, col) {
return merges.find(
(merge) =>
row >= merge.s.r &&
row <= merge.e.r &&
col >= merge.s.c &&
col <= merge.e.c
);
}
document
.getElementById("typst-code")
.addEventListener("input", function () {
renderTypst(this.value);
});
</script>
</body>
</html>