forked from darkdown/export-excel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcelUtil.js
36 lines (34 loc) · 961 Bytes
/
excelUtil.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
const excelUtil = {
loading: false,
excelReader: null,
fileHandler: [],
getExcelReader() {
if (this.excelReader === null) {
this.excelReader = new FileReader()
this.excelReader.onload = (e) => {
this.fileHandler = e.target.result
}
}
return this.excelReader
},
readAsBinaryString(file, callback = () => {}) {
this.getExcelReader().readAsBinaryString(file)
setTimeout(() => {
if (this.fileHandler) {
callback()
}
}, 500)
}
}
function excel2Json(file, callback = () => {}) {
excelUtil.loading = true
excelUtil.readAsBinaryString(file, () => {
const jsonFromExecl = require('./Excel2Json').jsonFromExecl
let jsonData = jsonFromExecl(excelUtil.fileHandler, {type: 'binary'})
callback(jsonData)
excelUtil.loading = false
})
}
export {
excel2Json
}