このスクリプトは、ユーザーの 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 リポジトリに関する基本情報を取得する
サンプル ブックで次のスクリプトを実行し、サンプルを自分で試してください。 コード エディターを開くには、[ Automate>New Script>Create in Code Editor]\(コード エディターで作成\) に移動し、既定のコードを実行するサンプル コードに置き換えて、[ 実行] を選択します。
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 呼び出しを行う方法
Office Scripts