Apps Script Basics – Making quizzes in Google Forms

In this post we’re going to look at how you can set up a Google Forms quiz with Apps Script. As an example, I’m going to take you through the example that was posted on the G Suite Developers blog here. I’ve tweaked it a little just to create a new form instead of editing an existing one.

We’re going to set up a simple one checkbox question quiz, which will contain the points for a correct answer and will then give the user feedback on their answer. If the answer is correct they can see confirmation of this, and if it’s wrong they will get a link to a Wikipedia page helping them get the question right.

As you’ll see, it’s really easy to set up and could then be easily adapted for other quizzes.


The Code

Instead of writing the code in the Script editor connected to a form or a sheet, we’re going to create a standalone script in our Drive. In Drive, click on New.

Then select More > Google Apps Script.

This will create a script project in your My Drive and will open the script editor.

Alternatively, you can just type script.new in the Chrome address bar and this will also create a script project in your My Drive.

1.	function createGradedCheckboxQuestionWithAutofeedback() {
2.	  // Make sure the form is a quiz.
3.	  const form = FormApp.create("Quiz");
4.	  form.setIsQuiz(true);

First, let’s create a new form.

Line 1: Set up the function.

Line 3: Create the new form and call it “Quiz”.

Line 4: We then need to set the form up as a quiz using the setIsQuiz() method and stating true in the brackets.

Now, let’s set up the question.

6.	  // Make a 10 point question and set up the question
7.	  const item = form.addCheckboxItem();
8.	  item.setTitle("What flavors are in neapolitan ice cream?");
9.	  item.setPoints(10);

Line 7: Add the checkbox question, using addCheckboxItem() and store it in item.

Line 8: Give the question a title.

Line 9: Set the points value you want the question to have. Here, it’s going to be a 10 pointer.

Now, create the choices for the questions and state which ones are correct and which are incorrect.

11.	  // chocolate, vanilla, & strawberry are the correct answers
12.	  item.setChoices([
13.	    item.createChoice("chocolate", true),
14.	    item.createChoice("vanilla", true),
15.	    item.createChoice("rum raisin", false),
16.	    item.createChoice("strawberry", true),
17.	    item.createChoice("mint", false)
18.	  ]);

Line 12: Set up the choices with setChoices and in the brackets, use the square brackets, as we’ve got an array of items for this question.

Line 13: We use createChoice() for each option and it carries 2 arguments: Option text and a boolean, true or false. ‘true’ means the option is a correct one and ‘false’ if it is incorrect. End the line with a comma as it’s connecting it to the next line.

Note, to get the question correct, all the correct options need to be selected.

Lines 14-17: Repeat the same for each option. Don’t add a comma after the last option.

Line 18: Close the array and brackets.

Now, we need to give some feedback to the user if the answer is correct or incorrect.

20.	  // If the respondent answers correctly, 
21.	  // they'll see this feedback when they view scores.
22.	  const correctFeedback = FormApp.createFeedback()
23.	     .setText("You're an ice cream expert!")
24.	     .build();
25.	item.setFeedbackForCorrect(correctFeedback);

Line 22: First, we create the feedback using createFeedback(). Then we chain a couple of methods to it.

Line 23: First, we state what text we want to show in the feedback, using setText().

Line 24: Then we ‘build’ the feedback.

Line 25: Finally, we need to set the feedback we’ve just built to the correct feedback. Just pass the variable correctFeedback inside the brackets.

We then go through a similar process for an incorrect answer. But this time we’re going to give the user some help and add a link to page where they can find out more information about the ice-cream.

27.	  // If they respond incorrectly, they'll see this feedback with 
28.	  // helpful link to read more about ice cream.
29.	  const incorrectFeedback = FormApp.createFeedback()
30.	      .setText("Sorry, wrong answer")
31.	      .addLink(
32.	        "https://en.wikipedia.org/wiki/Neapolitan_ice_cream",
33.	"Read more")
34.	      .build();
35.	item.setFeedbackForIncorrect(incorrectFeedback);
36.	}

Line 29: Set up the feedback as before, this time we’ll store it in the variable incorrectFeedback.

Line 30: Set the feedback text.

Lines 31-33: We use addLink() to add a link in the feedback. In the brackets, there are 2 arguments. One is the URL in speech marks. And secondly, the link text we want to show in the feedback message, rather than an ugly, long URL.

Line 34: Build the feedback.

Lines 35-36: Set it for the incorrect feedback, then close the function.

Run the code and authorize it.

The new form will appear in your My Drive.

Opening it, we can see that it has added the question, added the points, selected which questions are correct. Click on “Answer key”.

