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