function handleExcelDownload(args) {
try {
// Parse the JSON string if necessary
const data = typeof args.responseData === "string" ? JSON.parse(args.responseData) : args.responseData;
// Extract the base64-encoded file content
if (!data.file) {
throw new Error("Invalid response format: Missing 'file' property");
}
const base64Data = data.file;
// Decode the base64 string to binary
const byteCharacters = atob(base64Data);
const byteArrays = [];
// Convert the binary string into an array of bytes
for (let offset = 0; offset < byteCharacters.length; offset += 512) {
const slice = byteCharacters.slice(offset, offset + 512);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
// Create a Blob from the byte arrays
const blob = new Blob(byteArrays, { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// Create a temporary link element and trigger the download
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'customer-rates.xlsx';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(link.href);
} catch (error) {
console.error('Failed to download the file:', error);
}
}