forked from functionscope/Node-Excel-Export
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
279 lines (279 loc) · 19 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var fs = require('fs');
var temp = require('temp').track();
var path = require('path');
var JSZip = require('jszip');
var async = require('async');
Date.prototype.getJulian = function () {
return Math.floor(this / 86400000 - this.getTimezoneOffset() / 1440 + 2440587.5);
};
Date.prototype.oaDate = function () {
return (this - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
};
var templateXLSX = new Buffer('UEsDBBQAAAAIABN7eUK9Z10uOQEAADUEAAATABwAW0NvbnRlbnRfVHlwZXNdLnhtbFVUCQADJl5QUSZeUFF1eAsAAQT1AQAABBQAAACtlEtOAzEMhq8yyhZNUlgghDrtAthCJbhAlHg6UfNS7Jb2bCw4ElfAnUEFVYgC7SZRYvv//jzfXl7H03Xw1QoKuhQbcS5HooJoknVx3ogltfWVmE7GT5sM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jvb2sueG1sVVQFAAMmXlBRdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAA5YplRAAAAAAAAAAAAAAAAA4AGAAAAAAAAAAQAO1BJBEAAHhsL3dvcmtzaGVldHMvVVQFAANuTxdTdXgLAAEE9QEAAAQUAAAAUEsFBgAAAAARABEA7wUAAGwRAAAAAA==', 'base64');
var sheetFront = '<?xml version="1.0" encoding="utf-8"?><x:worksheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><x:sheetPr><x:outlinePr summaryBelow="1" summaryRight="1" /></x:sheetPr><x:sheetViews><x:sheetView tabSelected="0" workbookViewId="0" /></x:sheetViews><x:sheetFormatPr defaultRowHeight="15" /><cols>';
var sheetBack = '</x:sheetData><x:printOptions horizontalCentered="0" verticalCentered="0" headings="0" gridLines="0" /><x:pageMargins left="0.75" right="0.75" top="0.75" bottom="0.5" header="0.5" footer="0.75" /><x:pageSetup paperSize="1" scale="100" pageOrder="downThenOver" orientation="default" blackAndWhite="0" draft="0" cellComments="none" errors="displayed" /><x:headerFooter /><x:tableParts count="0" /></x:worksheet>';
var sharedStringsFront = '<?xml version="1.0" encoding="UTF-8"?><x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" uniqueCount="$count" count="$count">';
var sharedStringsBack = '</x:sst>';
exports.execute = function (config, callback) {
var cols = config.cols, data = config.rows, colsLength = cols.length, p, files = [], styleIndex, k = 0, cn = 1, dirPath, shareStrings = [], shareStringsObj = {}, convertedShareStrings = '', sheet, sheetPos = 0;
var write = function (str, callback) {
var buf = new Buffer(str);
var off = 0;
var written = 0;
return async.whilst(function () {
return written < buf.length;
}, function (callback) {
fs.write(sheet, buf, off, buf.length - off, sheetPos, function (err, w) {
if (err) {
return callback(err);
}
written += w;
off += w;
sheetPos += w;
return callback();
});
}, callback);
};
return async.waterfall([
function (callback) {
return temp.mkdir('xlsx', function (err, dir) {
if (err) {
return callback(err);
}
dirPath = dir;
return callback();
});
},
function (callback) {
return fs.mkdir(path.join(dirPath, 'xl'), callback);
},
function (callback) {
return fs.mkdir(path.join(dirPath, 'xl', 'worksheets'), callback);
},
function (callback) {
return async.parallel([
function (callback) {
return fs.writeFile(path.join(dirPath, 'data.zip'), templateXLSX, callback);
},
function (callback) {
if (!config.stylesXmlFile) {
return callback();
}
p = config.stylesXmlFile || __dirname + '/styles.xml';
return fs.readFile(p, 'utf8', function (err, styles) {
if (err) {
return callback(err);
}
p = path.join(dirPath, 'xl', 'styles.xml');
files.push(p);
return fs.writeFile(p, styles, callback);
});
}
], function (err) {
return callback(err);
});
},
function (callback) {
p = path.join(dirPath, 'xl', 'worksheets', 'sheet.xml');
files.push(p);
return fs.open(p, 'a+', function (err, fd) {
if (err) {
return callback(err);
}
sheet = fd;
return callback();
});
},
function (callback) {
return write(sheetFront, callback);
},
function (callback) {
return async.eachSeries(cols, function (col, callback) {
var colStyleIndex = col.styleIndex || 0;
var res = '<x:col min="' + cn + '" max="' + cn + '" width="' + (col.width ? col.width : 10) + '" customWidth="1" style="' + colStyleIndex + '"/>';
cn++;
return write(res, callback);
}, callback);
},
function (callback) {
return write('</cols><x:sheetData>', callback);
},
function (callback) {
return write('<x:row r="1" spans="1:' + colsLength + '">', callback);
},
function (callback) {
return async.eachSeries(cols, function (col, callback) {
var colStyleIndex = col.captionStyleIndex || 0;
var res = addStringCol(getColumnLetter(k + 1) + 1, col.caption, colStyleIndex, shareStrings, shareStringsObj);
k++;
if (res[1] !== null) {
convertedShareStrings += res[1];
}
return write(res[0], callback);
}, callback);
},
function (callback) {
return write('</x:row>', callback);
},
function (callback) {
var j, r, cellData, currRow, cellType;
var i = -1;
data.reverse();
return async.whilst(function () {
return data.length > 0;
}, function (callback) {
i++;
r = data.pop();
currRow = i + 2;
var row = '<x:row r="' + currRow + '" spans="1:' + colsLength + '">';
for (j = 0; j < colsLength; j++) {
styleIndex = cols[j].styleIndex;
cellData = r[j];
cellType = cols[j].type;
if (typeof cols[j].beforeCellWrite === 'function') {
var e = {
rowNum: currRow,
styleIndex: styleIndex,
cellType: cellType
};
cellData = cols[j].beforeCellWrite(r, cellData, e);
styleIndex = e.styleIndex || styleIndex;
cellType = e.cellType;
e = undefined;
}
switch (cellType) {
case 'number':
row += addNumberCol(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
case 'date':
row += addDateCol(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
case 'bool':
row += addBoolCol(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
default:
var res = addStringCol(getColumnLetter(j + 1) + currRow, cellData, styleIndex, shareStrings, shareStringsObj);
row += res[0];
convertedShareStrings += res[1];
}
}
row += '</x:row>';
return write(row, callback);
}, callback);
},
function (callback) {
return write(sheetBack, callback);
},
function (callback) {
return fs.close(sheet, callback);
},
function (callback) {
if (shareStrings.length === 0) {
return callback();
}
sharedStringsFront = sharedStringsFront.replace(/\$count/g, shareStrings.length);
p = path.join(dirPath, 'xl', 'sharedStrings.xml');
files.push(p);
return fs.writeFile(p, sharedStringsFront + convertedShareStrings + sharedStringsBack, callback);
}
], function (err) {
if (err) {
return callback(err);
}
var prev = fs.readFileSync(path.join(dirPath, 'data.zip'));
var zip = new JSZip(prev);
files.forEach(function (file) {
var relative = path.relative(dirPath, file);
zip.file(relative, fs.readFileSync(file));
});
var data = zip.generate({
mimeType: 'application/zip',
type: 'nodebuffer'
});
temp.cleanup();
return callback(null, data);
});
};
var startTag = function (obj, tagName, closed) {
var result = '<' + tagName, p;
for (p in obj) {
result += ' ' + p + '=' + obj[p];
}
if (!closed) {
result += '>';
} else {
result += '/>';
}
return result;
};
var endTag = function (tagName) {
return '</' + tagName + '>';
};
var addNumberCol = function (cellRef, value, styleIndex) {
styleIndex = styleIndex || 0;
if (value === null) {
return '';
} else {
return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="n"><x:v>' + value + '</x:v></x:c>';
}
};
var addDateCol = function (cellRef, value, styleIndex) {
styleIndex = styleIndex || 1;
if (value === null) {
return '';
} else {
return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="n"><x:v>' + value + '</x:v></x:c>';
}
};
var addBoolCol = function (cellRef, value, styleIndex) {
styleIndex = styleIndex || 0;
if (value === null) {
return '';
}
if (value) {
value = 1;
} else {
value = 0;
}
return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="b"><x:v>' + value + '</x:v></x:c>';
};
var addStringCol = function (cellRef, value, styleIndex, shareStrings, shareStringsObj) {
styleIndex = styleIndex || 0;
if (value === null) {
return [
'',
null
];
}
if (typeof value === 'string') {
value = value.replace(/&/g, '&').replace(/'/g, ''').replace(/>/g, '>').replace(/</g, '<');
}
var convertedShareStrings = null;
var i = shareStringsObj[value];
if (i === undefined) {
i = shareStrings.push(value) - 1;
shareStringsObj[value] = i;
convertedShareStrings = '<x:si><x:t>' + value + '</x:t></x:si>';
}
return [
'<x:c r="' + cellRef + '" s="' + styleIndex + '" t="s"><x:v>' + i + '</x:v></x:c>',
convertedShareStrings
];
};
var getColumnLetter = function (col) {
if (col <= 0) {
throw 'col must be more than 0';
}
var array = [];
while (col > 0) {
var remainder = col % 26;
col /= 26;
col = Math.floor(col);
if (remainder === 0) {
remainder = 26;
col--;
}
array.push(64 + remainder);
}
return String.fromCharCode.apply(null, array.reverse());
};