-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (37 loc) · 1.08 KB
/
Copy pathindex.js
File metadata and controls
49 lines (37 loc) · 1.08 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
'use strict'
const xlsx = require('xlsx')
const duplexify = require('duplexify')
const Writable = require('readable-stream').Writable
module.exports = function (options) {
options = options || {}
const chunks = []
const maxSize = options.maxSize || 0
let size = 0
const duplex = duplexify.obj(new Writable({
write (chunk, enc, next) {
size += Buffer.byteLength(chunk)
if (maxSize > 0 && size > maxSize) {
return next(new Error('Maximum size exceeded: ' + maxSize))
}
chunks.push(chunk)
next()
},
final (callback) {
const buf = Buffer.concat(chunks, size)
let workbook
try {
workbook = xlsx.read(buf, { type: 'buffer' })
} catch (err) {
return callback(err)
}
const name = options.sheet || workbook.SheetNames[options.sheetIndex || 0]
const sheet = workbook.Sheets[name]
if (!sheet) {
return callback(new Error('Sheet not found'))
}
duplex.setReadable(xlsx.stream.to_json(sheet, { raw: true, defval: null }))
callback()
}
}))
return duplex
}