-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexcel.js
45 lines (37 loc) · 999 Bytes
/
excel.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
Excel = {};
Excel.lib = Npm.require('excel-export');
Excel.getColumns = function(fields, data) {
var rows = [];
_.each(data, function(item) {
var row = [];
_.each(fields, function(field, index) {
var value = searchObject(item, field.key) || null;
value = _.isFunction(field.transform) ? field.transform(value, item) : value;
row[index] = value
});
rows.push(row);
});
return rows;
};
Excel.export = function(title, fields, data) {
check(title, String);
check(fields, [{
key: String,
title: String,
type: Match.Optional(String),
width: Match.Optional(Number),
transform: Match.Optional(Function)
}]);
check(data, [Match.Any]);
var rows = this.getColumns(fields, data);
var excel = {};
excel.cols = fields.map(function(field) {
return {
caption: field.title,
type: field.type || 'string',
width: field.width || 28.7109375
};
});
excel.rows = rows;
return Excel.lib.execute(excel);
}