Googleフォームの全設問の正解をまとめて抽出!記述式にも対応した万能スクリプト(Extract All Correct Answers from Google Forms – Including Multiple Types!)

このスクリプト extractCorrectAnswers() は、Googleフォームに含まれるすべての設問について、「問題タイトル」と「正解」を一覧で抽出・表示してくれるツールです。
対応形式は以下のとおり:

  • ラジオボタン形式(Multiple Choice)
  • チェックボックス形式(Checkbox)
  • ドロップダウン形式(List)
  • 記述式(Text)にも対応(注:正解設定は不可)

各設問の番号、タイトル、正解(または補足メッセージ)をコンソールに出力します。正解設定がされていない形式でも状況が分かるため、問題管理やレビュー作業に非常に便利です。


🛠 スクリプトの書き方・場所

📌 手順:スクリプトの貼り付け

  1. Googleフォームを開く
  2. 右上の「︙(縦3点)」メニューから 「スクリプトエディタ」 を選択
  3. Google Apps Scriptの画面が開くので、既存コードを削除し、以下のコードを貼り付け
function extractCorrectAnswers() {
  const form = FormApp.getActiveForm();
  const items = form.getItems();
  const correctAnswers = [];

  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    const title = item.getTitle();
    let answerText = "";
    const type = item.getType();

    if (type === FormApp.ItemType.MULTIPLE_CHOICE) {
      const mcItem = item.asMultipleChoiceItem();
      const choices = mcItem.getChoices();
      for (const choice of choices) {
        if (choice.isCorrectAnswer()) {
          answerText = choice.getValue();
          break;
        }
      }
    } else if (type === FormApp.ItemType.CHECKBOX) {
      const cbItem = item.asCheckboxItem();
      const choices = cbItem.getChoices();
      const corrects = choices.filter(c => c.isCorrectAnswer()).map(c => c.getValue());
      answerText = corrects.join(", ");
    } else if (type === FormApp.ItemType.LIST) {
      const listItem = item.asListItem();
      const choices = listItem.getChoices();
      for (const choice of choices) {
        if (choice.isCorrectAnswer()) {
          answerText = choice.getValue();
          break;
        }
      }
    } else if (type === FormApp.ItemType.TEXT) {
      answerText = "(記述式・正解は設定不可)";
    } else {
      answerText = "(対応していない形式)";
    }

    correctAnswers.push([i + 1, title, answerText]);
  }

  console.log("=== 正解一覧 ===");
  correctAnswers.forEach(row => {
    console.log(`${row[0]}|${row[1]}|正解:${row[2]}`);
  });
}

▶ 実行方法

  1. スクリプトエディタ画面上部の「関数を選択」から extractCorrectAnswers を選ぶ
  2. ▶(再生)ボタンをクリック
  3. 初回実行時は権限認証を求められるので、内容を確認し許可する
  4. メニューの「表示」→「ログ」から、正解リストを確認!

💡 使いどころ・応用例

  • フォームで作成したクイズの答え合わせリストを一括生成
  • 自作問題集の正解一覧を出力して教材作成を効率化
  • 複数人で問題レビューを行う際の確認ツールとして
  • (応用)スプレッドシートに書き出す処理も追加可能!
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

GoodMorning!

デジタルの海原を冒険しながら、美食の宝を探し求める探検家です。テクノロジーの世界を舞台に、新しい発見を求めて、キーボードの海を横断。そして、実世界では、隅々まで足を運んで、舌鼓を打つ価値のある美味しいお店を見つけ出します。

私の使命は、小さなITの豆知識から始まり、心を満たすグルメスポットの紹介まで、あなたの日常にちょっとしたスパイスを加えること。画面の向こう側から、気軽に楽しめる話題を届けたいのです。ここでは、私が「これは!」と思った技術的な小話や、舌の記憶に残るような食べ物屋さんを紹介していきます。

このWebサイトは、ITとグルメ、二つの世界を融合させた、まさにデジタルと現実の融合点。ふらっと立ち寄って、新たな発見や、ほっこりするような話題で一息ついていただけたら幸いです。知識の海を冒険し、味覚の旅を楽しみましょう。毎日を少しだけ特別なものに変える、そんな情報をお届けします。

GoodMorning!

I am an explorer who ventures across the digital sea in search of gastronomic treasures. In the world of technology, I traverse the sea of keyboards in search of new discoveries. And in the real world, I visit every nook and cranny to find a delicious restaurant worth tantalizing your taste buds.

My mission is to add a little spice to your everyday life, starting with little IT tidbits and ending with foodie spots that fill your heart. I want to bring you topics that you can easily enjoy from the other side of the screen. Here, I'm going to share with you some of the technical tidbits and I will introduce small technical stories and food shops that will leave a lasting impression on your taste buds.

This Web site is truly a fusion point of digital and reality, combining the two worlds of IT and gourmet. I hope you will stop by and take a breather with new discoveries and dusty topics. Come explore the sea of knowledge and enjoy a journey of taste. I will bring you the information that will change your everyday life into something a little more special.

目次