GASでスプレッドシートから記述式とラジオボタンの回答が混じったフォームを自動で作成したい
Q&A
Closed
解決したいこと
プログラム素人で大変恐縮です。
見よう見真似で、次の様なプログラムを書きましたが、上手く動きません。
修正箇所を教えていただければ大変助かります。
スプレッドシートには、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+ '」の作成が完了しました');
};
自分で試したこと
コードを私がいじってもだめでした、、、
お力を貸していただければ助かります