Excel-Custom-Functions icon indicating copy to clipboard operation
Excel-Custom-Functions copied to clipboard

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

Open jankrynauw opened this issue 6 months ago • 0 comments

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

jankrynauw avatar Feb 28 '24 13:02 jankrynauw