How to : Excel export

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