I'm having trouble trying to embed a Highchart's chart image into an Excel file. I am basically trying to use an existing "Export Excel" button on a website to now export a static image of the Highchart displayed on the website into an excel file. I'll add the relevant code below:
async getChartPng(chartInstance): Promise<string> {
console.log('getChartPng called');
return new Promise(async (resolve, reject) => {
console.log('Inside the promise');
try {
chartInstance.exportChart({}, (dataUrl) => {
console.log('Inside exportChart callback');
// Log the value of dataUrl
console.log('dataUrl value:', dataUrl);
if (dataUrl) {
console.log('DataUrl:', dataUrl);
resolve(dataUrl);
} else {
console.log('DataUrl is empty or undefined');
reject('Failed to get dataUrl');
}
});
console.log('After exportChart function call');
} catch (error) {
console.error('Error in exportChart function:', error);
reject(error);
}
});
}
async addChartImage(workbook: ExcelJS.Workbook, chartInstance {
console.log('addChartImage called');
const chartWorksheet = workbook.addWorksheet('HighChart Image');
console.log('chartWorksheet created:', chartWorksheet);
try {
const chartImagePng = await this.getChartPng(chartInstance);
console.log('chartImagePng:', chartImagePng);
const base64Image = chartImagePng.split(',')[1];
const imageId = workbook.addImage({
base64: base64Image,
extension: 'png',
});
chartWorksheet.addImage(imageId, {
tl: { col: 0, row: 0 },
ext: { width: 600, height: 400 },
});
} catch (error) {
console.error('Error adding chart image to workbook:', error);
}
}
My code never enters into the exportChart callback function and I'm not sure why. That is to say the console.log message ('Inside exportChart callback') never prints but the message ('After exportChart function call') does print to the console browser.
I should add that a worksheet named HighChart image does show up in an excel file when I click an existing custom button on the website as intended. However, the worksheet shows up as empty and a snapshot of the Highchart image downloads as a separate png file along with the Excel file. However, what I really need is the Highchart image to show up in the excel file, in the proper worksheet. How can I do that?