Office スクリプトで外部取得呼び出しを使用する

このスクリプトは、ユーザーの GitHub リポジトリに関する基本情報を取得します。 単純なシナリオでの使用方法 fetch を示します。 またはその他の外部呼び出しの使用 fetch の詳細については、「 Office スクリプトでの外部 API 呼び出しのサポート」を参照してください。 GitHub API から返されるものなど、 JSON オブジェクトの操作については、「 JSON を使用して Office スクリプトとの間でデータを渡す」を参照してください。

GitHub API リファレンスで使用されている GitHub API の詳細については、こちらをご覧ください。 Web ブラウザーにアクセス https://api.github.com/users/{USERNAME}/repos して生の API 呼び出しの出力を確認することもできます ({USERNAME} プレースホルダーは必ず GitHub ID に置き換えてください)。

リポジトリ情報の取得の例

サンプル コード: ユーザーの GitHub リポジトリに関する基本情報を取得する

async function main(workbook: ExcelScript.Workbook) {
  // Call the GitHub REST API.
  // Replace the {USERNAME} placeholder with your GitHub username.
  const response = await fetch('https://api.github.com/users/{USERNAME}/repos');
  const repos: Repository[] = await response.json();

  // Create an array to hold the returned values.
  const rows: (string | boolean | number)[][] = [];

  // Convert each repository block into a row.
  for (let repo of repos) {
    rows.push([repo.id, repo.name, repo.license?.name, repo.license?.url]);
  }
  // Create a header row.
  const sheet = workbook.getActiveWorksheet();
  sheet.getRange('A1:D1').setValues([["ID", "Name", "License Name", "License URL"]]);

  // Add the data to the current worksheet, starting at "A2".
  const range = sheet.getRange('A2').getResizedRange(rows.length - 1, rows[0].length - 1);
  range.setValues(rows);
}

// An interface matching the returned JSON for a GitHub repository.
interface Repository {
  name: string,
  id: string,
  license?: License
}

// An interface matching the returned JSON for a GitHub repo license.
interface License {
  name: string,
  url: string
}

トレーニング ビデオ: 外部 API 呼び出しを行う方法

このサンプルを YouTube で見てください