forked from elwerene/Node-Excel-Export
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
168 lines (138 loc) · 10.6 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
'use strict';
const fs = require('fs');
const temp = require('temp').track();
const path = require('path');
const JSZip = require('jszip');
const async = require('async');
const Sheet = require('./Sheet');
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);
};
const templateXLSX = new Buffer('UEsDBBQAAgAIABN7eUK9Z10uNwEAADUEAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbK2US04DMQyGrzLKFk1SWCCEOu0C2EIluECUeDpR81LslvZsLDgSV8CdQQUhRCntJlFi+//+PN9eXsfTdfDVCgq6FBtxLkeigmiSdXHeiCW19ZWYTsZPmwxYcWrERnRE+VopNB0EjTJliBxpUwmaeFjmKmuz0HNQF6PRpTIpEkSqaashJuNbaPXSU3W35ukBy+WiuhnytqhG6Jy9M5o4rFbRfoPUqW2dAZvMMnCJxFxAW+wAKHjZ9zJoF896YfUjs4DHw6Afq5Jc2edg5zL+hsgYrMn/g5hUoM6Fo4UcfGIe+KyKs1DNdKF7HVhR8T7MOBMVa8tj9xK2/i3Yv8LXXmGnC9hHKnxpUJ76ML9o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y8KACAAAAAAAAEAGADwJaggC+jRAfAlqCAL6NEBAFWyCX/n0QFQSwECFAAUAAIACAATe3lC717fXmEBAAA9AwAAEAAkAAAAAAABACAAAACzAQAAZG9jUHJvcHMvYXBwLnhtbAoAIAAAAAAAAQAYAADf/WOOKc4BAN/9Y44pzgEA3/1jjinOAVBLAQIUAAoAAAAAAORL+0gAAAAAAAAAAAAAAAADACQAAAAAAAAAEAAAAEIDAAB4bC8KACAAAAAAAAEAGADweK8gC+jRAfB4ryAL6NEBAILjCn/n0QFQSwECFAAKAAAAAADFhXlCAAAAAAAAAAAAAAAACQAkAAAAAAAAABAAAABjAwAAeGwvX3JlbHMvCgAgAAAAAAABABgAANXZx5kpzgEA1dnHmSnOAQDV2ceZKc4BUEsBAhQACgAAAAAA5Ev7SAAAAAAAAAAAAAAAAAkAJAAAAAAAAAAQAAAAigMAAHhsL3RoZW1lLwoAIAAAAAAAAQAYAHBAriAL6NEBcECuIAvo0QEAVbIJf+fRAVBLAQIUABQAAgAIABN7eUJ1sZFepQUAALsbAAASACQAAAAAAAEAIAAAALEDAAB4bC90aGVtZS90aGVtZS54bWwKACAAAAAAAAEAGAAA3/1jjinOAQDf/WOOKc4BAN/9Y44pzgFQSwECFAAKAAAAAADlimVEAAAAAAAAAAAAAAAADgAkAAAAAAAAABAAAACGCQAAeGwvd29ya3NoZWV0cy8KACAAAAAAAAEAGAAAs5YbuTjPAQCzlhu5OM8BALOWG7k4zwFQSwUGAAAAAAkACQBJAwAAsgkAAAAA', 'base64');
const sheetsFront = '<?xml version="1.0" encoding="utf-8"?><x:workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:workbookPr codeName="ThisWorkbook" /><x:bookViews><x:workbookView firstSheet="0" activeTab="0" /></x:bookViews><x:sheets>';
const sheetsBack = '</x:sheets><x:definedNames /><x:calcPr calcId="125725" /></x:workbook>';
let sharedStringsFront = '<?xml version="1.0" encoding="UTF-8"?><x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" uniqueCount="$count" count="$count">';
const sharedStringsBack = '</x:sst>';
const relFront = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">';
const relBack = '<Relationship Id="rId101" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/><Relationship Id="rId100" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId102" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme.xml"/></Relationships>';
exports.execute = (configs, callback) => {
let p, files = [], styleIndex, k = 0, cn = 1, dirPath, shareStrings = [], convertedShareStrings = '', sheet, sheetPos = 0;
let zip = new JSZip();
if (!Array.isArray(configs)) {
configs = [configs];
}
let makeTemporaryFolder = (callback) => {
return temp.mkdir('xlsx', (err, dir) => {
if (err) {
return callback(err);
}
dirPath = dir;
return callback();
});
};
let addStyleSheet = (callback) => {
let configWithStylesheet = configs.find((config) => config.stylesXmlFile != null);
if (configWithStylesheet != null) {
let readStream = fs.createReadStream(configWithStylesheet.stylesXmlFile, {
encoding: 'utf8'
});
zip.file('xl/styles.xml', readStream);
}
callback();
};
let generateRel = (callback) => {
let workbook = relFront;
let i = 1;
configs.forEach( function(config) {
workbook += '<Relationship Id="rId' + (i + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + i + '.xml"/>';
i++;
});
workbook += relBack;
return async.parallel([
(callback) => {
zip.file('xl/_rels/workbook.xml.rels', workbook);
callback();
},
(callback) => {
let rels = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
+ '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
+ '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>'
+ '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>';
zip.file('_rels/.rels', rels, workbook);
callback();
}
], () => {
callback();
});
};
let generateWorkbook = (callback) => {
let workbook = sheetsFront;
configs.forEach( (config, i) => {
let name = config.name == null ? 'Sheet ' + (i + 1) : config.name;
workbook += '<x:sheet name="' + name + '" sheetId="' + (i + 2) +'" r:id="rId' + (i + 2) + '"/>';
});
workbook += sheetsBack;
zip.file('xl/workbook.xml', workbook);
callback();
}
let writeSharedString = (callback) => {
if (shareStrings.length === 0) {
return callback();
}
sharedStringsFront = sharedStringsFront.replace(/\$count/g, shareStrings.length);
zip.file('xl/sharedStrings.xml', sharedStringsFront + convertedShareStrings + sharedStringsBack);
callback();
};
let zipFile = (err) => {
if (err) {
return callback(err);
}
files.forEach((file) => {
let fileName = path.relative(dirPath, file);
zip.file('xl/worksheets/' + fileName, fs.createReadStream(file));
});
zip.loadAsync(templateXLSX)
.then((zip) => {
zip
.generateNodeStream({streamFiles:true})
.pipe(fs.createWriteStream(dirPath + '.xlsx'))
.on('finish', () => {
temp.cleanup();
return callback(null, dirPath + '.xlsx')
});
});
};
let finalizeZip = () => {
return async.waterfall([
writeSharedString
], () => {
zipFile();
});
};
let writeSheets = (callback) => {
async.eachOfSeries(configs, (config, i, callback) => {
let p = path.join(dirPath, 'sheet' + (i + 1) + '.xml');
files.push(p);
Sheet(p, config, shareStrings, convertedShareStrings)
.then((ammendedSharedStrings) => {
convertedShareStrings = ammendedSharedStrings.convertedShareStrings;
shareStrings = ammendedSharedStrings.shareStrings;
callback();
});
}, callback);
};
async.parallel([
addStyleSheet,
generateRel,
generateWorkbook,
(callback) => {
async.waterfall([
makeTemporaryFolder,
writeSheets
], callback);
}
], finalizeZip);
};