Reputation: 21
Is there any other way to print the excel sheet or any way to trigger print button of excel?
https://learn.microsoft.com/en-us/office/dev/add-ins/excel/
I have searched a lot in office JS documentation. But, not getting any solution for print the excel sheet. I think not any API available in the office JS for print the sheet.
Upvotes: 1
Views: 474
Reputation: 49
You cannot print Excel sheets directly from the office js API. However, you can use a workaround by exporting the Excel sheet to a PDF and then triggering the print dialog for the PDF, like this:
// Function to export Excel sheet to PDF
function exportSheetToPDF(context) {
// Replace with your sheet name
let sheet = context.workbook.worksheets.getItem("YourSheetName");
// Get the used range of the sheet
let range = sheet.getUsedRange();
range.load("address");
// Run the batch operation
return context.sync().then(function() {
// Replace with your logic to export the range to PDF
// This part depends on your application's capabilities
});
}
// Function to trigger print dialog on the PDF
function printPDF(pdfUrl) {
let win = window.open(pdfUrl, '_blank');
win.focus();
win.onload = function() {
win.print();
};
}
// Usage
Excel.run(function (context) {
return exportSheetToPDF(context);
}).then(function (pdfUrl) {
printPDF(pdfUrl);
}).catch(function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
Upvotes: 1