I’m currently creating my google chrome extension that inject a button in netsuite and when this button is clicked it should trigger the native Excel export Netsuite button and everything is working but I want to use SheetJs to convert directly the XLS file into XLSX.
I tried a lot of different approach but the XLS file keeps getting downloaded and the conversion is not working and this is my background script code:
importScripts ["xlsx.full.min.js"];
console.log('Background script loading...');
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Message received in background:', message);
if (message.action === "exportStarted") {
console.log('Export process started by user');
sendResponse({status: 'received', message: 'Export start acknowledged'});
watchForDownload();
}
return true; // Keeps the message channel open for asynchronous response
});
function watchForDownload() {
console.log('Watching for download...');
let attempts = 0;
const maxAttempts = 10;
function checkDownloads() {
chrome.downloads.search({limit: 1, orderBy: ['-startTime']}, (downloads) => {
console.log('Recent downloads:', downloads);
if (downloads && downloads.length > 0) {
const download = downloads[0];
console.log('Most recent download:', download);
if (download.filename.toLowerCase().endsWith('.xls')) {
console.log('XLS file detected, attempting conversion');
convertToXLSX(download);
} else {
console.log('Not an XLS file, continuing to watch...');
retryCheck();
}
} else {
console.log('No downloads found, continuing to watch...');
retryCheck();
}
});
}
function retryCheck() {
attempts++;
if (attempts < maxAttempts) {
console.log(`Retry attempt ${attempts} of ${maxAttempts}`);
setTimeout(checkDownloads, 1000); // Check every second
} else {
console.log('Max attempts reached, stopping watch');
}
}
checkDownloads();
}
function convertToXLSX(downloadItem) {
console.log('Starting conversion for:', downloadItem.filename);
fetch(downloadItem.url)
.then(response => response.arrayBuffer())
.then(data => {
console.log('XLS data fetched, size:', data.byteLength);
const workbook = XLSX.read(new Uint8Array(data), {type: 'array'});
console.log('Workbook read, sheets:', workbook.SheetNames);
const xlsxOutput = XLSX.write(workbook, {bookType: 'xlsx', type: 'array'});
console.log('XLSX data created, size:', xlsxOutput.length);
const blob = new Blob([xlsxOutput], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
const url = URL.createObjectURL(blob);
const newFilename = downloadItem.filename.replace(/.xls$/i, '.xlsx');
console.log('New filename:', newFilename);
chrome.downloads.download({
url: url,
filename: newFilename,
saveAs: false
}, (downloadId) => {
console.log('XLSX download started, id:', downloadId);
URL.revokeObjectURL(url);
chrome.downloads.removeFile(downloadItem.id, () => {
console.log('Original XLS file removed');
});
});
})
.catch(error => console.error('Conversion error:', error));
}
console.log('Background script loaded and running.');