0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

第1回:【GAS×Gemini】スプレッドシートからAIを呼び出す最短ルート(環境構築編)

0
Last updated at Posted at 2026-07-14

はじめに

「スプレッドシートのデータをAIに投げたいけど、毎回コピペするのは面倒…」

そんな悩みを解決してくれるのが、Google Apps Script(GAS)Gemini API の組み合わせです。

このシリーズでは、GoogleスプレッドシートをAIが自動で処理してくれるツールへ変えていきます。

第1回では、

  • Gemini APIキーの取得
  • GASへの設定
  • AIから返答を受け取る

までを最短で実装します。


完成したスプレッドシート全体

スクリーンショット 2026-07-14 194423.png

Gemini APIとは?

Gemini APIはGoogleの生成AIをプログラムから利用するためのAPIです。

GASもGoogle製なので認証やGoogleサービスとの連携がしやすく、Gemini APIとも扱いやすく、

  • スプレッドシート
  • Gmail
  • Google Drive
  • Google Forms

などとも簡単に連携できます。


Step1 APIキーを取得する

Google AI Studioへアクセスします。

https://aistudio.google.com/

APIキーを発行してください。


Step2 GASを開く

スプレッドシートから

拡張機能
↓
Apps Script

を開きます。


Step3 APIキーはコードに書かない

セキュリティのため、

プロジェクトの設定
↓
スクリプトプロパティ

に保存します。

キー名は

GEMINI_API_KEY

とします。


Step4 テストコード

function testGemini() {
  const apiKey = PropertiesService.getScriptProperties()
    .getProperty("GEMINI_API_KEY");

  const model = "gemini-2.5-flash";

  const endpoint =
    `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;

  const requestBody = {
    contents: [{
      parts: [{
        text: "「こんにちは、GASから呼んでいます」と返事してください。"
      }]
    }]
  };

  const response = UrlFetchApp.fetch(endpoint, {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify(requestBody)
  });

  const result = JSON.parse(response.getContentText());

  Logger.log(result.candidates[0].content.parts[0].text);
}

実行してみる

実行するとログに

こんにちは!GASからの呼び出しを確認しました。

のような返答が表示されれば成功です。


今回のポイント

  • APIキーはスクリプトプロパティへ保存
  • UrlFetchAppでGemini APIを呼び出す
  • Logger.logで結果を確認

これだけでGASからAIを利用できます。


次回

第2回では、

「要約」「翻訳」「分類」をセルの入力だけで切り替える

task列方式を紹介します。

これを作ると、コードを書き換えることなく様々なAI処理ができるようになります。


現在、React + FastAPIで教育向けのデータ分析・機械学習プラットフォームを開発しています。

AI・機械学習・GAS関連の記事も継続して投稿予定です。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?