Categories: Apps ScriptForms

Add Timer to Google Form Quiz

Finally, I fund a way to add a timer to a google form, and yes, it forces the user to submit the form after a given time. It’s not like all the other plugins that will only close the form to not accept responses and the users that are in the process of filling in the form will lose their answers. Imagine the student who spends an hour answering the question to be only 1 minute late.

This solution will be very useful to educators that use Google Form to create on-line Quiz and need to add a time-frame. I know, you can always check the time of the form submission manually and dismiss the ones that are late. But I don’t want to dismiss all the answers from the student just because he was 15 min late.

So, by implementing this solution you can set the deadline time and the script will make sure that the student can’t answer another question and can only submit the responses. Also, you don’t need to set a deadline, you can manually run the script and end the form.

Check the GIF to see how the solution works:

The solution is very easy to implement, you just need to insert a few lines of code into the script editor and set up a trigger. But first, check the way you need to create or adapt your Form.

Google Form

In order for the timer script to work, you will need to create your form in, maybe, a little bit different way. Why I use the word maybe, it’s probably because you already use this kind of approach to create your google form quiz.

The way you need to create your form is to use a section for each question. So, if you are not already doing that, you must divide the questions into separate section, like in the image below:

This will not only make the solution work as intended, but it can make your form more readable and accessible. Especially if you enable the option “Show progress bar” from the “Presentation” tab in the form settings. Check here for other options available to Google Forms: Google Forms Settings

Now, the next thing is to insert the script.

The Script

The script is very simple, it contains only 8 lines of code, but when executed it changes the “After section” on all of the sections from “Continue to next section” to “Submit form”.

So, after the script executes, when the user presses the “Next” button it will submit the form or it will give him only the option to Submit the form. It would not be possible to enter a new section/question by pressing the Next button.

Here is the script:

function closeForm() {
  var activeForm = FormApp.getActiveForm();
  var sectionItems = activeForm.getItems(FormApp.ItemType.PAGE_BREAK);
  for (var i=0; i < sectionItems.length; i++) {
    var pageBreak = sectionItems[i].asPageBreakItem();
    pageBreak.setGoToPage(FormApp.PageNavigationType.SUBMIT);
  }
}

To insert the script, access the Script editor from the three-dot menu, and copy/paste the script there.

Now, you can run the script, just to accept the permissions and to check if it works. If everything is fine, the script will change all the sections to “Submit form”. To check this go back to your Form and see if all the sections are changed.

Notice

In order to use the Form after the script executes, you will need to change back the “After section” option to “Continue to next section”. In the last section “Script to Reset Form”, you can find a script to change them back automatically.

As I said earlier, this script will change all the sections to “Submit form”, so by executing it will stop the user to enter another question/section and force him to submit. So, to set up a time frame on your Form you just need to control the time of the execution of the script. There are two ways of doing this:

  • Manually (by executing the script from the script editor) or
  • By using a Time-driven trigger

If you decide to manually stop the Form, you can just execute the script at any time, hopefully, the time you told your students to send the form. The script will change all the sections to “Submit form” and the user will submit the form when he presses the “Next” button.

If you want the quiz/form to stop (script to execute) at a given time you will need to:

Add a time-driven trigger

Let’s say you have scheduled the exam tomorrow 8/24/2020, from 12:00 to 13:00 h, so you need to stop the exam on 8/24/2020 at 13:00. To set up this you will need to add a time-driven trigger to execute the script at a “Specific date and time”. So, in your script editor, click on the “Current project’s triggers” toolbar icon, and in the projects triggers window click on the button “+ Add trigger”. This will open a dialog box where you can set up your trigger.

Now, you will need to select “Time-driven” for the “Select event source” and select “Specific date and time” for the “Select type of time based trigger” option. And in the next text box enter the time when you would like your script to execute (stop the quiz). Check the image below:

Before entering the time, check the GMT if it’s for your time zone. If it’s not, you have to adapt for the time or you will need to change the GMT in your Google Calendar settings.

Click “Save” and that’s it, now you have a timer that will close the Google Form automatically.

Changing the Form/Quiz won’t affect the script, so you can go ahead and add more or remove some questions, but in order to be 100% efficient, always use sections for each question. And remember, when the script executes it changes all the sections, so to reuse your form or test it, you will need to change them back to “Continue to next section”

