-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (55 loc) · 1.63 KB
/
Copy pathindex.js
File metadata and controls
62 lines (55 loc) · 1.63 KB
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
(() => {
const fixdata = (data) => {
var o = '',
l = 0,
w = 10240
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w,
l * w + w)))
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
return o
}
// ワークブックのデータをjsonに変換
const to_json = (workbook) => {
var result = {}
workbook.SheetNames.forEach(function (sheetName) {
var roa = XLSX.utils.sheet_to_json(
workbook.Sheets[sheetName],
{
header: ['name_ja', 'name', 'yomi', 'roma', 'keyword'],
})
if (roa.length > 0) {
result[sheetName] = roa
}
})
return result
}
const readFile = () => {
const fileInput = document.getElementById('file')
const file = fileInput.files[0]
if (!file) {
alert('ファイルがありません')
return
}
var reader = new FileReader()
reader.onload = function (e) {
var data = e.target.result
var wb
var arr = fixdata(data)
wb = XLSX.read(btoa(arr), {
type: 'base64',
cellDates: true,
})
const d = Object.values(to_json(wb))[0]
const nd = d.slice(1)
const blob = new Blob([JSON.stringify(nd, null, ' ')], {type: 'application\/json'})
const url = URL.createObjectURL(blob)
document.getElementById('link').href = url
document.getElementById('total').textContent = nd.length
}
reader.readAsArrayBuffer(file)
}
const startButton = document.getElementById('start')
startButton.addEventListener('click', () => {
readFile()
})
})()