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

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, is there a code-sample available for a POST request as well? Thanks
Sure, fetch() works the same way it does in the browser—Mozilla's developer docs have some great examples. See: developer.mozilla.org/en-US/docs/Web/API/Fetch_API/…
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);
}

1 Comment

Hi, is this confirmed working? I know that fetch also supports POSTing to an endpoint, however, as written here it doesn't seem to be tht easy.... I have not yet seen a single reference to POSTing something in an official doc
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();"

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.