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?

Google SheetsでChatGPT OpenAI APIを使って自動要約する方法

Last updated at Posted at 2025-02-23

はじめに

Google Sheetsで特定のセルの内容をGPT-4を使って自動要約する方法を紹介します。Google Apps ScriptとOpenAI APIを組み合わせることで、スプレッドシート上のテキストを効率的に要約することができます。

準備するもの

実装手順

1. スプレッドシートの準備

  1. 新しいGoogle Sheetsを作成します
  2. シート名を"GPT"に変更します
  3. A1セルにベースとなるプロンプトを入力します(例:「以下の内容を3行で要約してください:{content}」)

2. Google Apps Scriptの実装

スプレッドシートのメニューから「拡張機能」→「Apps Script」を選択し、以下のコードを貼り付けます:

function GPTCell(cellAddress) {
    const API_KEY = "your-api-key-here"; // ⚠️ APIキーを設定してください
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GPT");
    const basePrompt = sheet.getRange("A1").getValue();
    
    try {
        const cellValue = sheet.getRange(cellAddress).getValue();
        const prompt = basePrompt.replace("{content}", cellValue);
        const messages = [{ role: "user", content: prompt }];
        const payload = {
            model: "gpt-4o-mini-2024-07-18",  // または "gpt-3.5-turbo" など、利用したいモデルを指定
            messages: messages
        };
        const options = {
            contentType: "application/json",
            headers: { Authorization: "Bearer " + API_KEY },
            payload: JSON.stringify(payload),
            muteHttpExceptions: true
        };
        const url = "https://api.openai.com/v1/chat/completions";
        const response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
        const summary = response.choices[0].message.content.trim();
        return summary;
    } catch (e) {
        return `エラー: ${e.message}`;
    }
}

3. APIキーの設定と安全な管理 ⚠️

  1. OpenAIのウェブサイトからAPIキーを取得します
  2. コード内のyour-api-key-hereを取得したAPIキーに置き換えます
  3. 重要な注意事項:
    • APIキーは秘密情報です。絶対に公開しないでください
    • スクリプトを共有する際は、必ずAPIキーを削除してください
    • 理想的には、APIキーはスクリプトプロパティなどで管理することをお勧めします

APIキーをスクリプトプロパティとして設定する方法

より安全な実装のために、以下の手順でAPIキーを設定できます:

  1. Apps Scriptエディタで「プロジェクトの設定」を開く
  2. 「スクリプトプロパティ」タブを選択
  3. 新しいプロパティを追加:
    • プロパティ名:OPENAI_API_KEY
    • 値:あなたのAPIキー

そして、コードを以下のように修正:

const API_KEY = PropertiesService.getScriptProperties().getProperty('OPENAI_API_KEY');

使い方

  1. 要約したいテキストをスプレッドシートのセルに入力します
  2. 別のセルに以下のような数式を入力します:
    =GPTCell("B1")
    
    ※ B1は要約したいテキストが入っているセルのアドレスに置き換えてください

コードの解説

主な機能

  • GPTCell関数は指定されたセルの内容を取得し、GPT-4に要約を依頼します
  • A1セルのベースプロンプトを使用して、柔軟に指示を変更できます
  • エラーハンドリングにより、APIリクエスト失敗時にも適切なメッセージを返します

モデルの選択

OpenAIは複数のモデルを提供しています:

  • gpt-4: 最も高性能なモデル
  • gpt-3.5-turbo: コストパフォーマンスの良いモデル

用途と予算に応じて適切なモデルを選択してください。

注意点

  • OpenAI APIの利用には課金が発生します
  • API利用制限に注意してください
  • APIキーは絶対に公開しないでください
  • 共有や公開時は必ずAPIキーを削除してください

まとめ

このスクリプトを使用することで、Google Sheets上で簡単にGPT-4による要約機能を実装できます。ビジネスシーンでの文書要約や、大量のテキストデータの処理に活用できるでしょう。

プロンプトをカスタマイズすることで、要約以外にも様々な用途に応用可能です。例えば:

  • テキストの感情分析
  • キーポイントの抽出
  • 文章のリライト
  • 多言語翻訳

参考資料

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?