Some checks failed
Validation applicative / TypeScript, tests et build (push) Failing after 2m21s
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
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" }));
|
|
}
|