feat: améliorer inscription et export apprenants
Some checks failed
Validation applicative / TypeScript, tests et build (push) Failing after 2m21s
Some checks failed
Validation applicative / TypeScript, tests et build (push) Failing after 2m21s
This commit is contained in:
35
server/apprenantExportService.ts
Normal file
35
server/apprenantExportService.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as XLSX from "xlsx";
|
||||
|
||||
export type ApprenantExcelExport = {
|
||||
nom: string;
|
||||
prenom: string;
|
||||
formationSuivie: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Empêche qu'une valeur saisie par un utilisateur soit interprétée comme une formule
|
||||
* lorsqu'elle est ouverte dans Excel ou LibreOffice.
|
||||
*/
|
||||
function spreadsheetSafeText(value: string): string {
|
||||
return /^[=+\-@]/.test(value) ? `'${value}` : value;
|
||||
}
|
||||
|
||||
/** Génère la liste des apprenants et des formations auxquelles ils sont inscrits. */
|
||||
export function generateApprenantsExcelExport(apprenants: readonly ApprenantExcelExport[]): Buffer {
|
||||
const rows = [
|
||||
["Nom", "Prénom", "Formation suivie"],
|
||||
...apprenants.map((apprenant) => [
|
||||
spreadsheetSafeText(apprenant.nom),
|
||||
spreadsheetSafeText(apprenant.prenom),
|
||||
spreadsheetSafeText(apprenant.formationSuivie),
|
||||
]),
|
||||
];
|
||||
|
||||
const worksheet = XLSX.utils.aoa_to_sheet(rows);
|
||||
worksheet["!cols"] = [{ wch: 24 }, { wch: 24 }, { wch: 48 }];
|
||||
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "Apprenants");
|
||||
|
||||
return Buffer.from(XLSX.write(workbook, { type: "buffer", bookType: "xlsx" }));
|
||||
}
|
||||
Reference in New Issue
Block a user