Create a Google Forms Quiz From Google Sheets With Script

 Making a quiz in google forms takes a lot of time while creating multiple choice questions and setting correct answers. It's very compact to create questions in google forms. So in this blog I am writing about how to make a quiz with help of google sheets and google script. By using this method we put questions and answers with multiple choice in google sheet and run google apps script this will create a new quiz in google form.So here i am describing how to make all the processes and providing all google script.

Create a Google Forms Quiz From Google Sheets



STEP - 1. Create a Google Sheet

First of all go to https://docs.google.com/spreadsheets/ and create a new sheet.


STEP - 2. Put Questions and Multiple Choices in Sheet

Questions and Multiple Choices in Sheet

After creating google sheet now time to put your all questions, correct answers with multiple options in sheet. As shown in the screenshot, put all questions in A:A column, correct answers in B:B column and put the other three options in C:E columns.



STEP - 3. Set Google Script In Apps Script

In Google Sheet now go to Apps Script by going to Extensions > Apps Script and copy and paste the below script in apps script.


function popForm() {

   var ss = SpreadsheetApp.getActive();

  // var sheet = ss.getActiveSheet();

  var sheet = ss.getSheetByName('Sheet1');

  var numberRows = sheet.getDataRange().getNumRows();

  // Read the sheet data into 3 arrays.  Would be better practice (faster performance) to read all into 1 array and divide as needed).  

  var myQuestions = sheet.getRange(1,1,numberRows,1).getValues();

  var myAnswers = sheet.getRange(1,2,numberRows,1).getValues();

  var myGuesses = sheet.getRange(1,2,numberRows,4).getValues();

  // Shuffle the 4 choices horizontally.  This script only works with Questions in col A, correct Answer in col B, and false choices in col C thru E.

  var myShuffled = myGuesses.map(shuffleEachRow);

  Logger.log(myShuffled);

  Logger.log(myAnswers);

  // Create the form as a quiz.  The resulting form's "Quiz options" are different from a manually created quiz.  Be aware (and change manually if needed!

var form = FormApp.create("New Quiz");

  form.setIsQuiz(true);

  form.setConfirmationMessage("𝗙𝗢𝗟𝗟𝗢𝗪 𝗨𝗦 : \n \n 𝗔𝗻𝗸𝗶𝘁 𝗬𝗮𝗱𝗮𝘃 - https://www.instagram.com/ankityadav.info/")

  form.setAllowResponseEdits(false

  // Write out each multiple choice question to the form.

  for(var i=0;i<numberRows;i++){

    if (myShuffled[i][0] = myAnswers[i][0]) {

      var addItem = form.addMultipleChoiceItem();

      addItem.setTitle(myQuestions[i][0])

      .setPoints(1)

      .setChoices([

        addItem.createChoice(myShuffled[i][0],true),

        addItem.createChoice(myShuffled[i][1]),

        addItem.createChoice(myShuffled[i][2]),

        addItem.createChoice(myShuffled[i][3]),      

      ]);

    }

    else if (myShuffled[i][1] == myAnswers[i][0]) {

      var addItem = form.addMultipleChoiceItem();

      addItem.setTitle(myQuestions[i][0])

      .setPoints(1)

      .setChoices([

        addItem.createChoice(myShuffled[i][0]),

        addItem.createChoice(myShuffled[i][1],true),

        addItem.createChoice(myShuffled[i][2]),

        addItem.createChoice(myShuffled[i][3]),

      ]);

    }

    else if (myShuffled[i][2] == myAnswers[i][0]) {

      var addItem = form.addMultipleChoiceItem();

      addItem.setTitle(myQuestions[i][0])

      .setPoints(1)

      .setChoices([

        addItem.createChoice(myShuffled[i][0]),

        addItem.createChoice(myShuffled[i][1]),

        addItem.createChoice(myShuffled[i][2],true),

        addItem.createChoice(myShuffled[i][3])

      ]);

    }

    else if (myShuffled[i][3] == myAnswers[i][0]) {

      var addItem = form.addMultipleChoiceItem();

      addItem.setTitle(myQuestions[i][0])

      .setPoints(1)

      .setChoices([

        addItem.createChoice(myShuffled[i][0]),

        addItem.createChoice(myShuffled[i][1]),

        addItem.createChoice(myShuffled[i][2]),

        addItem.createChoice(myShuffled[i][3],true),

      ]);

    }

    else {

      var addItem = form.addMultipleChoiceItem();

      addItem.setTitle(myQuestions[i][0])

      .setPoints(1)

      .setChoices([

        addItem.createChoice(myShuffled[i][0]),

        addItem.createChoice(myShuffled[i][1]),

        addItem.createChoice(myShuffled[i][2]),

        addItem.createChoice(myShuffled[i][3]),

      ]);

    }

  }

}

// This function, called by popForm, shuffles the 4 choices.

function shuffleEachRow(array) {

  var i, j, temp;

  for (i = array.length - 1; i > 0; i--) {

    j = Math.floor(Math.random() (i + 1));

    temp = array[i];

    array[i] = array[j];

    array[j] = temp;

  }

  return array;

}


Now save the script.



STEP - 4. Run The Google Script

After saving the script now time to run the script. Click on Run in google script. After clicking on Run google will ask to allow permissions to proceed.



STEP - 5. Open Quiz In Google Forms

Now go to https://docs.google.com/forms/ . Your quiz will be there in google form with the title of ‘New Quiz’.


Now you can change the title of the quiz and do your setting in the quiz.



If you want, I will create the sheet app for you with paid charges of approx Rupees 2000. I will also customise the sheet according to your necessity.


2 comments:

  1. Trying to use this, but hitting an error with this line: j = Math.floor(Math.random() (i + 1));

    generating a Type Error; operator is missing. Does this mean we need a division sign after math.random? or maybe modulo?

    ReplyDelete

Pages