Function to download the report displayed on the NetSuite Dashboard using Portlet script

Attached the current view of the report displayed in the NetSuite Dashboard

/**
 * Downloads the commission report as an Excel file.
 */
function downloadExcelReport() {
    const commissionReport = document.querySelector('.commission-report table');
    const tableData = [];
    const rows = commissionReport.querySelectorAll('tr');


    // Get table headers
    const headers = [];
    rows[0].querySelectorAll('td').forEach(header => {
        headers.push(header.innerText);
    });
    tableData.push(headers);


    // Get table rows
    rows.forEach((row, index) => {
        if (index === 0) return; // Skip the repeated header row
        const rowData = [];
        row.querySelectorAll('td').forEach(cell => {
            rowData.push(cell.innerText);
        });
        tableData.push(rowData);
    });


    const worksheet = XLSX.utils.aoa_to_sheet(tableData);


    // Define cell styles
    const headerCellStyle = {
        font: { bold: true },
        alignment: { horizontal: "center", vertical: "center" }
    };


    const dataCellStyle = {
        alignment: { horizontal: "center", vertical: "center" }
    };


    // Apply styles to headers
    headers.forEach((header, colIdx) => {
        const cellAddress = XLSX.utils.encode_cell({ r: 0, c: colIdx });
        if (!worksheet[cellAddress]) worksheet[cellAddress] = {};
        worksheet[cellAddress].s = headerCellStyle;
    });


    // Apply styles to data rows
    tableData.slice(1).forEach((row, rowIdx) => {
        row.forEach((cell, colIdx) => {
            const cellAddress = XLSX.utils.encode_cell({ r: rowIdx + 1, c: colIdx });
            if (!worksheet[cellAddress]) worksheet[cellAddress] = {};
            worksheet[cellAddress].s = dataCellStyle;
        });
    });


    const workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workbook, worksheet, 'Commission Report');


    XLSX.writeFile(workbook, 'commission_report.xlsx');
}

Leave a comment

Your email address will not be published. Required fields are marked *