In this post, we’re going to look at how to create a Google Form with a multiple-choice question. We’ll look how you can create the form from data in the script, then look how it can be done taking data from a Google Sheet. Then we’ll look at how we can update the question with data from a Sheet.


Creating a Google Form with a multiple-choice question

Here we’re going to create a new form called “New form” and add the question “Where do you want to go on holiday?” and add two options “Seville” and “London”.

Let’s look at the code line by line:

Line 1: Set up a function called createForm.

Line 2: Use FormApp class then the method create() with the title of the form in the brackets to create a new form. To then work with it, we store it in the variable form.

Line 3: Now let’s add the multiple-choice question. We get the form and then use the addMultipleChoiceItem() method. We then store this in the variable formQ1, to then add the question and possible options.

Line 4: To add a question, we get the ‘multiple-choice item’ and use setTitle() to set the question.

Lines 5-6: Finally, we need to set the choices that will appear for the question. Use setChoiceValues() and in the brackets list the options as an array with a comma between each option. Close the function in line 6.

Run the createForm function.

The first time you run it, it will ask for authorisation as you’re going to be adding a file to your Google Drive. Click “Review Permissions”.

Then select the account you want to use. Normally, you’ll just click the one you’re currently using.

Finally, click “Allow” to run the script.

This create a new form and stores it in your My Drive.

Opening the form, we can see it’s given the form a title, added a multiple-choice question and added 2 options.

Opening the form view we can see the question we’ve set up.


Creating a Google Form with data from a Google Sheet

Often you don’t want to have to ‘hard-wire’ the form questions and options, i.e. adding all that information within the script. A more flexible way, is to use a Google Sheet and write the question and options there and then get the script to read that information and add it to your form.

Here, we have a page called “newQ”.

On the page in column A, we have a question and two options.

The script below is going to get the spreadsheet, get the question and options, then create a form, with a multiple-choice question, using the question and options info from the sheet.

Let’s look at it line by line:

Line 8: Create a function called “createFormFromData”.

Line 9: Get the active spreadsheet and get the sheet called “newQ”. See earlier posts on working with spreadsheets.

Line 10: Get the question from cell A2 (i.e. row 2, column 1) and store it in the variable question.

Line 11: Get the options from cells A4 and A5 (i.e. rows 4 to 5, column 1) and store it in the array options.

Line 13: As we saw before, we use FormApp to create the new form.

Line 14: Again as before, we need to add a multiple-choice question.

Line 15: We set the title of the question using the variable question.

Lines16-17: Set the choices using the array options, then close the function.

Run the function “createFormFromData”.

This time as we’re accessing both the spreadsheet and creating a form, there is extra security and the authorisation is highlighting that the app hasn’t been verified by Google. To get around this, click “Advanced”.

 

Then click “Go to 11FormApp (unsafe)”. It looks scary but it’s just warning you of potential danger. Slightly annoyingly, this appears even if you are the developer and have written the code.

Type in “continue” to prove you’re not a robot and click “Next”.

Then click “Allow”.

The same as before, we get a new form with the question added.


Updating a question in a Google Form from data in a Google Sheet

We can use similar code to update an already existing Form and change the question and options within that form.

This time our question and options are on a page called “updateQ”.

Here, we have a different question and 2 different options.

The script is going to get the question and option info as we did before, then it’s going to open an existing form, then change the first question to the information we collected from the Sheet.

Let’s look at it line by line:

Line 19: Set up the function.

Line 20: Get the sheet called “updateQ”.

Line 21: Get the question.

Line 22: Get the options.

Line 24: Get the existing form by opening it by its ID and store it in the form variable. You could do the same by getting the form’s URL with openByUrl(). Obviously, use your own form ID here.

Line 25: This time we can’t just add a multiple-choice item, as we would just create a second question. Instead, we first need to get the number of items in our form. To do so, we use getItems(). It get the items as an array. Here, I’ve stored it in allItems.

Line 26: Now we need to get our question. It’s the first question, so this will be position 0 in our allItems array. We need to state what type of question we want. So, here we get it as a multiple-choice item.

Line 27: Then, we set the question title as before using setTitle() and the question variable.

Lines 28-29: Then we set the options as before using setChoiceValues() and the options variable. Close the function.

Run the function “updateFormFromData”.

As we can see it’s changed the question and options in our previous form.

So, as you can see, it’s very easy to create questions in a form and to update them from data stored in a Google Sheet.


You can make a copy of the Google Sheet with the code here.

You can find the code on GitHub here.


eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Baz Roberts (Google+Flipboard / Twitter)