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 is stored in the Script Editor in Forms, although could be added to a spreadsheet.
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.
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.
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 questions.
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.
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.
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 authorise 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, and contains the 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 and 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 your 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.
You can find the code here at GitHub.
See my post on manually making quizzes in Google Forms.
eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:
- “Beginner’s Guide to Google Drive” – iBooks store / Kindle store
- “Beginner’s Guide to Google Forms” - iBooks store / Kindle store
- "Beginner's Guide to Google Sheets" - iBooks store / Kindle store
- "Beginner's Guide to Google Docs" - iBooks Store / Kindle store
- "Beginner's Guide to Google Slides" - iBooks Store / Kindle store
- "Google Sheet Functions - A step-by-step guide" - iBooks Store / Kindle Store