/
xlsxreader.js
206 lines (169 loc) · 6.01 KB
/
xlsxreader.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
var EventEmitter = process.EventEmitter,
sheeter_utils= require('../utils/utils'),
Zip = require('adm-zip'),
sax = require('sax'),
fs = require('fs'),
Cell = require('../cell/cell').Cell,
ContentReader = require('./xlsx/content').ContentReader,
RelationReader = require('./xlsx/rels').RelationReader,
MainReader = require('./xlsx/main').MainReader,
StringReader = require('./xlsx/strings').StringReader,
SheetReader = require('./xlsx/sheet').SheetReader;
var XLSXReader = function (config){
this._filename;
this._workbook;
this._cellDefault = {};
if (typeof config==='undefined'){
config = {};
}
if (typeof config.filename!=='undefined'){
this.filename = config.filename;
}
if (typeof config.workbook!=='undefined'){
this.workbook = config.workbook;
}
if (typeof config.cellDefault!=='undefined'){
this._cellDefault = config.cellDefault;
}
this._entrieIndex = -1;
this._readingOrderIndex = -1;
this._readRequiredStopped=true;
this.zipEntries;
this._contentReader = new ContentReader();
this._relationReader = new RelationReader();
this._mainReader = new MainReader();
this._stringReader = new StringReader();
this._intend = 0;
/*
this._mainReader.on('sheet',this.onSheetFound.bind(this));
this._mainReader.on('ready',this.onMainReady.bind(this));
this._contentReader.on('ready',this.onContentReady.bind(this));
this._relationReader.on('ready',this.onRelationsReady.bind(this));
*/
return this;
}
sheeter_utils.inherits(XLSXReader,EventEmitter,{
get filename () { return this._filename; },
set filename (fn) { this._filename=fn; return this; },
get workbook () { return this._workbook; },
set workbook (fn) { this._workbook=fn; return this; },
get cellDefault() { return this._cellDefault; },
set cellDefault (fn) { this._cellDefault=fn; return this; },
get contentReader() { return this._contentReader; },
get relationReader() { return this._relationReader; },
get mainReader() { return this._mainReader; },
get stringReader() { return this._stringReader; }
});
XLSXReader.prototype.open = function (config,cb){
var zip,
data,
zipEntries,
i,
m;
if ( (typeof config !== 'undefined') && (typeof config.filename!=='undefined')){
this.filename = config.filename;
}
zip = new Zip(this.filename);
this.zipEntries = zip.getEntries();
this._openCallback = cb;
this.contentReader.data = this.getEntry('[Content_Types].xml').toString();
this.contentReader.on('ready',this.onContentReady.bind(this));
this.contentReader.parse();
}
// `getEntry(filename)` returns the decompressed data Buffer of the filename.
// If the file can not be found an error will be thrown.
XLSXReader.prototype.getEntry=function(filename){
var i,
m;
for(i=0,m=this.zipEntries.length;i<m;i+=1){
if (this.zipEntries[i].entryName == filename.replace(/^\//,'')){
return this.zipEntries[i].getData();
}
}
throw new Error(filename+' not found');
}
XLSXReader.prototype.onContentReady = function(){
//console.log('content ready');
//console.log(this.contentReader);//.strings[0]
if (this.contentReader.strings>0){
this.stringReader.data = this.getEntry(this.contentReader.strings[0]).toString();
this.stringReader.on('ready',this.onStringReady.bind(this));
this.stringReader.parse();
}else{
this.onStringReady();
}
}
XLSXReader.prototype.onStringReady = function(){
//console.log('string ready');
this.relationReader.data = this.getEntry('xl/_rels/workbook.xml.rels').toString();
this.relationReader.on('ready',this.onRelationsReady.bind(this));
this.relationReader.parse();
}
XLSXReader.prototype.onRelationsReady = function(){
//console.log('rel ready');
this.mainReader.data = this.getEntry(this.contentReader.main).toString();
this.mainReader.on('ready',this.onMainReady.bind(this));
this.mainReader.on('sheet',this.onSheetFound.bind(this));
this.mainReader.parse();
}
XLSXReader.prototype.onMainReady = function(){
var me = this;
me.emit('mainReady');
}
XLSXReader.prototype.onSheetReady = function(){
var me = this;
me.emit('sheetsReady');
}
XLSXReader.prototype.onSheetFound = function(title,index,rid){
var sheetreader = new SheetReader(),
me = this,
config,
cx,
sheet;
if (typeof me.workbook!='undefined'){
sheet = me.workbook.createWorkSheet({
id: index,
title: title,
cellDefault: me.cellDefault
});
};
me._intend += 1;
sheetreader.on('ready',me.onSheetReady.bind(me));
sheetreader.on('cell',function(c){
if ((typeof c.t != 'undefined') && (c.t=='s')){
c.value = me.stringReader.string(c.value);
}
if (typeof sheet!='undefined'){
var cell;
//console.log(c);
cell = new Cell(c.r,me.cellDefault);
cell._id = c.r;
cell._worksheet = sheet;
cell._value = c.value;
if (typeof c.formula != 'undefined'){
cell.formula = c.formula;
}
sheet._cellHash[c.r] = cell; /*not nice, but it speed's it up */
if (typeof c.formula == 'undefined'){
cell._value = c.value;
}
/*
config = {
id: c.r,
value: c.value
};
if (typeof c.formula != 'undefined'){
config.formula = c.formula;
}
cx = sheet.getCell(config.id,config);
if (typeof c.formula == 'undefined'){
cx.value = c.value;
}
*/
}
});
sheetreader.data = me.getEntry('xl/'+me.relationReader.relation(rid)).toString();
sheetreader.parse();
}
exports.XLSXReader = XLSXReader;