PetrS
PetrS

Reputation: 33

Google form for classroom

I have a script that generates a quiz from a Google spreadsheet of questions. Is it possible to programmatically assign a defined number of questions (for example 4), randomly selected from a google table of questions where there are, for example, 20 rows of questions? So that for each student there is a random selection of 4 questions from the table.

Upvotes: 1

Views: 44

Answers (1)

Nikko J.
Nikko J.

Reputation: 5543

Unfortunately, limiting the number of question and randomizing it to a single form is not possible. You need to create a new form for each set of questions.

Here I created a script that will create a form that contains 4 random question from this sheet.

enter image description here

Code:

function convertToForm(){

  const ss = SpreadsheetApp.getActive().getActiveSheet();
  const questionList = ss.getRange("A1:A20").getValues();

  const form = FormApp.create('TEST ONLY');

  var randNumArr = createRandNumArr();
  for( var i = 0; i < randNumArr.length; i++ ){
    const item = form.addTextItem();
    item.setTitle(questionList[randNumArr[i]])
  }

  Logger.log('Published URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());

}

function createRandNumArr(){
  var arr = [];
  while(arr.length < 4){
      var r = Math.floor(Math.random() * 20) + 1;
      if(arr.indexOf(r) === -1) arr.push(r);
  }
  return arr;
}

Example forms:

enter image description here

enter image description here

Upvotes: 1

Related Questions