Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 1230b9b

Browse files
committed
test
0 parents  commit 1230b9b

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
yarn.lock

example.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const request = require('superagent')
2+
const through = require('through2')
3+
const stream = require('./')
4+
5+
const req = request.get('http://localhost:8000/file.xlsx')
6+
stream(req).on('data', (d) => console.log(String(d)))

index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Excel = require('exceljs')
2+
const through = require('through2')
3+
const Stream = require('stream')
4+
const readable = new Stream.Readable()
5+
6+
module.exports = (input) => {
7+
const workbook = new Excel.Workbook()
8+
readable._read = () => {
9+
let headers = null
10+
const reader = workbook.xlsx.read(input)
11+
.then((worksheet) => {
12+
workbook.eachSheet((sheet, id) => {
13+
sheet.eachRow((row, id) => {
14+
if (id === 1) {
15+
headers = row.values
16+
return
17+
}
18+
let item = {}
19+
row.values.forEach((v, k) => {
20+
item[headers[k]] = v
21+
})
22+
readable.push(JSON.stringify(item))
23+
})
24+
})
25+
readable.push(null)
26+
})
27+
}
28+
return readable
29+
}

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"dependencies": {
3+
"duplexer": "^0.1.1",
4+
"exceljs": "^0.2.41",
5+
"sheetjs": "^1.0.21",
6+
"through2": "^2.0.3",
7+
"unzip": "^0.1.11",
8+
"xlsx": "^0.8.2",
9+
"xml-stream": "^0.4.5"
10+
},
11+
"devDependencies": {
12+
"superagent": "^3.4.1"
13+
}
14+
}

parser.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const XmlStream = require('xml-stream')
2+
const unzip = require('unzip')
3+
const assert = require('assert')
4+
5+
const defaultOpts = {
6+
parser: {}
7+
}
8+
9+
const constants = {
10+
header: 't',
11+
rowPrefix: 'c'
12+
}
13+
14+
module.exports = function xlsx (opts) {
15+
opts = opts || defaultOpts
16+
let sharedStrings = []
17+
let rows = []
18+
return unzip.Parse()
19+
.on('entry', function (entry) {
20+
// let sheets
21+
// if (entry.path === 'xl/workbook.xml') {
22+
// const xml = new XmlStream(entry)
23+
// xml.collect('sheet')
24+
// xml.on('endElement: sheet', function (d) {
25+
// sheets = d['$']
26+
// })
27+
// xml.on('end', () => {
28+
// console.log(sheets)
29+
// })
30+
// }
31+
if (entry.path.match(/worksheets\/(.*).xml$/)) {
32+
const xml = new XmlStream(entry)
33+
xml.collect('row')
34+
xml.on('endElement: c', function (item) {
35+
rows.push(item)
36+
})
37+
}
38+
else if (entry.path.match(/sharedStrings/)) {
39+
const xml = new XmlStream(entry)
40+
xml.collect('sst')
41+
xml.on('updateElement: si', function (item) {
42+
sharedStrings.push(item[constants.header])
43+
})
44+
}
45+
else {
46+
entry.autodrain()
47+
}
48+
})
49+
.on('end', function () {
50+
console.log(sharedStrings)
51+
console.log(JSON.stringify(rows[0], null, 2))
52+
console.log(JSON.stringify(rows[4], null, 2))
53+
let results = {}
54+
let headers = {}
55+
rows.forEach(function (value, key) {
56+
if (!value['$']) return
57+
const item = {}
58+
// check if it is already partialy created
59+
// let insert = results[value['$'].r]
60+
// if (!insert) results[value['$'].r] = {}
61+
const c = value['$']
62+
const cv = parseInt(value.v)
63+
const id = c.r.substring(1, 10)
64+
if (id === '1') {
65+
headers[c.r] = { id: c.r.substring(0, 1), r: c.r, row: sharedStrings[cv] }
66+
return
67+
}
68+
if (!results[id]) {
69+
results[id] = {}
70+
}
71+
if (c && c.t) {
72+
if (sharedStrings[cv]) {
73+
item[headers[c.r.substring(0, 1) + '1'].row] = sharedStrings[cv]
74+
75+
}
76+
}
77+
results[c.r] = item
78+
})
79+
console.log(results)
80+
const expected = [{
81+
row: '1',
82+
date: '2/8/2017',
83+
cost: '100',
84+
notes: '111'
85+
}]
86+
// assert.deepEqual(expected, result)
87+
})
88+
}
89+
90+
91+
92+
93+
let o = {
94+
"c": {
95+
"v": "3",
96+
"$": {
97+
"r": "D1",
98+
"s": "1",
99+
"t": "s"
100+
}
101+
},
102+
"$": {
103+
"r": "1"
104+
}
105+
}

test/file.xlsx

4.99 KB
Binary file not shown.

test/file1.xlsx

3.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)