Googleスプレッドシートからアイウエ選択肢を自動生成する方法(同じフォルダにGoogleフォームを作成するGASコード付き)

授業や教材作成で、Googleフォームの4択問題を大量に作るのは非常に手間がかかります。

1問ずつ手で作ると、

  • 問題を作る
  • 選択肢を入力する
  • 正解を設定する

という作業を何十回も繰り返す必要があります。

そこで今回は、Googleスプレッドシートに入力した正解データをもとに、Googleフォームを自動で作成するGAS(Google Apps Script)を紹介します。

しかも、

  • スプレッドシートと同じフォルダに自動保存
  • クイズ形式(自動採点)
  • 正解付き4択問題を自動生成

という、実用レベルのコードです。

目次

できること

このプログラムを実行すると:

  • スプレッドシートのB列を読み取る
  • 各行の「ア・イ・ウ・エ」を正解として認識
  • Googleフォームに4択問題を自動作成
  • フォームは同じフォルダに保存される
  • クイズ形式で自動採点可能

スプレッドシート

プログラム

function createFormUnderSameFolder_NoNumber() {

  // ===== 設定 =====
  const SHEET_NAME = 'シート1';   // シート名
  const FORM_TITLE = '4択フォーム';
  const QUESTION_TITLE = '問題';   // ←固定(番号なし)
  const OPTIONS = ['ア', 'イ', 'ウ', 'エ'];

  // ===== スプレッドシート取得 =====
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(SHEET_NAME);
  const sheetFile = DriveApp.getFileById(ss.getId());

  // ===== 同じフォルダ取得 =====
  const parentFolders = sheetFile.getParents();
  if (!parentFolders.hasNext()) {
    throw new Error('親フォルダが見つかりません');
  }
  const parentFolder = parentFolders.next();

  // ===== フォーム作成 =====
  const form = FormApp.create(FORM_TITLE);
  form.setIsQuiz(true);

  // ===== フォームを同じフォルダに移動 =====
  const formFile = DriveApp.getFileById(form.getId());
  parentFolder.addFile(formFile);
  DriveApp.getRootFolder().removeFile(formFile);

  // ===== データ取得(B列基準)=====
  const lastRow = sheet.getLastRow();
  const data = sheet.getRange(2, 2, lastRow - 1, 1).getValues(); // B列のみ

  // ===== 問題作成 =====
  for (let i = 0; i < data.length; i++) {

    const answer = String(data[i][0] || '').trim();

    if (answer === '') continue;

    if (!OPTIONS.includes(answer)) continue;

    const item = form.addMultipleChoiceItem();

    item.setTitle(QUESTION_TITLE);  // ←完全に「問題」のみ
    item.setRequired(true);

    const choices = OPTIONS.map(opt =>
      item.createChoice(opt, opt === answer)
    );

    item.setChoices(choices);
    item.setPoints(1);
  }

  Logger.log("フォーム編集URL: " + form.getEditUrl());
  Logger.log("回答URL: " + form.getPublishedUrl());
}
よかったらシェアしてね!
  • 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.

目次