Here we can see the points score and feedback messages for a correct or incorrect answer.

I want the user to be able to see the feedback as soon as they have submitted the form, so I need to select the option to release the mark immediately. You could also, add an email question and then select the option for them to receive the mark and feedback in an email.

Click on the Settings cog.

Then click on Quizzes.

Select “Immediately after each submission”.

Then click Save.

Click on the eye icon to open the form in the user view. All the user will see is the question and the 5 options.

If we tick the correct ones and submit the form, we’ll see the confirmation message. Then click “View accuracy”.

As we can see, it’s shows the answers I chose and gives the correct feedback.

If I answered the question incorrectly, when I click on “View your accuracy” I receive the incorrect feedback and I can see the link that was set up earlier.

Clicking on the link, takes me straight to the Wikipedia page.

See my post on manually making quizzes in Google Forms.


This post is taken from my book “Beginner’s Guide to Google Apps Script 2 – Forms“, available on Amazon here.

Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2

12 comments

  1. Apologies for posting about a different app script here but there was no way to leave a reply on the “Multiple Folder Maker” blog. I’m trying to adapt your script to make folders. I tested it and it said that it made the folders but I can’t find them anywhere. Any hints for me? Does it have something to do with folder sharing?

    1. Did you paste a new folder URL in cell C3? You need to open the folder you want the folders to be created in first, in Drive, then copy the URL and paste it into cell C3

      1. Thanks for the reply. I had some of the code wrong and have now been able to do part of what I want to do. My next step to figure out is to copy a file and rename it in the new folders that are being created. Any help with that would be appreciated.

        I’ve modified your script to create folders based on spreadsheet info that is created from a form. What I ultimately want the script to do is make a folder based on information that is entered via a form and copy an empty spreadsheet from one location and rename it in the new folders.

  2. You can make a copy of the file and then set its destination using makeCopy(). Try something like this:
    var folderId = DriveApp.createFolder(‘folderName’).getId;
    var folder = DriveApp.getFolderById(folderId);
    var file = DriveApp.getFileById(‘fileId’);
    var newFile = file.makeCopy(“filename”, folder);

  3. I will be moving more to google forms. Thank you for the introduction. Is it possible to set the solutions to
    textItems
    checkBoxGrid
    and gridItem

    Like one does for multipleChoice items and chekbox items.

    For checkBoxGrid and gridItem it appears as though points are assigned for each row, how does one do that from the app script?

    1. Hi-I think you can do it with the text item as there is the option to set points but as far as I know it’s not possible for the grids. For that it would have to be done within Sheets, but that’s completely different.

  4. Thanks for this post! I am working on something similar. I have been able to make the form with the quiz items, including marking the correct answers via apps script. However I also want to add a NavigationItem option to the list of choices. If they get the wrong answer I take them to a remedial page which talks about what they missed, then that page takes them to the next question. If they answered correctly they skip the remedial page and just go to the next page.
    The createChoice method seems to have 4 options:
    createChoice(value)
    createChoice(value, isCorrect)
    createChoice(value, navigationItem)
    createChoice(value, navigationType)

    What I want to do is something like: createChoice(value, isCorrect, navigationItem) so that when I set the form as a Quiz it will automatically grade the quiz as well as provide the navigation as mentioned above.

    It’s worth noting that I can manually edit the form to achieve my results, but I can’t quite figure out how to do it with apps script. Can you help?

  5. Compliments for your lesson. I’m trying to write a code to update a multiple choice question. In the creation of the choices the code is able to set the correct answer or to set the navigation page to go. My problem is that i would like to define both for the same choice. Could you give me a suggestion for the code?

    1. Hi-To be honest I’m not sure how you could do that as the createChoice() method either takes isCorrect or navigationType as values but not both. One way around it is to do the calculation on the Sheet side with the form submission.

  6. Hello Nice post,

    Thank you for the information, I have a question though, I am trying to adapt part of your code about the feedback for incorrect but I am struggling there. I want to import the feedback for each question that is present in the spreadsheet to it’s feedback of it’s respective question in google forms, I am not sure if that is possible or not, I used the following code but it seems not to be able to import the information from column G (which has the feedback to the answers), can you help with that , here is the part of the code related to the feedback:

    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();

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

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

    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(‘Fast Track Question – Domain I’);

    form.setIsQuiz(true);

    // 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])

    ]);

    var incorrectFeedback = FormApp.createFeedback()
    .setText(myfeedback[i][7])
    .build();
    addItem.setFeedbackForIncorrect(incorrectFeedback);

    I can send you the whole code but I think that would be a long post, if it's not clear maybe we can discuss that on mail or something

    Thanks inadvance

Comments are closed.