Bob bomo
Bob bomo

Reputation: 31

Shuffle answer properties

I have to activate the properties to shuffle the question of a multiple choice type questoin, but I can't find the properties. I found this code that randomizes questions, but not the answers.

form.setShuffleQuestions(true); 

Image of the visual component

Upvotes: 3

Views: 608

Answers (3)

Tanaike
Tanaike

Reputation: 201388

Unfortunately, in the current sage, it seems that this cannot still be achieved by Google Forms service (FormApp).

But, On March 16, 2022, Google Forms API has been officially released. Ref By this, fortunately, I confirmed that your this question can be directly achieved using Google Forms API.

In the current stage, Google Forms API cannot be used with Advanced Google services. So it is required to do the following flow.

Usage:

1. Linking Google Cloud Platform Project to Google Apps Script Project for New IDE.

In order to use Forms API, please link Google Cloud Platform Project to Google Apps Script Project for New IDE, and please enable Forms API at the API console. Ref

When Google Forms API can be used with Advanced Google services, this process can be skipped.

2. Scope.

In order to use this sample script, please use the following scopes. Ref

  • https://www.googleapis.com/auth/script.external_request
  • https://www.googleapis.com/auth/forms
  • https://www.googleapis.com/auth/forms.body

When Google Forms API can be used with Advanced Google services, this process can be skipped.

3. Sample script:

Please copy and paste the following script to the script editor linked to Google Cloud Platform Project and save the script.

function myFunction() {
  const formTitle = "sample"; // This is a form title.

  // THis is an object for creating quizzes.
  const obj = [
    { "question": "sample question 1", "answers": ["answer1", "answer2", "answer3"], "correct": ["answer1"], "point": 1, "type": "RADIO" },
  ];

  // Create new Google Form and set as quize.
  const form = FormApp.create(formTitle);
  form.setIsQuiz(true).setTitle("Sample");
  // form.setShuffleQuestions(true); // If you want to shuffle questions, you can use this line.

  // Create request body for Google Forms API.
  const url = `https://forms.googleapis.com/v1/forms/${form.getId()}:batchUpdate`;
  const requests = obj.map(({ question, answers, correct, point, type }, index) => ({
    createItem: {
      item: {
        title: question,
        questionItem: { question: { choiceQuestion: { options: answers.map(value => ({ value })), shuffle: true, type }, grading: { correctAnswers: { answers: correct.map(e => ({ value: e })) }, pointValue: point } } },
      }, location: { index }
    }
  }));

  // Request to Google Forms API.
  const params = {
    method: "post",
    contentType: "application/json",
    headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() },
    payload: JSON.stringify({ requests }),
    muteHttpExceptions: true
  };
  const res = UrlFetchApp.fetch(url, params);
  console.log(res.getContentText())
}
  • When this script is run, as a sample, a new Google Form is created, and a sample question including the radio button is set. And, you can confirm that "Shuffle option order" is checked.

References:

Upvotes: 0

TheMaster
TheMaster

Reputation: 50452

Issuetracker:

This isn't currently possible. Consider adding a star (on top left) to the following feature requests for Google to prioritize the issue:

Partial worksround:

Partial Workaround as already mentioned in this answer is to shuffle the array creating options and setback the array using setChoiceValues(). The drawback of such server side randomizing is

  • It can only be done whenever the server script runs and not when client opens the form

  • Even if you randomize each minute, it is possible that users opening the form simultaneously will see the same order of options

Sample script:

const form = FormApp.openById('/*form id*/');
const item = form.addMultipleChoiceItem();
item.setTitle('Car or truck?');
const options = ['Truck', 'Car'];
//Durstenfeld algo
for (let i = options.length - 1; i > 0; i--) {
  let rand = Math.floor(Math.random() * i);
  [options[i], options[rand]] = [options[rand], options[i]];
}
item.setChoiceValues(options);

Upvotes: 2

Cooper
Cooper

Reputation: 64062

I believe that you have to randomize the options yourself with something like this:

function randomizeArray(A) {
  var iA=A.slice();
  var oA=[];
  for(var i=0;i<A.length;i++) {
    var index=Math.floor(Math.random()*iA.length);
    oA.push(iA[index]);
    iA.splice(index,1); 
  }
  return oA;
}

Upvotes: 0

Related Questions