How to Use Javascript in the Excel Web Add-In Application to Zoom the Current Excel WorkSheet

61 views Asked by At

I am trying to increase Zoom for my Excel Online file using my Add-In. I am using the below javascript code but getting error as "GeneralException: An internal error has occurred."

Please advise where am I going wrong. Online tutorials not much of help with this generic error.

Excel.run(function (context) {
    const worksheets = context.workbook.worksheets;
    const worksheet = worksheets.getActiveWorksheet();

    if (worksheet) {
        return context.sync()
            .then(function () {
                worksheet.pageLayout.zoom = { scale: 200 };
            });
    } 
}).catch(function (error) {
    $('#txtcount').val(error);
    console.error(error);
    if (error instanceof OfficeExtension.Error) {
        console.error("Debug info: " + JSON.stringify(error.debugInfo));
        $('#txtcount2').val("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
1

There are 1 answers

0
penglongzhaochina On

After do some investigation, I think the API works well. I think the issue may cause by some "async" issue. Below is the code can work, please try it:

const worksheets = context.workbook.worksheets;
const worksheet = worksheets.getActiveWorksheet();

worksheet.pageLayout.load('zoom');
await context.sync();
console.log(worksheet.pageLayout.zoom);
worksheet.pageLayout.zoom = { scale: 200 };
await context.sync();
worksheet.pageLayout.load('zoom');
await context.sync();
console.log(worksheet.pageLayout.zoom);

Feel free to contact me if you have other questions.