OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
676 stars 95 forks source link

Read formula error information by JavaScript API #4984

Open chengtie opened 6 days ago

chengtie commented 6 days ago

Hello,

I would like to know if it's possible to read formula error information by JavaScript API. Here is an example of formula error:

error-excel

Thank you

qinliuMSFT commented 6 days ago

Hello @chengtie , are you using Custom Function? If so, hope this article Handle and return errors from your custom function can help you.

chengtie commented 6 days ago

Hello @chengtie , are you using Custom Function? If so, hope this article Handle and return errors from your custom function can help you.

Hello, I'm not using Custom Function.

Given a cell (e.g., E4), is there a JavaScript API to check if its formula raises an alarm and return the alarm type (e.g., "Inconsistent Formula")?

qinliuMSFT commented 5 days ago

Hi @chengtie , the Excel JavaScript API provides methods to check if a cell contains a formula and if there is an error. You can use the Range object to access the cell and its properties. Specifically, you can use the formulas and values properties to determine if a cell contains a formula and if there is an error.

Please refer to the following code snippet:

  await Excel.run(async (context) => {
    // Get the active cell
    const range = context.workbook.getActiveCell();

    // Load the necessary properties
    range.load(["formulas", "values"]);
    await context.sync();

    console.log(`Cell formulas:  ${range.formulas[0][0]}`);
    console.log(`Cell value: ${range.values[0][0]}`);
  });

For example, you will get output like this: {C914E7E0-3A9A-4F9D-B76A-0FB0B2322B9F}

Hope it's helpful.

chengtie commented 5 days ago

Hi @chengtie , the Excel JavaScript API provides methods to check if a cell contains a formula and if there is an error. You can use the Range object to access the cell and its properties. Specifically, you can use the formulas and values properties to determine if a cell contains a formula and if there is an error.

Please refer to the following code snippet:

  await Excel.run(async (context) => {
    // Get the active cell
    const range = context.workbook.getActiveCell();

    // Load the necessary properties
    range.load(["formulas", "values"]);
    await context.sync();

    console.log(`Cell formulas:  ${range.formulas[0][0]}`);
    console.log(`Cell value: ${range.values[0][0]}`);
  });

For example, you will get output like this: {C914E7E0-3A9A-4F9D-B76A-0FB0B2322B9F}

Hope it's helpful.

Hello, I know these APIs you mentioned. But they don't return information like "Inconsistent Formula" shown in the screenshot of my initial question. My question is whether there is any API to return that information.

qinliuMSFT commented 5 days ago

@chengtie , thanks for the clarification, I understand your request now. I've put this case in our backlog #9455408 , and PG team will reply to you soon.