How to : Excel export

Hello ! My client absolutely wants an Excel export, whereas there already is CSV export, but that’s not enough for him.
Anyone know how to do that ? I’m trying to run a javascript but i’m just getting “False” although there are data in my collection :
// Get the data from the collection
const data = wwLib.wwVariable.getValue(“CA’s_paginated_groupe”);

// Check if data exists and is an array
if (!data || !Array.isArray(data)) {
console.error(“No valid data found”);
return false;
}

// Create CSV content
const csvContent = data.map(item => {
return {
‘Montant CA’: item.montant_ca || 0,
‘Remise’: item.remise || 0,
‘Fournisseur’: item.fournisseurs?.nom_fournisseur || ‘’,
‘Date de création’: item.created_at || ‘’
};
});

// Convert to CSV string
const headers = Object.keys(csvContent[0]);
const csvString = [
headers.join(‘,’),
…csvContent.map(row => headers.map(header => row[header]).join(‘,’))
].join(‘\n’);

// Create and download file
const blob = new Blob([csvString], { type: ‘text/csv;charset=utf-8;’ });
const link = document.createElement(‘a’);
link.href = URL.createObjectURL(blob);
link.download = ‘export_ca.csv’;
link.click();

return true;
Thanks

1 Like

Has solucionado este punto?

Nop, finally I managed to convince him to stay with csv export. But I explored 2 solutions : with Make and with custom code. Both weren’t working but I may have made mistakes !

I implemented .xlsx export thru the sheetJS package in the NPM plugin. Here is a video of how it works

and this is the script that executes onClick

try {

    const row = context.item.data?.['row'];

if (!row) {

        wwLib.wwNotification.open({ text: 'No se encontraron datos', color: 'warning' });

return;

}


const estructuraVertical = [

["ID Org", row.id_ce] // map any other fields you want here, “ID Org” is the header of the field i want to have in the file and row.id_ce is where the value is stored

];




const ws = XLSX.utils.aoa_to_sheet(estructuraVertical);




const maxWidthA = estructuraVertical.reduce((max, f) => {

const largo = f[0] ? f[0].toString().length : 0;

return Math.max(max, largo);

}, 10);




    ws['!cols'] = [

{ wch: maxWidthA + 2 }, // +2 para dar un pequeño margen de respiro

{ wch: 50 }

];




const wb = XLSX.utils.book_new();

XLSX.utils.book_append_sheet(wb, ws, 'Ficha Registro');




const fileName = `ficha_${row["Nombre corto"] || 'registro'}_${new Date().toISOString().slice(0, 10)}.xlsx`;

XLSX.writeFile(wb, fileName);




    wwLib.wwNotification.open({ text: 'Ficha generada', color: 'success' });




} catch (error) {

    console.error('Error:', error);

    wwLib.wwNotification.open({ text: 'Error: ' + error.message, color: 'error' });

}
2 Likes

Thanks @javierlaborde looks nice !!

And thanks for the script alsoo haha