function createQuizForm_n() {
var spreadsheetId = '';
var sheetName = '';
var folderId = '';
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
// 1行目(ヘッダー)を除くデータを取得
var questionsData = data.slice(1);
var form = FormApp.create('小テスト');
form.setIsQuiz(true);
for (var i = 0; i < questionsData.length; i++){
var question = questionsData[i][0];
var choices = [questionsData[i][1], questionsData[i][2], questionsData[i][3], questionsData[i][4]];
var correctAnswer = questionsData[i][5];
var points = questionsData[i][6]; // G列の配点を取得
if (question === '' || question === undefined) {
continue;
}
if (correctAnswer === '' || correctAnswer === undefined) {
continue;
}
var item = form.addMultipleChoiceItem();
item.setTitle(question.toString());
// 選択肢をランダムに並び替え
choices = shuffleArray(choices);
var uniqueChoices = [];
var choiceValues = {}; // 各質問ごとに選択肢の一意性を確認するためにリセット
for (var j = 0; j < choices.length; j++) {
var choice = choices[j];
if (choice !== '' && choice !== undefined && !choiceValues[choice]) {
uniqueChoices.push(choice);
choiceValues[choice] = true;
}
}
if (uniqueChoices.length < 2) {
continue;
}
var choiceObjects = uniqueChoices.map(function(choice){
return item.createChoice(choice.toString(), choice === correctAnswer);
});
item.setChoices(choiceObjects)
.setRequired(true)
.setPoints(points || 1); // G列の配点を設定、デフォルトは1点
}
// 作成したフォームを指定のフォルダに移動
var file = DriveApp.getFileById(form.getId());
var folder = DriveApp.getFolderById(folderId);
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file); // ルートフォルダから削除
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function onOpen(){
SpreadsheetApp
.getActiveSpreadsheet()
.addMenu('追加メニュー', [
{name: '小テスト作成', functionName: 'createQuizForm'}
]);
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme