我可以随机化MultipleChoiceItem中的项目吗?

问题描述 投票:0回答:1

我是GoogleAppsScript的新手,现在使用GAS在Google表单和电子表格中进行测验。

我想在Google表单重新加载后重新整理MultipleChoiceItem中的项目。

下面是我当前脚本的一部分,形式略微修改this code

//vars from spreadsheet
var form = FormApp.openById(id);
var ss = SpreadsheetApp.openById(question_bank_ID);
var text = sheet.getSheetValues(questions[i]+1, 2, 1, 1)[0][0];
var options = sheet.getSheetValues(questions[i]+1, 5, 1, 5)[0];
var ans = sheet2.getSheetValues(questions[i]+1, 5, 1, 5)[0];

//MultipleChoiceItem
var mc = form.addMultipleChoiceItem().setTitle(text);
        mc.setPoints(1) // set point 
         // add choices with isCorrect
        while (options[options.length - 1] === "") {
          options.pop();
        }
        mc.setChoices(options.map(function (options, i) {
          return mc.createChoice(options, ans[i]);
        }
                                 )
                      )

有人可以告诉我解决方案吗?感谢您的帮助!

google-apps-script google-form google-classroom
1个回答
0
投票
  • 为了在每次重新加载表单时重新整理值,您需要使用onOpen trigger将脚本绑定到表单上>
  • 检索所有问题,并为每个问题检索选择
  • 使用随机播放功能随机选择
  • 将打乱的选项分配回问题
  • 示例:

function onOpen(){
  form = FormApp.getActiveForm();
  var questions = form.getItems();
  for (var i =0; i < questions.length; i++){
    var question = questions[i];
    var choices = question.asMultipleChoiceItem().getChoices();
    var newChoices = shuffle(choices);
    question.asMultipleChoiceItem().setChoices(newChoices);
  }

}


function shuffle(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;
}
© www.soinside.com 2019 - 2024. All rights reserved.