@john17 (章男 田中)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

GASでスプレッドシートから記述式とラジオボタンの回答が混じったフォームを自動で作成したい

解決したいこと

プログラム素人で大変恐縮です。
見よう見真似で、次の様なプログラムを書きましたが、上手く動きません。
修正箇所を教えていただければ大変助かります。

スプレッドシートには、5行目から質問事項等がはじまります。
A列:問題のタイトル
B列:問題の説明
C列:配点(記述式の場合は空欄)
D列:問題形式(記述式かラジオボタンが記入してます)
E列:正解の番号(記述式の場合は空欄)
F列:選択肢①(記述式の場合は空欄)
G列:選択肢② (記述式の場合は空欄)
H列:フィードバック(答えの解説など)(記述式の場合は空欄)

発生している問題・エラー

質問も作成されずに終了してしまう状況です。

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

function createForm() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('社員評価項目');

  const formTitle = sheet.getRange('B1').getDisplayValue();
  const formDescription = sheet.getRange('B2').getDisplayValue();

  const firstRow = 5;
  const lastRow = sheet.getLastRow();

  const dataRows = lastRow - (firstRow - 1);

  var questionList = sheet.getRange(firstRow, 1, dataRows, 10).getDisplayValues();

  questionList = questionList.map(question => {
    return {
      title: question[0],
      helpText: question[1],
      point: question[2],
      type: question[3],
      answer: Number(question[4]),
      choices: [question[5], question[6]],
      feedback: question[7]
    }
  });

  const form = FormApp.create(formTitle);

  form.setDescription(formDescription)
    .setIsQuiz(true);


  questionList.forEach(question => {

    if (question.type == 'ラジオボタン') {
      var choiceItem = form.addMultipleChoiceItem();

      var choiceList = [];

      question.choices.forEach((choice, index) => {

        if (choice != '') {

          let isCorrect = question.answer == String(index + 1);

          let choiceObj = choiceItem.createChoice(choice, isCorrect);

          choiceList.push(choiceObj);
        }

      });

      const feedback = FormApp.createFeedback().setText(question.feedback).build()

      choiceItem.setTitle(question.title)
        .setHelpText(question.helpText)
        .setPoints(question.point)
        .setChoices(choiceList)
        .setFeedbackForCorrect(feedback)
        .setFeedbackForIncorrect(feedback);

    }
    else if (question.type == '記述式') {
      var checkboxItem = form.addTextItem();

      checkboxItem.setTitle(question.title)
        .setHelpText(question.helpText)
        .setPoints(question.point);

    }
    else {
      Browser.msgBox('「' + question.title + '」は作成できない問題形式です');
    }
  });

  // 追加部ここから

  // 作成したフォームの編集用URLをB3セルに書き込み
  sheet.getRange('B3').setValue(form.getEditUrl());

  // メッセージボックスの表示
  Browser.msgBox('「' + formTitle+ '」の作成が完了しました');
};

自分で試したこと

コードを私がいじってもだめでした、、、

お力を貸していただければ助かります

0 likes

3Answer

スプレッドシートに2つ設問を入力し、記載のコードを手元で動かしたところ、正常に問題が設定されたフォームが Google ドライブ内に新規作成されました。
 
データ元となるスプレッドシートがうまく読み取れていない可能性があります。
 
1行目の

  const ss = SpreadsheetApp.getActiveSpreadsheet();

  const ss = SpreadsheetApp.openById('スプレッドシートID');

に変えて実行した場合、どうでしょうか。
(※上記「スプレッドシートID」の部分は、実際のフォーム用のデータが記入されているスプレッドシートのIDに変更してください。

「スプレッドシートID」とは、スプレッドシートのURLが
https://docs.google.com/spreadsheets/d/1Kxxx***************ao/edit#gid=0
とした場合の
「1Kxxx***************ao」の部分のことです)

0Like

ありがとうございます!
早速、変更してみたのですが、まだできず、、、、
お力を貸していただければ幸いです。

GASは動くのですが、出来上がりのフォームが
1つの質問しかできません

image.png

ちなみに、もとであるスプレッドシートはこんな感じです

image.png

image.png

0Like

再度見直して、単純なミスに気づき、できました!
ありがとうございます!

0Like

Your answer might help someone💌