|
1 |
| -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">' |
2 |
| - + ' <x:sheetPr/><x:sheetViews><x:sheetView tabSelected="1" workbookViewId="0" /></x:sheetViews>' |
3 |
| - + ' <x:sheetFormatPr defaultRowHeight="15" />'; |
4 |
| -var sheetBack =' <x:pageMargins left="0.75" right="0.75" top="0.75" bottom="0.5" header="0.5" footer="0.75" />' |
5 |
| - + ' <x:headerFooter /></x:worksheet>'; |
6 |
| - |
7 |
| -var fs = require('fs'); |
| 1 | +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">' |
| 2 | + + ' <x:sheetPr/><x:sheetViews><x:sheetView tabSelected="1" workbookViewId="0" /></x:sheetViews>' |
| 3 | + + ' <x:sheetFormatPr defaultRowHeight="15" />' |
| 4 | +var sheetBack = ' <x:pageMargins left="0.75" right="0.75" top="0.75" bottom="0.5" header="0.5" footer="0.75" />' |
| 5 | + + ' <x:headerFooter /></x:worksheet>' |
8 | 6 |
|
9 |
| -function Sheet(config, xlsx, shareStrings, convertedShareStrings){ |
10 |
| - this.config = config; |
11 |
| - this.xlsx = xlsx; |
12 |
| - this.shareStrings = shareStrings; |
13 |
| - this.convertedShareStrings = convertedShareStrings; |
| 7 | +var fs = require('fs') |
| 8 | + |
| 9 | +function Sheet (config, xlsx, shareStrings, convertedShareStrings) { |
| 10 | + this.config = config |
| 11 | + this.xlsx = xlsx |
| 12 | + this.shareStrings = shareStrings |
| 13 | + this.convertedShareStrings = convertedShareStrings |
14 | 14 | }
|
15 | 15 |
|
16 |
| -Sheet.prototype.generate = function(){ |
17 |
| - var config = this.config, xlsx = this.xlsx; |
18 |
| - var cols = config.cols, |
19 |
| - data = config.rows, |
20 |
| - colsLength = cols.length, |
21 |
| - rows = "", |
22 |
| - row = "", |
23 |
| - colsWidth = "", |
24 |
| - styleIndex, |
25 |
| - self = this, |
26 |
| - k; |
27 |
| - config.fileName = 'xl/worksheets/' + (config.name || "sheet").replace(/[*?\]\[\/\/]/g, '') + '.xml'; |
28 |
| - if (config.stylesXmlFile) { |
29 |
| - var path = config.stylesXmlFile; |
30 |
| - var styles = null; |
31 |
| - styles = fs.readFileSync(path, 'utf8'); |
32 |
| - if (styles) { |
33 |
| - xlsx.file("xl/styles.xml", styles); |
34 |
| - } |
35 |
| - } |
| 16 | +Sheet.prototype.generate = function () { |
| 17 | + var config = this.config, xlsx = this.xlsx |
| 18 | + var cols = config.cols, |
| 19 | + data = config.rows, |
| 20 | + colsLength = cols.length, |
| 21 | + rows = '', |
| 22 | + row = '', |
| 23 | + colsWidth = '', |
| 24 | + styleIndex, |
| 25 | + self = this, |
| 26 | + k |
| 27 | + config.fileName = 'xl/worksheets/' + (config.name || 'sheet').replace(/[*?\]\[\/\/]/g, '') + '.xml' |
| 28 | + if (config.stylesXmlFile) { |
| 29 | + var path = config.stylesXmlFile |
| 30 | + var styles = null |
| 31 | + styles = fs.readFileSync(path, 'utf8') |
| 32 | + if (styles) { |
| 33 | + xlsx.file('xl/styles.xml', styles) |
| 34 | + } |
| 35 | + } |
36 | 36 |
|
37 |
| - //first row for column caption |
38 |
| - row = '<x:row r="1" spans="1:' + colsLength + '">'; |
39 |
| - var colStyleIndex; |
40 |
| - for (k = 0; k < colsLength; k++) { |
41 |
| - colStyleIndex = cols[k].captionStyleIndex || 0; |
42 |
| - row += addStringCell(self, getColumnLetter(k + 1) + 1, cols[k].caption, colStyleIndex); |
43 |
| - if (cols[k].width) { |
44 |
| - colsWidth += '<col customWidth = "1" width="' + cols[k].width + '" max = "' + (k + 1) + '" min="' + (k + 1) + '"/>'; |
45 |
| - } |
46 |
| - } |
47 |
| - row += '</x:row>'; |
48 |
| - rows += row; |
| 37 | + // first row for column caption |
| 38 | + row = '<x:row r="1" spans="1:' + colsLength + '">' |
| 39 | + var colStyleIndex |
| 40 | + for (k = 0; k < colsLength; k++) { |
| 41 | + colStyleIndex = cols[k].captionStyleIndex || 0 |
| 42 | + row += addStringCell(self, getColumnLetter(k + 1) + 1, cols[k].caption, colStyleIndex) |
| 43 | + if (cols[k].width) { |
| 44 | + colsWidth += '<col customWidth = "1" width="' + cols[k].width + '" max = "' + (k + 1) + '" min="' + (k + 1) + '"/>' |
| 45 | + } |
| 46 | + } |
| 47 | + row += '</x:row>' |
| 48 | + rows += row |
49 | 49 |
|
50 |
| - //fill in data |
51 |
| - var i, j, r, cellData, currRow, cellType, dataLength = data.length; |
| 50 | + // fill in data |
| 51 | + var i, j, r, cellData, currRow, cellType, dataLength = data.length |
52 | 52 |
|
53 |
| - for (i = 0; i < dataLength; i++) { |
54 |
| - r = data[i], |
55 |
| - currRow = i + 2; |
56 |
| - row = '<x:row r="' + currRow + '" spans="1:' + colsLength + '">'; |
57 |
| - for (j = 0; j < colsLength; j++) { |
58 |
| - styleIndex = null; |
59 |
| - cellData = r[j]; |
60 |
| - cellType = cols[j].type; |
61 |
| - if (typeof cols[j].beforeCellWrite === 'function') { |
62 |
| - var e = { |
63 |
| - rowNum: currRow, |
64 |
| - styleIndex: null, |
65 |
| - cellType: cellType |
66 |
| - }; |
67 |
| - cellData = cols[j].beforeCellWrite(r, cellData, e); |
68 |
| - styleIndex = e.styleIndex || styleIndex; |
69 |
| - cellType = e.cellType; |
70 |
| - delete e; |
71 |
| - } |
72 |
| - switch (cellType) { |
73 |
| - case 'number': |
74 |
| - row += addNumberCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex); |
75 |
| - break; |
76 |
| - case 'date': |
77 |
| - row += addDateCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex); |
78 |
| - break; |
79 |
| - case 'bool': |
80 |
| - row += addBoolCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex); |
81 |
| - break; |
82 |
| - default: |
83 |
| - row += addStringCell(self, getColumnLetter(j + 1) + currRow, cellData, styleIndex); |
84 |
| - } |
85 |
| - } |
86 |
| - row += '</x:row>'; |
87 |
| - rows += row; |
88 |
| - } |
89 |
| - if (colsWidth !== "") { |
90 |
| - sheetFront += '<cols>' + colsWidth + '</cols>'; |
91 |
| - } |
92 |
| - xlsx.file(config.fileName, sheetFront + '<x:sheetData>' + rows + '</x:sheetData>' + sheetBack); |
| 53 | + for (i = 0; i < dataLength; i++) { |
| 54 | + r = data[i], |
| 55 | + currRow = i + 2 |
| 56 | + row = '<x:row r="' + currRow + '" spans="1:' + colsLength + '">' |
| 57 | + for (j = 0; j < colsLength; j++) { |
| 58 | + styleIndex = null |
| 59 | + cellData = r[j] |
| 60 | + cellType = cols[j].type |
| 61 | + if (typeof cols[j].beforeCellWrite === 'function') { |
| 62 | + var e = { |
| 63 | + rowNum: currRow, |
| 64 | + styleIndex: null, |
| 65 | + cellType: cellType |
| 66 | + } |
| 67 | + cellData = cols[j].beforeCellWrite(r, cellData, e) |
| 68 | + styleIndex = e.styleIndex || styleIndex |
| 69 | + cellType = e.cellType |
| 70 | + delete e |
| 71 | + } |
| 72 | + switch (cellType) { |
| 73 | + case 'number': |
| 74 | + row += addNumberCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex) |
| 75 | + break |
| 76 | + case 'date': |
| 77 | + row += addDateCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex) |
| 78 | + break |
| 79 | + case 'bool': |
| 80 | + row += addBoolCell(getColumnLetter(j + 1) + currRow, cellData, styleIndex) |
| 81 | + break |
| 82 | + default: |
| 83 | + row += addStringCell(self, getColumnLetter(j + 1) + currRow, cellData, styleIndex) |
| 84 | + } |
| 85 | + } |
| 86 | + row += '</x:row>' |
| 87 | + rows += row |
| 88 | + } |
| 89 | + if (colsWidth !== '') { |
| 90 | + sheetFront += '<cols>' + colsWidth + '</cols>' |
| 91 | + } |
| 92 | + xlsx.file(config.fileName, sheetFront + '<x:sheetData>' + rows + '</x:sheetData>' + sheetBack) |
93 | 93 | }
|
94 | 94 |
|
95 |
| -module.exports = Sheet; |
| 95 | +module.exports = Sheet |
96 | 96 |
|
97 |
| -var startTag = function (obj, tagName, closed){ |
98 |
| - var result = "<" + tagName, p; |
99 |
| - for (p in obj){ |
100 |
| - result += " " + p + "=" + obj[p]; |
| 97 | +var startTag = function (obj, tagName, closed) { |
| 98 | + var result = '<' + tagName, p |
| 99 | + for (p in obj) { |
| 100 | + result += ' ' + p + '=' + obj[p] |
101 | 101 | }
|
102 |
| - if (!closed) |
103 |
| - result += ">"; |
104 |
| - else |
105 |
| - result += "/>"; |
106 |
| - return result; |
107 |
| -}; |
108 |
| - |
109 |
| -var endTag = function(tagName){ |
110 |
| - return "</" + tagName + ">"; |
111 |
| -}; |
| 102 | + if (!closed) { result += '>' } else { result += '/>' } |
| 103 | + return result |
| 104 | +} |
112 | 105 |
|
113 |
| -var addNumberCell = function(cellRef, value, styleIndex){ |
114 |
| - styleIndex = styleIndex || 0; |
115 |
| - if (value===null) |
116 |
| - return ""; |
117 |
| - else |
118 |
| - return '<x:c r="'+cellRef+'" s="'+ styleIndex +'" t="n"><x:v>'+value+'</x:v></x:c>'; |
119 |
| -}; |
| 106 | +var endTag = function (tagName) { |
| 107 | + return '</' + tagName + '>' |
| 108 | +} |
120 | 109 |
|
121 |
| -var addDateCell = function(cellRef, value, styleIndex){ |
122 |
| - styleIndex = styleIndex || 1; |
123 |
| - if (value===null) |
124 |
| - return ""; |
125 |
| - else |
126 |
| - return '<x:c r="'+cellRef+'" s="'+ styleIndex +'" t="n"><x:v>'+value+'</x:v></x:c>'; |
127 |
| -}; |
| 110 | +var addNumberCell = function (cellRef, value, styleIndex) { |
| 111 | + styleIndex = styleIndex || 0 |
| 112 | + if (value === null) { return '' } else { |
| 113 | + return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="n"><x:v>' + value + '</x:v></x:c>' |
| 114 | + } |
| 115 | +} |
128 | 116 |
|
129 |
| -var addBoolCell = function(cellRef, value, styleIndex){ |
130 |
| - styleIndex = styleIndex || 0; |
131 |
| - if (value===null) |
132 |
| - return ""; |
133 |
| - if (value){ |
134 |
| - value = 1; |
135 |
| - } else |
136 |
| - value = 0; |
137 |
| - return '<x:c r="'+cellRef+'" s="'+ styleIndex + '" t="b"><x:v>'+value+'</x:v></x:c>'; |
138 |
| -}; |
| 117 | +var addDateCell = function (cellRef, value, styleIndex) { |
| 118 | + styleIndex = styleIndex || 1 |
| 119 | + if (value === null) { |
| 120 | + return '' |
| 121 | + } else { |
| 122 | + return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="n"><x:v>' + value + '</x:v></x:c>' |
| 123 | + } |
| 124 | +} |
139 | 125 |
|
| 126 | +var addBoolCell = function (cellRef, value, styleIndex) { |
| 127 | + styleIndex = styleIndex || 0 |
| 128 | + if (value === null) { |
| 129 | + return '' |
| 130 | + } |
| 131 | + if (value) { |
| 132 | + value = 1 |
| 133 | + } else { |
| 134 | + value = 0 |
| 135 | + } |
| 136 | + return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="b"><x:v>' + value + '</x:v></x:c>' |
| 137 | +} |
140 | 138 |
|
141 |
| -var addStringCell = function(sheet, cellRef, value, styleIndex){ |
142 |
| - styleIndex = styleIndex || 0; |
143 |
| - if (value===null) |
144 |
| - return ""; |
145 |
| - if (typeof value ==='string'){ |
146 |
| - value = value.replace(/&/g, "&").replace(/'/g, "'").replace(/>/g, ">").replace(/</g, "<"); |
| 139 | +var addStringCell = function (sheet, cellRef, value, styleIndex) { |
| 140 | + styleIndex = styleIndex || 0 |
| 141 | + if (value === null) { return '' } |
| 142 | + if (typeof value === 'string') { |
| 143 | + value = value.replace(/&/g, '&').replace(/'/g, ''').replace(/>/g, '>').replace(/</g, '<') |
147 | 144 | }
|
148 | 145 | // TODO there is no default value in js map
|
149 |
| - var i |
150 |
| - if (!sheet.shareStrings.has(value)) { |
151 |
| - i = -1 |
152 |
| - } else { |
153 |
| - i = sheet.shareStrings.get(value) |
154 |
| - } |
155 |
| - if ( i< 0){ |
156 |
| - i = sheet.shareStrings.size; |
157 |
| - sheet.shareStrings.set(value, i); |
158 |
| - sheet.convertedShareStrings += "<x:si><x:t>"+value+"</x:t></x:si>"; |
159 |
| - } |
160 |
| - return '<x:c r="'+cellRef+'" s="'+ styleIndex + '" t="s"><x:v>'+i+'</x:v></x:c>'; |
161 |
| -}; |
162 |
| - |
| 146 | + var i |
| 147 | + if (!sheet.shareStrings.has(value)) { |
| 148 | + i = -1 |
| 149 | + } else { |
| 150 | + i = sheet.shareStrings.get(value) |
| 151 | + } |
| 152 | + if (i < 0) { |
| 153 | + i = sheet.shareStrings.size |
| 154 | + sheet.shareStrings.set(value, i) |
| 155 | + sheet.convertedShareStrings += '<x:si><x:t>' + value + '</x:t></x:si>' |
| 156 | + } |
| 157 | + return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="s"><x:v>' + i + '</x:v></x:c>' |
| 158 | +} |
163 | 159 |
|
164 |
| -var getColumnLetter = function(col){ |
165 |
| - if (col <= 0) |
166 |
| - throw "col must be more than 0"; |
167 |
| - var array = new Array(); |
168 |
| - while (col > 0) |
169 |
| - { |
170 |
| - var remainder = col % 26; |
171 |
| - col /= 26; |
172 |
| - col = Math.floor(col); |
173 |
| - if(remainder ===0) |
174 |
| - { |
175 |
| - remainder = 26; |
176 |
| - col--; |
177 |
| - } |
178 |
| - array.push(64 + remainder); |
| 160 | +var getColumnLetter = function (col) { |
| 161 | + if (col <= 0) { throw 'col must be more than 0' } |
| 162 | + var array = new Array() |
| 163 | + while (col > 0) { |
| 164 | + var remainder = col % 26 |
| 165 | + col /= 26 |
| 166 | + col = Math.floor(col) |
| 167 | + if (remainder === 0) { |
| 168 | + remainder = 26 |
| 169 | + col-- |
| 170 | + } |
| 171 | + array.push(64 + remainder) |
179 | 172 | }
|
180 |
| - return String.fromCharCode.apply(null, array.reverse()); |
181 |
| -}; |
| 173 | + return String.fromCharCode.apply(null, array.reverse()) |
| 174 | +} |
0 commit comments