From 7250018d116efc897bd6933f2e4ccb08acf52581 Mon Sep 17 00:00:00 2001
From: Etienne LOUPIAS <eloupias@grandlyon.com>
Date: Mon, 25 Nov 2024 09:38:09 +0000
Subject: [PATCH] fix(data): improve csv export for excel

---
 .gitlab-ci.yml         |  6 ++++--
 src/app/utils/utils.ts | 11 +++++++----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e8e1e6145..f0d535dcb 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -24,9 +24,10 @@ build:
   rules:
     # Always run the stage for the 'dev' branch
     - if: '$CI_COMMIT_REF_NAME == "dev"'
-    # For Merge Requests, make the stage manual
+    # For Merge Requests, make the stage manual (and optional)
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
       when: manual
+      allow_failure: true
     # Otherwise, skip the stage
     - when: never    
   script:
@@ -155,9 +156,10 @@ deploy-10-dev:
   rules:
     # Always run the stage for the 'dev' branch
     - if: '$CI_COMMIT_REF_NAME == "dev"'
-    # For Merge Requests, make the stage manual
+    # For Merge Requests, make the stage manual (and optional)
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
       when: manual
+      allow_failure: true
     # Otherwise, skip the stage
     - when: never    
 
diff --git a/src/app/utils/utils.ts b/src/app/utils/utils.ts
index b414dc9e8..e65f9c8b0 100644
--- a/src/app/utils/utils.ts
+++ b/src/app/utils/utils.ts
@@ -199,7 +199,7 @@ export class Utils {
   }
 
   private convertToCSV(data: any[]): string {
-    const headers = Object.keys(data[0]).join(','); // CSV headers
+    const headers = Object.keys(data[0]).join(';'); // CSV headers
 
     const rows = data.map((row) => {
       return Object.values(row)
@@ -213,20 +213,23 @@ export class Utils {
           }
 
           // If the value contains commas, newlines, or apostrophes, enclose it in double quotes
-          if (strValue.includes(',') || strValue.includes('\n') || strValue.includes("'")) {
+          if (strValue.includes(';') || strValue.includes('\n') || strValue.includes("'")) {
             strValue = `"${strValue}"`;
           }
 
           return strValue;
         })
-        .join(','); // Join fields with commas
+        .join(';'); // Join fields with commas
     });
 
     return [headers, ...rows].join('\n'); // Return the headers and data rows
   }
 
   private downloadCSV(csv: string, filename: string): void {
-    const blob = new Blob([csv], { type: 'text/csv' });
+    const BOM = '\uFEFF'; // Special character at the beginning of the file for Excel encoding
+    csv = BOM + csv;
+    csv = csv.replace(/\n/g, '\r\n'); // Ensure proper line breaks in CSV
+    const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' });
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
     a.setAttribute('href', url);
-- 
GitLab