rabin 3 gadi atpakaļ
vecāks
revīzija
3db242b01d
1 mainītis faili ar 30 papildinājumiem un 0 dzēšanām
  1. 30 0
      assets/lib/excel/excel.js

+ 30 - 0
assets/lib/excel/excel.js

@@ -4,12 +4,42 @@ var Excel =
     open : function(data, filename, sheetname) {
         this.name = sheetname || 'sheet1';
         this.sheet = XLSX.utils.aoa_to_sheet(data, {raw: true});
+        this.autoWidthFunc(this.sheet, data);
         if (filename.substr(-5).toLowerCase() !== '.xlsx') {
             filename += '.xlsx';
         }
         this.openDownloadDialog(this.sheet2blob(this.sheet, this.name), filename);
     },
 
+    autoWidthFunc : function (ws, data) {
+      // set worksheet max width per col
+      const colWidth = data.map(row =>
+        row.map(val => {
+          var l = val.toString().length;
+          var n = l + l*0.5;
+          // if null/undefined
+          if (val == null) {
+            return { wch: 10 };
+          } else if (val.toString().charCodeAt(0) > 255) {
+            // if chinese
+            return { wch: n * 2 };
+          } else {
+            return { wch: n };
+          }
+        })
+      );
+      // start in the first row
+      const result = colWidth[0];
+      for (let i = 1; i < colWidth.length; i++) {
+        for (let j = 0; j < colWidth[i].length; j++) {
+          if (result[j].wch < colWidth[i][j].wch) {
+            result[j].wch = colWidth[i][j].wch;
+          }
+        }
+      }
+      ws['!cols'] = result;
+    },
+
     export : function (url, data) {
     	var self = this;
         var filename = '';