1

In the Excel WebApp (Office 365) it is possible to place Office Scripts via the "Automate" tab, which is using the JavaScript-syntax and which could automate excel like a VBA-macro, but for the excel WebApp (Screenshot).

How is it possible to add an API call to an external endpoint (Like a GET request) via this Excel WebApp "Automate" Office Script?
(A scenario would be fetched data from an external API (like weather data) for display in the excel-grid of the excel-webapp).

Screenshot of the excel webapp with office-scripts-code-editor opened

Peter
  • 323
  • 4
  • 15

3 Answers3

4

Requests to external APIs / URLs can be achieved with fetch()

Example:

async function main(workbook: ExcelScript.Workbook) {
  const uri = 'https://jsonplaceholder.typicode.com/posts/1';
  const result = await fetch(uri);
  const json = await result.json();
  
  console.log(json);
}
Daniel G. Wilson
  • 14,955
  • 2
  • 32
  • 39
  • 2
    Awesome, is there a code-sample available for a POST request as well? Thanks – Peter Oct 29 '20 at 11:16
  • Sure, fetch() works the same way it does in the browser—Mozilla's developer docs have some great examples. See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options – Daniel G. Wilson Nov 06 '20 at 02:13
3

Awesome, is there a code-sample available for a POST request as well?

Please refer to the below.

async function main(workbook: ExcelScript.Workbook) {
    const param = {
      method: "POST",
      body: JSON.stringify({
        title: "Test",
        body: "Hello World.",
        userId: 1
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      }
    };
    const res = await fetch("https://jsonplaceholder.typicode.com/posts/", param);
    const json = await res.json();
    console.log(json);
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
kinuasa
  • 91
  • 3
  • Hi, is this confirmed working? I know that fetch also supports POSTing to an endpoint, however, as written [here](https://stackoverflow.com/questions/75364291/office-scripts-post-request-to-external-api-succeeds-but-reports-as-failed) it doesn't seem to be tht easy.... I have not yet seen a single reference to POSTing something in an official doc – baouss Feb 07 '23 at 08:46
0

just to confirm, post from @kinuasa gives me an error "Office Scripts cannot infer the data type of this variable. Please declare a type for the variable.". Adding type to the output solves helped - "const json: string = await res.json();"

iqb0
  • 1
  • 2
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34853851) – treckstar Aug 19 '23 at 05:08