I'm new to GoogleAppsScript and now making quizzes in google form and spreadsheet by using GAS.
I want to shuffle items in a MultipleChoiceItem when the google form is reloaded.
A part of my current scirpt, slightly modified form this code, is presented below.
//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]);
}
)
)
Could someone please tell me a solution? Thanks for your help!
As explained there is no direct answer to this, which is rather surprising considering there is an option to Shuffle option order via the front end interface. (see here)
To me, the proposals of triggering reshuffles every 1 minute are too inefficient and it does not ensure that the order will change for each user. In my opinion, a less bad approach (though not a solution) would be to manually create a MultipleChoiceItem
that has the Shuffle option order checked, then duplicate the MultipleChoiceItem, rather than create a new one.
You would need to have some hard-coded reference such as get the ID for the MultipleChoiceItem
, but once you have that you could continually create as many questions as needed that would do exactly what this question asks.
Here's a sample item as well as a function to display the ID of all multiple-choice items in a form. It's not perfect, but perhaps an alternative.
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());
}
}