forked from exceljs/exceljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSax2.js
91 lines (86 loc) · 1.83 KB
/
testSax2.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
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
const events = require('events');
const Sax = require('sax');
const utils = require('./utils/utils');
const Row = function(r) {
this.number = r;
this.cells = {};
};
Row.prototype = {
add(cell) {
this.cells[cell.address] = cell;
},
};
const parser = Sax.createStream(true, {});
let target = 0;
let count = 0;
const e = new events.EventEmitter();
e.on('drain', () => {
setImmediate(() => {
const a = [];
for (let i = 0; i < 1000; i++) {
a.push(`<row r="${target++}">`);
for (let j = 0; j < 10; j++) {
a.push(`<cell c="${j}">${utils.randomNum(10000)}</cell>`);
}
a.push('</row>');
}
const buf = Buffer.from(a.join(''));
parser.write(buf);
});
});
e.on('row', (/*row*/) => {
if (++count % 1000 === 0) {
process.stdout.write(`Count:${count}, ${target}\u001b[0G`); // "\033[0G"
}
if (target - count < 100) {
e.emit('drain');
}
});
e.on('finished', () => {
console.log(`Finished worksheet: ${count}`);
});
let row = null;
let cell = null;
parser.on('opentag', node => {
// console.log('opentag ' + node.name);
switch (node.name) {
case 'row': {
const r = parseInt(node.attributes.r, 10);
row = new Row(r);
break;
}
case 'cell': {
const c = parseInt(node.attributes.c, 10);
cell = {
c,
value: '',
};
break;
}
default:
}
});
parser.on('text', text => {
// console.log('text ' + text);
if (cell) {
cell.value += text;
}
});
parser.on('closetag', name => {
// console.log('closetag ' + name);
switch (name) {
case 'row':
e.emit('row', row);
row = null;
break;
case 'cell':
row.add(cell);
break;
default:
}
});
parser.on('end', () => {
e.emit('finished');
});
parser.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root>');
e.emit('drain');