API 通过 MS 中的 Office 脚本调用 Excel WebApp

API call via Office scripts in MS Excel WebApp

在 Excel WebApp (Office 365) 中,可以通过“自动化”选项卡放置 Office 脚本,该选项卡使用 JavaScript-语法并且可以自动执行 excel 类似于 VBA 宏,但用于 excel WebApp(屏幕截图)。

如何通过此 Excel WebApp“自动化”Office 脚本向外部端点(如 GET 请求)添加 API 调用?
(场景将从外部 API 获取数据(如天气数据)以显示在 excel-webapp 的 excel-grid 中。

可以通过 fetch()

实现对外部 API/URL 的请求

示例:

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);
}

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

请参考以下

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);
}