1/* 2 * #################################################################################################### 3 * https://www.npmjs.com/package/xlsx-style 4 * #################################################################################################### 5 */ 6var Jhxlsx = { 7 config: { 8 fileName: "report", 9 extension: ".xlsx", 10 sheetName: "Sheet", 11 fileFullName: "report.xlsx", 12 header: true, 13 createEmptyRow: true, 14 maxCellWidth: 20 15 }, 16 worksheetObj: {}, 17 rowCount: 0, 18 wsColswidth: [], 19 merges: [], 20 worksheet: {}, 21 range: {}, 22 init: function (options) { 23 this.reset(); 24 if (options) { 25 for (var key in this.config) { 26 if (options.hasOwnProperty(key)) { 27 this.config[key] = options[key]; 28 } 29 } 30 } 31 this.config['fileFullName'] = this.config.fileName + this.config.extension; 32 }, 33 reset: function () { 34 this.range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; 35 this.worksheetObj = {}; 36 this.rowCount = 0; 37 this.wsColswidth = []; 38 this.merges = []; 39 this.worksheet = {}; 40 }, 41 parse2Int0: function (num) { 42 num = parseInt(num); 43 num = Number.isNaN(num) ? 0 : num; 44 return num; 45 }, 46 cellWidth: function (cellText, pos) { 47 var max = (cellText && cellText.length * 1.3); 48 if (this.wsColswidth[pos]) { 49 if (max > this.wsColswidth[pos].wch) { 50 this.wsColswidth[pos] = {wch: max}; 51 } 52 } else { 53 this.wsColswidth[pos] = {wch: max}; 54 } 55 }, 56 cellWidthValidate: function () { 57 for (var i in this.wsColswidth) { 58 if (this.wsColswidth[i].wch > this.config.maxCellWidth) { 59 this.wsColswidth[i].wch = this.config.maxCellWidth; 60 } 61 } 62 }, 63 datenum: function (v, date1904) { 64 if (date1904) 65 v += 1462; 66 var epoch = Date.parse(v); 67 return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); 68 }, 69 setCellDataType: function (cell) { 70 if (typeof cell.v === 'number') { 71 cell.t = 'n'; 72 } else if (typeof cell.v === 'boolean') { 73 cell.t = 'b'; 74 } else if (cell.v instanceof Date) { 75 cell.t = 'n'; 76 cell.z = XLSX.SSF._table[14]; 77 cell.v = this.datenum(cell.v); 78 } else { 79 cell.t = 's'; 80 } 81 }, 82 jhAddRow: function (rowObj) { 83 for (var c in rowObj) { 84 c = this.parse2Int0(c); 85 var cellObj = rowObj[c]; 86 if (this.range.s.r > this.rowCount) 87 this.range.s.r = this.rowCount; 88 if (this.range.s.c > c) 89 this.range.s.c = c; 90 if (this.range.e.r < this.rowCount) 91 this.range.e.r = this.rowCount; 92 if (this.range.e.c < c) 93 this.range.e.c = c; 94 95 var cellText = null; 96 if (cellObj.hasOwnProperty('text')) { 97 cellText = cellObj.text; 98 } 99 var cell = {v: cellText}; 100 101 var calColWidth = true; 102 if (cellObj.hasOwnProperty('merge')) { 103 var mergeObj = cellObj.merge; 104 calColWidth = false; 105 //var colStartEnd = cellObj.merge.split('-'); 106 var ec = c; 107 var er = this.rowCount; 108 if (mergeObj.hasOwnProperty('c')) { 109 ec = (c + parseInt(mergeObj.c)); 110 } 111 if (mergeObj.hasOwnProperty('r')) { 112 er = (this.rowCount + parseInt(mergeObj.r)); 113 } 114 115 this.merges.push({s: {r: this.rowCount, c: c}, e: {r: er, c: ec}}); 116 //this.merges.push({s: {r: this.rowCount, c: c}, e: {r: (this.rowCount + 1), c: (c + 2)}}); 117 } 118 if (calColWidth) { 119 this.cellWidth(cell.v, c); 120 } 121 if (cell.v === null) 122 continue; 123 var cell_ref = XLSX.utils.encode_cell({c: c, r: this.rowCount}); 124 this.setCellDataType(cell); 125 126 if (cellObj.hasOwnProperty('style')) { 127 cell.s = cellObj.style; 128 } 129 130 this.worksheet[cell_ref] = cell; 131 } 132 this.rowCount++; 133 }, 134 createWorkSheet: function () { 135 for (var i in this.worksheetObj.data) { 136 this.jhAddRow(this.worksheetObj.data[i]); 137 } 138 this.cellWidthValidate(); 139 //console.log(this.merges); 140 //this.worksheet['!merges'] = [{s: {r: 0, c: 0}, e: {r: 0, c: 4}},{s: {r: 5, c: 0}, e: {r: 6, c: 3}}];//this.merges; 141 this.worksheet['!merges'] = this.merges; 142 this.worksheet['!cols'] = this.wsColswidth; 143 if (this.range.s.c < 10000000) 144 this.worksheet['!ref'] = XLSX.utils.encode_range(this.range); 145 return this.worksheet; 146 }, 147 s2ab: function (s) { 148 var buf = new ArrayBuffer(s.length); 149 var view = new Uint8Array(buf); 150 for (var i = 0; i != s.length; ++i)
151 view[i] = s.charCodeAt(i) & 0xFF; 152 return buf; 153 }, 154 getBlob: function (workbookObj, options) { 155 this.init(options); 156 var workbook = new Workbook(); 157 /* add worksheet to workbook */ 158 for (var i in workbookObj) { 159 this.reset(); 160 this.worksheetObj = workbookObj[i]; 161 var sheetName = this.config.sheetName + i; 162 if (this.worksheetObj.hasOwnProperty('sheetName')) { 163 sheetName = this.worksheetObj.sheetName; 164 } 165 this.createWorkSheet(); 166 workbook.SheetNames.push(sheetName); 167 workbook.Sheets[sheetName] = this.worksheet; 168 } 169 var wbout = XLSX.write(workbook, {bookType: 'xlsx', bookSST: true, type: 'binary'}); 170 var blobData = new Blob([this.s2ab(wbout)], {type: "application/octet-stream"}); 171 return blobData; 172 }, 173 export: function (workbookObj, options) { 174 saveAs(this.getBlob(workbookObj, options), this.config.fileFullName); 175 }, 176} 177 178function Workbook() { 179 if (!(this instanceof Workbook)) 180 return new Workbook(); 181 this.SheetNames = []; 182 this.Sheets = {}; 183}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.