-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (67 loc) · 2.26 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
let xlsx = require("xlsx");
let XlsxBuilder = function () {
let workbook = xlsx.utils.book_new();
return {
XLSX: xlsx,
addJson: function (ws, json, config = {}) {
if (!config) config = {};
let { headerMap = {}, excludeHeaders = [] } = config;
let keyOperations = function (json, newKeys = {}, excludeHeaders = []) {
json.map((obj) => {
const keyValues = Object.keys(obj).map((key) => {
const newKey = newKeys[key] || key;
if (!excludeHeaders.includes(newKey)) return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
});
return json;
};
json = keyOperations(json, headerMap, excludeHeaders);
if (!config.origin) config.origin = -1;
return xlsx.utils.sheet_add_json(ws, json, config);
},
addAoa: function (ws, aoa, config = {}) {
if (!config) config = {};
if (!config.origin) config.origin = -1;
return xlsx.utils.sheet_add_aoa(ws, aoa, config);
},
addSheet: function (values, sheetName, config = {}) {
let ws = null;
for (let value of values) {
if (value.type == "json") {
ws = this.addJson(ws, value.data, value.options);
} else if (value.type == "columns") {
ws = this.addAoa(ws, value.data, value.options);
}
}
if (config) {
if (Object.keys(config).length) {
for (let key of Object.keys(config)) {
ws[key] = config[key];
}
}
}
xlsx.utils.book_append_sheet(workbook, ws, sheetName);
return this;
},
build: function () {
return workbook;
},
export: function (options) {
return xlsx.write(workbook, options);
},
};
};
XlsxBuilder.middleware = function (req, res, next) {
res.xlsx = function (fn, data, config) {
if (!config) config = { type: "buffer", bookType: "xlsx" };
if (!config.type) config.type = "buffer";
if (!config.bookType) config.bookType = "xlsx";
let xlsxWb = new XlsxBuilder().addSheet(data).export(config);
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.setHeader("Content-Disposition", "attachment; filename=" + fn);
res.end(xlsxWb, "binary");
};
next();
};
module.exports = XlsxBuilder;