Jelajahi Sumber

Fix: Impossible to export board to excel where title exceeding 31 chars

Ben0it-T 3 tahun lalu
induk
melakukan
08a01c6d2c
1 mengubah file dengan 82 tambahan dan 39 penghapusan
  1. 82 39
      models/server/ExporterExcel.js

+ 82 - 39
models/server/ExporterExcel.js

@@ -167,7 +167,28 @@ class ExporterExcel {
     workbook.lastPrinted = new Date();
     const filename = `${result.title}.xlsx`;
     //init worksheet
-    const worksheet = workbook.addWorksheet(result.title, {
+    let worksheetTitle = result.title;
+    if (worksheetTitle.length > 31) {
+      // MS Excel doesn't allow worksheet name longer than 31 chars
+      // Exceljs truncate names to 31 chars
+      let words = worksheetTitle.split(" ");
+      let tmpTitle = "";
+      for (let i=0;i<words.length; i++) {
+        if (words[0].length > 27) {
+          // title has no spaces
+          tmpTitle = words[0].substr(0,27) + " ";
+          break;
+        }
+        if(tmpTitle.length + words[i].length < 27) {
+          tmpTitle += words[i] + " ";
+        }
+        else {
+          break;
+        }
+      }
+      worksheetTitle = tmpTitle + "...";
+    }
+    const worksheet = workbook.addWorksheet(worksheetTitle, {
       properties: {
         tabColor: {
           argb: 'FFC0000',
@@ -179,7 +200,7 @@ class ExporterExcel {
       },
     });
     //get worksheet
-    const ws = workbook.getWorksheet(result.title);
+    const ws = workbook.getWorksheet(worksheetTitle);
     ws.properties.defaultRowHeight = 20;
     //init columns
     //Excel font. Western: Arial. zh-CN: 宋体
@@ -311,7 +332,7 @@ class ExporterExcel {
     };
     ws.getCell('A1').alignment = {
       vertical: 'middle',
-      horizontal: 'center',
+      horizontal: 'left',
     };
     ws.getRow(1).height = 40;
     //get member and assignee info
@@ -354,6 +375,7 @@ class ExporterExcel {
     }
     //add data +8 hours
     function addTZhours(jdate) {
+      if (!jdate) { return ' '; }
       const curdate = new Date(jdate);
       const checkCorrectDate = moment(curdate);
       if (checkCorrectDate.isValid()) {
@@ -382,7 +404,14 @@ class ExporterExcel {
         wrapText: true,
       };
     }
-
+    // cell Card alignment
+    function cellCardAlignment(cellno) {
+      ws.getCell(cellno).alignment = {
+        vertical: 'top',
+        horizontal: 'left',
+        wrapText: true,
+      };
+    }
     //all border
     function allBorder(cellno) {
       ws.getCell(cellno).border = {
@@ -412,6 +441,8 @@ class ExporterExcel {
 
     ws.mergeCells('B3:H3');
     ws.getRow(3).height = 40;
+    // In MS Excel, we can't use the AutoFit feature on a column that contains a cell merged with cells in other columns.
+    // Likewise, we can't use AutoFit on a row that contains a cell merged with cells in other rows. 
     ws.getRow(3).font = {
       name: TAPi18n.__('excel-font'),
       size: 10,
@@ -619,6 +650,7 @@ class ExporterExcel {
         name: TAPi18n.__('excel-font'),
         size: 10,
       };
+      // Border
       allBorder(`A${y}`);
       allBorder(`B${y}`);
       allBorder(`C${y}`);
@@ -637,20 +669,35 @@ class ExporterExcel {
       allBorder(`P${y}`);
       allBorder(`Q${y}`);
       allBorder(`R${y}`);
-      cellCenter(`A${y}`);
-      ws.getCell(`B${y}`).alignment = {
+      // Alignment
+      ws.getCell(`A${y}`).alignment = {
+        vertical: 'top',
+        horizontal: 'right',
         wrapText: true,
       };
-      ws.getCell(`C${y}`).alignment = {
-        wrapText: true,
-      };
-      ws.getCell(`M${y}`).alignment = {
-        wrapText: true,
-      };
-      ws.getCell(`N${y}`).alignment = {
+      cellCardAlignment(`B${y}`);
+      cellCardAlignment(`C${y}`);
+      cellCardAlignment(`D${y}`);
+      cellCardAlignment(`E${y}`);
+      cellCardAlignment(`F${y}`);
+      cellCardAlignment(`G${y}`);
+      cellCardAlignment(`H${y}`);
+      cellCardAlignment(`I${y}`);
+      cellCardAlignment(`J${y}`);
+      cellCardAlignment(`K${y}`);
+      cellCardAlignment(`L${y}`);
+      cellCardAlignment(`M${y}`);
+      cellCardAlignment(`N${y}`);
+      cellCardAlignment(`O${y}`);
+      cellCardAlignment(`P${y}`);
+      ws.getCell(`Q${y}`).alignment = {
+        vertical: 'top',
+        horizontal: 'center',
         wrapText: true,
       };
-      ws.getCell(`O${y}`).alignment = {
+      ws.getCell(`R${y}`).alignment = {
+        vertical: 'top',
+        horizontal: 'center',
         wrapText: true,
       };
     }
@@ -714,6 +761,14 @@ class ExporterExcel {
         },
       },
     ];
+    // cell Card alignment
+    function cellCardAlignmentWs2(cellno) {
+      ws2.getCell(cellno).alignment = {
+        vertical: 'top',
+        horizontal: 'left',
+        wrapText: true,
+      };
+    }
     //all border
     function allBorderWs2(cellno) {
       ws2.getCell(cellno).border = {
@@ -805,37 +860,25 @@ class ExporterExcel {
         name: TAPi18n.__('excel-font'),
         size: 10,
       };
-      ws2.getCell(`A${y}`).alignment = {
-        vertical: 'middle',
-        horizontal: 'center',
-        wrapText: true,
-      };
-      ws2.getCell(`B${y}`).alignment = {
-        vertical: 'middle',
-        wrapText: true,
-      };
-      ws2.getCell(`C${y}`).alignment = {
-        vertical: 'middle',
-        wrapText: true,
-      };
-      ws2.getCell(`D${y}`).alignment = {
-        vertical: 'middle',
-        wrapText: true,
-      };
-      ws2.getCell(`E${y}`).alignment = {
-        vertical: 'middle',
-        wrapText: true,
-      };
-      ws2.getCell(`F${y}`).alignment = {
-        vertical: 'middle',
-        wrapText: true,
-      };
+      // Border
       allBorderWs2(`A${y}`);
       allBorderWs2(`B${y}`);
       allBorderWs2(`C${y}`);
       allBorderWs2(`D${y}`);
       allBorderWs2(`E${y}`);
       allBorderWs2(`F${y}`);
+      // Alignment
+      ws2.getCell(`A${y}`).alignment = {
+        vertical: 'top',
+        horizontal: 'right',
+        wrapText: true,
+      };
+      cellCardAlignmentWs2(`B${y}`);
+      cellCardAlignmentWs2(`C${y}`);
+      cellCardAlignmentWs2(`D${y}`);
+      cellCardAlignmentWs2(`E${y}`);
+      cellCardAlignmentWs2(`F${y}`);
+
     }
     workbook.xlsx.write(res).then(function () {});
   }