最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - API call via Office scripts in MS Excel WebApp - Stack Overflow

programmeradmin6浏览0评论

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).

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).

Share Improve this question asked Oct 28, 2020 at 7:15 PeterPeter 3718 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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

Example:

async function main(workbook: ExcelScript.Workbook) {
  const uri = 'https://jsonplaceholder.typicode./posts/1';
  const result = await fetch(uri);
  const json = await result.json();
  
  console.log(json);
}

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./posts/", param);
    const json = await res.json();
    console.log(json);
}

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();"

发布评论

评论列表(0)

  1. 暂无评论