-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcelGenerator.js
More file actions
111 lines (94 loc) · 3.42 KB
/
Copy pathexcelGenerator.js
File metadata and controls
111 lines (94 loc) · 3.42 KB
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
/*
* @author: Rafael Bellotti
* @version: 1.1
* @last_update: 19/09/2017 - 16:06
* @changelog: Definição de opções da planilha
*/
/*
*Pattern type value must be one of darkDown, darkGray, darkGrid, darkHorizontal, darkTrellis, darkUp, darkVerical, gray0625, gray125, lightDown, lightGray, *lightGrid, lightHorizontal, lightTrellis, lightUp, lightVertical, mediumGray, none, solid
*/
var Excel4Node = require('excel4node');
function Excel()
{
//Initializes the workbook
var workbook = new Excel4Node.Workbook();
var defaultDataStyle = {
font: {
color: "#000000",
size: 12
},
numberFormat: '$#,##0.00; ($#,##0.00); -',
alignment: {
shrinkToFit: true,
wrapText: true,
vertical: 'center',
horizontal: 'left'
}
};
var defaultHeaderStyle = {
font: {
color: "#000000",
size: 13
},
numberFormat: '$#,##0.00; ($#,##0.00); -',
alignment: {
shrinkToFit: true,
wrapText: true,
vertical: 'center',
horizontal: 'left'
},
fill: {
type: 'pattern',
patternType: 'solid',
fgColor: "#d4d4db"
}
};
var defaultOptions = {};
//Adds a worksheet to the created workbook
//Data is added can only be written as string
this.addSheet = function(_configuration)
{
var worksheetOptions = (typeof _configuration.worksheetOptions != 'undefined') ? _configuration.worksheetOptions : defaultOptions;
var worksheet = workbook.addWorksheet(_configuration.worksheetName, worksheetOptions);
var headerStyle = (typeof _configuration.headerStyle != 'undefined') ? workbook.createStyle(_configuration.headerStyle) : workbook.createStyle(defaultHeaderStyle);
var dataStyle = (typeof _configuration.dataStyle != 'undefined') ? workbook.createStyle(_configuration.dataStyle) : workbook.createStyle(defaultDataStyle);
//Writes the column names on the Excel sheet
for (var i = 0; i < _configuration.columnNames.length; i++)
{
worksheet.cell(1,i + 1).string(_configuration.columnNames[i]).style(headerStyle);
}
//Writes the data on the sheet
for (var i = 0; i < _configuration.data.length; i++)
{
var j = 1;
for (var key in _configuration.data[i])
{
var value = _configuration.data[i][key];
if (_configuration.data[i].hasOwnProperty(key))
{
value = _configuration.data[i][key];
}
worksheet.cell(i + 2,j).string(value).style(dataStyle);
j++;
}
}
};
//Saves the workbook with the file name as specified (extension is not to be provided in the parameter)
this.saveFile = function(fileName,onSuccess)
{
var newFileName = fileName + ".xlsx";
workbook.write(newFileName , function (error, stats)
{
if (error)
{
console.log("Error: " + error);
}
else
{
console.log("Excel file " + newFileName + " created.");
onSuccess();
}
});
};
}
module.exports = Excel;