xlsx-service.js
887 Bytes
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
/**
* Created by TaoHuang on 2017/5/17.
*/
const Context = require('../framework/context');
const xlsx2json = require('xlsx2json');
const Api = require('../common/api');
const _ = require('lodash');
class XlsxService extends Context {
constructor() {
super();
this.api = this.instance(Api);
}
resolveExcel(path, cols) {
return xlsx2json(path).then(excelData => {
const sheetData = _.first(excelData);
const header = sheetData[0];
const rows = _.slice(sheetData, 1);
const columns = _.map(cols, (v, k) => {
return {
key: k,
col: _.findKey(header, hv => _.trim(hv) === v),
};
});
return _.map(rows, row => {
const item = {};
_.each(columns, col => {
item[col.key] = row[col.col];
});
return item;
});
});
}
}
module.exports = XlsxService;