-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
95 lines (87 loc) · 2.77 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>i18nExcel2Json</title>
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
<style>
.result {
display: flex;
}
* {
user-select: none;
}
pre {
user-select: all;
}
</style>
</head>
<body>
<input type="file" name="" id="fileInput">
<a href="./in.xlsx">下载模板文件</a>
<button id="copyBtn">复制结果</button>
<a href="./merge.html" style="margin-left: 10px;" target="_blank">跳转:merge i18n json</a>
<div class="result">
<div id="TableContainer"></div>
<pre id="JSOnContainer"></pre>
</div>
<script>
let fileInput = document.querySelector('#fileInput')
let JSOnContainer = document.querySelector('#JSOnContainer')
let copyBtn = document.querySelector('#copyBtn')
let copyJson = '12'
copyBtn.addEventListener('click', () => {
try {
navigator.clipboard.writeText(copyJson)
} catch (error) {
alert('复制失败!请手动复制')
}
})
fileInput.addEventListener('change', () => {
run(fileInput.files[0])
})
const run = async (file) => {
/* replace with the URL of the file */
const ab = await file.arrayBuffer();
/* Parse file and get first worksheet */
const wb = XLSX.read(ab);
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* Generate HTML */
var output = document.getElementById("TableContainer");
output.innerHTML = XLSX.utils.sheet_to_html(ws);
const json = toJson(ws)
JSOnContainer.innerHTML = json
copyJson = json
navigator.clipboard.writeText(json)
};
const toJson = (sheet) => {
let code_key = 0
let random_key = Math.random().toString(16).substring(3, 7) + '_'
// 将工作表转换为JSON
let data = XLSX.utils.sheet_to_json(sheet);
const getLangStr = (str = '') => /[a-z]+/.exec(str)[0];
data = data.map((v) => {
let temp = {};
Object.keys(v).forEach((k) => {
temp[getLangStr(k)] = (v[k] + '').replace(/(\r)|(\n)/g, '');
});
if (temp.code == undefined) {
temp.code = temp.zh || ('code_' + random_key + (code_key++));
}
return temp;
});
let temp = {};
let lang = Object.keys(data[0]).filter((v) => v != 'code');
lang.forEach((k) => (temp[k] = {}));
data.forEach((item) => {
lang.forEach((l) => {
temp[l][item.code] = item[l];
});
});
return JSON.stringify(temp, null, 2)
}
</script>
</body>
</html>