OfficeDev / OfficeDev/Excel-Custom-Functions

Javascript function making an api call to http://localhost:8080 vs https://app.mydomain.com

Open
#400 0 comments 0 reactions 3 assignees View on GitHub

@donlvMSFT is already working on this.

Since Mar 1, 2024.

Needs: triage :mag:
Dominant language
TypeScript
Stars
333
Forks
69
Avg merge
3d 15h
Merged PRs (30d)
3

Description

Expected behavior

I have defined the following javascript function within my functsion.ts file:

/* global clearInterval, console, CustomFunctions, setInterval */
/**
 * Calculate the Present Value of a Policy
 * @customfunction
 * @param deathBenefit Death Benefit
 * @param discountRate Discount Rate
 * @param policyTerm Policy Term
 * @returns The Present Value
 */
export async function calculate(deathBenefit: number, discountRate: number, policyTerm: number): Promise<string> {
  // let host = "https://app.mydomain.com"; // does work.
  let host = "http://localhost:8080"; // does not work
  // let host = "http://ngrok...url"; // an Ngrok url pointing to my localhost:8080 does work
  // let host = "https://...-8080.inc1.devtunnels.ms"; // microsoft dev tunnels does not work.

  try {
    let request = new CalculateTermLifePresentValueRequest();
    request.setDeathBenefit(deathBenefit);
    request.setDiscountRate(discountRate);
    request.setPolicyTerm(policyTerm);

    let api = new ExcelServicePromiseClient(host);
    let response = await api.calculateTermLifePresentValue(request);
    return JSON.stringify(response.getResults().getBasicValue());

  } catch (e) {
    console.error("Error:", e.code, e.message); // Detailed output
    return `host: ${host}, ${JSON.stringify(e)}`;
  }
}

Current behavior

When I spin up a local server at http://localhost:8080, and update the host to this local endpoint, the method fails with the following error:

host: http://localhost:8080, {"message":"Http response at 400 or 500 level, http status code: 0","stack":"E@https://localhost:3000/functions.js:2389:68\nY@https://localhost:3000/functions.js:2425:45\n@https://localhost:3000/functions.js:2423:197\nKb@https://localhost:3000/functions.js:2401:186\nO@https://localhost:3000/functions.js:2400:616\nzc@https://localhost:3000/functions.js:2411:412\nBc@https://localhost:3000/functions.js:2414:230\n@https://localhost:3000/functions.js:2412:254\n@https://localhost:3000/functions.js:2412:232","code":2,"metadata":{}}

Steps to Reproduce

To confirm that my server at http://localhost:8080 works, I created an Ngrok endpoint, to my host. And when using the public Ngrok as the host, the api call is made successfully.

Also deploying this api server to a custom domain say http://api.mydomain.com also works.

Any idea why the local host would not work?

The order of events:

  1. Spin up an api server at http://localhost:8080 to serve the api call made within the functions.ts file.
  2. webpack serve --mode development
  3. office-addin-debugging start manifest.xml
  4. Navigate to the opened MS Excel and test the method: CONTOSO.CALCULATE

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.