johannesboyne's avatar
johannesboyne
Brass Contributor
Jan 11, 2023
Status:
New

Excel JavaScript Office API evaluate formula

Currently, it is not possible to pass a formula to the Office API and receive an evaluation of it.

There are workarounds, like writing to an empty cell, or writing to a scoped name. Both solutions are not directly pretty.

 

Ideal API:

 

  // ...
  await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    const evaluatedFormula = await sheet.evaluateFormula(`=SUM(A1:A5,Sheet2!B1:B5)`)
    // best possible return value
    /*
    {
      "value": 165,
      "type": number,
      "cells_included_in_calculation": ["Sheet1!A1:A5","Sheet2!B1:B5"],
      "precedents": ["Sheet1!A1:A5","Sheet2!B1:B5"] // can deviate from cells included, in case of formulas like SUMIF / SUMIFS / COUNTIF / COUNTIFS / INDEX / ...
    }
    => in case of an error, it could simply return one
    */
  }
  // ...

 

 

  • tobydimmick's avatar
    tobydimmick
    Copper Contributor

    This is a fairly substantial shortcoming compared to the `Application.Evaluate` function available in the COM API. This can make updating a COM addin to Office.js much trickier than necessary.