forked from functionscope/Node-Excel-Export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.js
175 lines (159 loc) · 4.87 KB
/
sheet.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
169
170
171
172
173
174
175
var sheetFront = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><x:worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
+ ' <x:sheetPr/><x:sheetViews><x:sheetView tabSelected="1" workbookViewId="0" /></x:sheetViews>'
+ ' <x:sheetFormatPr defaultRowHeight="15" />';
var sheetBack =' <x:pageMargins left="0.75" right="0.75" top="0.75" bottom="0.5" header="0.5" footer="0.75" />'
+ ' <x:headerFooter /></x:worksheet>';
var fs = require('fs');
function Sheet(config, xlsx, shareStrings, convertedShareStrings){
this.config = config;
this.xlsx = xlsx;
this.shareStrings = shareStrings;
this.convertedShareStrings = convertedShareStrings;
}
Sheet.prototype.generate = function(){
var config = this.config, xlsx = this.xlsx;
var cols = config.cols,
data = config.rows,
colsLength = cols.length,
rows = "",
row = "",
colsWidth = "",
styleIndex,
self = this,
k;
config.fileName = 'xl/worksheets/' + (config.name || "sheet").replace(/[*?\]\[\/\/]/g, '') + '.xml';
if (config.stylesXmlFile) {
var path = config.stylesXmlFile;
var styles = null;
styles = fs.readFileSync(path, 'utf8');
if (styles) {
xlsx.file("xl/styles.xml", styles);
}
}
//first row for column caption
row = '<x:row r="1" spans="1:' + colsLength + '">';
var colStyleIndex;
for (k = 0; k < colsLength; k++) {
colStyleIndex = cols[k].captionStyleIndex || 0;
row += addStringCell(self, getColumnLetter(k + 1) + 1, cols[k].caption, colStyleIndex);
if (cols[k].width) {
colsWidth += '<col customWidth = "1" width="' + cols[k].width + '" max = "' + (k + 1) + '" min="' + (k + 1) + '"/>';
}
}
row += '</x:row>';
rows += row;
//fill in data
var i, j, r, cellData, currRow, cellType, dataLength = data.length;
for (i = 0; i < dataLength; i++) {
r = data[i],
currRow = i + 2;
row = '<x:row r="' + currRow + '" spans="1:' + colsLength + '">';
for (j = 0; j < colsLength; j++) {
styleIndex = null;
cellData = r[j];
cellType = cols[j].type;
if (typeof cols[j].beforeCellWrite === 'function') {
var e = {
rowNum: currRow,
styleIndex: null,
cellType: cellType
};
cellData = cols[j].beforeCellWrite(r, cellData, e);
styleIndex = e.styleIndex || styleIndex;
cellType = e.cellType;
delete e;
}
switch (cellType) {
case 'number':
row += addNumberCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
case 'date':
row += addDateCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
case 'bool':
row += addBoolCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex);
break;
default:
row += addStringCell(self, getColumnLetter(j + 1) + currRow, cellData, styleIndex);
}
}
row += '</x:row>';
rows += row;
}
if (colsWidth !== "") {
sheetFront += '<cols>' + colsWidth + '</cols>';
}
xlsx.file(config.fileName, sheetFront + '<x:sheetData>' + rows + '</x:sheetData>' + sheetBack);
}
module.exports = Sheet;
var startTag = function (obj, tagName, closed){
var result = "<" + tagName, p;
for (p in obj){
result += " " + p + "=" + obj[p];
}
if (!closed)
result += ">";
else
result += "/>";
return result;
};
var endTag = function(tagName){
return "</" + tagName + ">";
};
var addNumberCell = function(cellRef, value, styleIndex){
styleIndex = styleIndex || 0;
if (value===null)
return "";
else
return '<x:c r="'+cellRef+'" s="'+ styleIndex +'" t="n"><x:v>'+value+'</x:v></x:c>';
};
var addDateCell = function(cellRef, value, styleIndex){
styleIndex = styleIndex || 1;
if (value===null)
return "";
else
return '<x:c r="'+cellRef+'" s="'+ styleIndex +'" t="n"><x:v>'+value+'</x:v></x:c>';
};
var addBoolCell = function(cellRef, value, styleIndex){
styleIndex = styleIndex || 0;
if (value===null)
return "";
if (value){
value = 1;
} else
value = 0;
return '<x:c r="'+cellRef+'" s="'+ styleIndex + '" t="b"><x:v>'+value+'</x:v></x:c>';
};
var addStringCell = function(sheet, cellRef, value, styleIndex){
styleIndex = styleIndex || 0;
if (value===null)
return "";
if (typeof value ==='string'){
value = value.replace(/&/g, "&").replace(/'/g, "'").replace(/>/g, ">").replace(/</g, "<");
}
var i = sheet.shareStrings.get(value, -1);
if ( i< 0){
i = sheet.shareStrings.length;
sheet.shareStrings.add(value, i);
sheet.convertedShareStrings += "<x:si><x:t>"+value+"</x:t></x:si>";
}
return '<x:c r="'+cellRef+'" s="'+ styleIndex + '" t="s"><x:v>'+i+'</x:v></x:c>';
};
var getColumnLetter = function(col){
if (col <= 0)
throw "col must be more than 0";
var array = new Array();
while (col > 0)
{
var remainder = col % 26;
col /= 26;
col = Math.floor(col);
if(remainder ===0)
{
remainder = 26;
col--;
}
array.push(64 + remainder);
}
return String.fromCharCode.apply(null, array.reverse());
};