exceloffice-jsjavascript-api-for-office

Trigger calculation in JavaScript API for Excel


I am trying to use JavaScript API for Excel.

Does anyone know how to trigger calculation for a cell, a range, a worksheet or a workbook?

For instance, the following code selects a range, but how can we re-calculate the range?

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "F5:F10"; 
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    range.select();
    return ctx.sync(); 
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

Solution

  • You can re-calculate the workbook, which should accomplish your scenario. For the JS API, it's at the application level:

    Excel.run(function (ctx) { 
        ctx.workbook.application.calculate('Full');
        return ctx.sync(); 
    }).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
    

    That being said, unless your workbook is in manual calculation mode, there should be no need for you to force a recalc. All dependent cells should get updated automatically.

    More info on the API here: https://github.com/OfficeDev/office-js-docs/blob/master/excel/resources/application.md#calculatecalculationtype-string

    Hope this helps!

    ~ Michael Zlatkovsky, developer on Office Extensibility team, MSFT