Script to Reset Form

On a suggestion from Adam Davis, a Diamond Product Expert on Google Docs community help forums, I have included an extra function that can help you reset back the Form so you don’t have to manually change the sections back to “Continue to next section”. This is very helpful if you have a lot of questions/sections in your form and changing them manually can get overwhelming.

Here is the script:

function resetForm() {
  var activeForm = FormApp.getActiveForm();
  var sectionItems = activeForm.getItems(FormApp.ItemType.PAGE_BREAK);
  for (var i=0; i < sectionItems.length; i++) {
    var pageBreak = sectionItems[i].asPageBreakItem();
    pageBreak.setGoToPage(FormApp.PageNavigationType.CONTINUE);
  }
}

Add this function after the previous one.

How to run this script

After you have run the previous script and you want to reuse your form just select this function “resetForm” from the dropdown list and press on the Run button. Check the image:

If you fund this solution useful and it’s working for you, drop me a comment and share it with your peers.

SavoE

I'm an IT Manager and a big Google fan. In my free time, I like to write articles for How to GApps and help people on Google help communities (My Profile) and Quora. Check the About page for more. If you appreciate my work, maybe you can help me pay my hosting bill on Buy Me a Coffee.

View Comments

  • Hi there, this seems helpful. I just want to ask if its possible to put a trigger that can automatically enable response at a given time. Do you have some set of codes that I can work with? Thanks in this really nice piece of work. :)

    • Hi Jeff,

      You can accomplish that the same way you can use a trigger to close the form. So, you can set up a trigger to run the "resetForm" function at a given time, for example at 1 PM, and set a trigger to close the form, the "closeForm" function, at 2 PM. Hope this is what you are looking for.

      Best regards.

  • Can you tell me how it will work at different times for different users if trigger time is 30 minutes and not the specific time.

    • Hi Pooja,

      Because of Google Form limitations, you can not set a different time limit for users. You can only set the Form to close at a specific time or you can close the form manually by executing this script. Sorry about that.

      Best regards.

  • Hi, SavoE,

    I would like to implement a version that automatically submits the completed responses after a set amount of time--say 30 minutes. (i.e., so that once the quiz is started the form responses are automatically submitted after a set time.) Is this doable?

    • Hi LJ Ham,

      As far as I know, what you need can't be done. This solution is the closest thing to what you need, only it won't send the responses automatically and you can't set a 30 min timer. But, it will force the user to submit the answers (it can't answer any more questions) and it can be activated in a specific time (set the time 30 min from when they start).

      Best regards.

      • SavoE,

        Thank you for the response. I should have made more of a technical distinction with regard to the time settings of the trigger as well as the technical state of the form after closeForm is called. The technical state of the form after the call to closeForm works for me (user can no longer answer any more questions and can only submit the form) . How can can I code for any specific amount of minutes, lets say, 32, 45, or 54 minutes, after which the call to closeForm occurs? (i.e, other than preset 30 minute, 1 hour, etc. trigger settings?)

        Thanks again.

        • I did understand your question, but setting the time like that it's not possible. You can only set the exact time to execute the closeForm script, for example on 14:32 11/12/2020, so if you start the form on 14:00 11/12/2020 the users will have only 32 min. Until Google adds an onFormStart trigger, this is the only solution.

          Best regards.

  • I pasted the code in, and it changed each section to submit, but after I hit submit in the first section, it locked me out of the quiz so I couldn't finish the test. How do I make it so that the student's responses are entered after each section so that when the time runs out, the parts that the student did get completed are submitted to me. I want to have a grade for the stuff she did complete, rather than a zero percent on the entire thing because she didn't beat the timer.

    • Hi Heather,

      The script changes all the sections to submit when you execute it, either manually or by a trigger at a given time. So, by executing the script you stop the exam.
      Before the script is executed the students can answer the questions. Once the script executes, students can only submit the answers, so all the answers that were answered will be sent to you.
      You can check the video at the beginning of the article.
      But, in order to use the form/quiz again, you must change back the sections to "next", maybe this is the problem that you are facing.

      Hope this helps. Best regards.

      • I'm not sure what problem I'm having. I pasted the code in the quiz. Then, I set a trigger for 3:37. I hit save and then the play button. The student started the quiz at 3:30. The first question required her to enter her name. As soon as she did, she hit the submit key. When she did that, she got locked out of the quiz and wasn't allowed to answer anymore questions, even though the time had not run out yet. Submit was the only option given. She didn't have the choice of choosing a next button. I'm not sure what I'm doing wrong. All of the questions are in separate sections. I copied and pasted the code. I followed the trigger instructions. Is there a step that I'm missing?

        • I see the problem, you have pressed the run (play) button. This means that you have executed the script manually before the student even started the exam and all the sections were changed to "submit". 
          So, don't press the "run" button, just set the trigger, hit save and that's it. Also, don't forget to reset all the sections back to "next".
          Best regards.

          • I've tried that also. When I do that, the trigger never shuts the test off It allows the student to answer all of the remaining questions.

          • I don't know, maybe it's some kind of problem with GMT, can you check that. I don't see any other problem, since the time based trigger fails to run. Can you check the "My executions" section in the Google Apps script dashboard: script.google.com, to see if there is an record for the trigger.
            For now, you can always run the script at a specific time and end the form/quiz.

          • I checked the my executions section in the Google Apps script dashboard, it says that the trigger was completed.

            Head closeForm EditorSep 28, 2020, 9:10:57 PM0.413 s Completed

            The GMT is set to 05:00 Chicago. I live in MO, so that should be the correct time zone. Can you think of anything else I could try or check? I appreciate all the responses you've given me.

          • Hi Heather,

            From the record you pasted I can see that the script was executed from the editor, not from the trigger. So, probably you made a mistake in setting up the trigger and it didn't start. Can you try again and set another trigger. You can also check the "My executions" if it executes, the type should be "Trigger".(check the image: https://prnt.sc/upyzpv)

          • I was able to get it to work 2 times, but now it's back to not shutting off the quiz.

            closeForm Trigger Sep 29, 2020, 9:23:54 AM1.827 s Completed

            I can't figuring out what I'm messing up. I'm pretty positive that I'm setting everything up the exact same way each time. I even made a entirely new quiz to try it with and it still didn't shut-off the quiz.

            I wasn't able to look at the diagram you sent yet. It's blocked on my computer. I emailed our tech guy to come unblock it, but I don't know if or when he'll do that for me.

          • If you got it to work, I can't see why it won't work again.
            Did you add the second function in the script editor, maybe when you add the trigger you are selecting the resetForm function. Check that too, you need to select the closeForm function.
            If I have time I will create a video on how to setup a time driven trigger, so check back.

            Best regards.

  • Hi.
    This seems maybe a bit beyond me, but I still might try it.
    If I'm understanding correctly, this will end the quiz after a certain, predetermined time.

    Is there a way to adjust it so that it just keeps track of how long it takes? So that they could then get a benchmark and try to improve each time?

    Thank you.
    JK

    • Hi JK,

      Yes, you did understand correctly, the script can be set-up to run in predetermined time and end the form. You can also run the script manually and end the form.
      Regarding your second question, it's not possible to track the time since Google doesn't support this kind of trigger.

      It's very simple to implement the script so I don't think you will have any problems with it.

      Best regards.

Recent Posts

Google Form to Slides + Save to Drive and/or send it in email as a PDF

On your request, as an addition to the how-to: Google Form to PDF (save to Drive and/or send it in…

1 day ago

Google Form Script to Automatically Add New Choices from “Other:” Form Responses

Whit this Google Form Script you can Automatically Add the "Other:" response from the multiple-choice question or checkbox question to…

2 months ago

Google Form to PDF (save to Drive and/or send it in email)

This solution will create a PDF file from a Google Form submission using a Google Doc as a template. You…

3 months ago

Automated e-card sender – Google Spreadsheet Script to Send Birthday Cards via Email

Sending an email to wish your customers a happy birthday it's usually a thing that big companies do. There is…

4 months ago

Embed Google Form, Sheet, Chart and Doc in WordPress

You have a plugin for that too, but adding more plugins to your WordPress site can make it slower, and…

6 months ago

Google Form Script to AutoFill and Email a Doc Template

I have used a similar solution for creating documents from a template, but it was a spreadsheet script and used…

10 months ago