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

Can I randomize items in MultipleChoiceItem?

我是 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]);
        }
                                 )
                      )

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

  • 为了在每次重新加载表单时随机排列您的值,您需要使用 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;
}

更新:

如果您想为每个用户提供相同的测验,但问题是随机的,则没有直接的方法 - 到目前为止还没有这种方法 setShuffleItems。 目前您所能做的就是解决方法,例如您可以绑定到 installable time-driven trigger 上方的示例,这将按所需间隔(最短为一分钟)随机播放项目。这并不能保证每个用户都会看到不同的测验,但至少每分钟测验都会有所不同。

正如所解释的那样,对此没有直接的答案,考虑到有一个选项可以通过前端界面 Shuffle option order ,这是相当令人惊讶的。 (see here)

对我来说,每 1 分钟触发一次重新洗牌的提议效率太低,并且不能确保每个用户的顺序都会改变。在我看来,一个不太糟糕的方法(虽然不是解决方案)是手动创建一个MultipleChoiceItem,检查随机播放选项顺序,然后duplicate the MultipleChoiceItem,而不是创建一个新的。

您需要一些硬编码的参考,例如 MultipleChoiceItemget the ID,但是一旦有了,您就可以根据需要不断地创建尽可能多的问题,这些问题将完全符合此要求问题问。

这是一个示例项目以及一个函数,用于显示表单中所有多选项目的 ID。它并不完美,但也许是另一种选择。

function makeShuffledQuestion() {
  const theIDofShuffleQuestion = 2???????1; //<--- must find by getID()

  const theShuffleQuestion = form.getItemById(theIDofShuffleQuestion);

  //next line use duplicate, or start loops to duplicate types that
  var mc = theShuffleQuestion.duplicate();
  mc.setTitle("This one is new!");
  mc.setPoints(1);
  mc.setChoices([
    mc.createChoice("alpha", fale),
    mc.createChoice("bravo", false),
    mc.createChoice("charlie", true),
    mc.createChoice("delta", false)
  ])
}

function listAllItemsID() {
  //function will display all multiplie choice items with title and ID
  var allItems = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
  for (var i = 0; i < allItems.length; i++) {
    Logger.log(allItems[i].getTitle() + "  ID:" + allItems[i].getId().toString());
  }
}