Record Time taken to complete a google form - google-apps-script

I am trying to record the total time taken to complete and submit a Google form.
My logic is simple that the following code would record and put the timestamp as a multiple choice option.
Then upon submitting the form, we get a time stamp anyway but along with that, we would get the initially recorded timestamp as an answer to that question.
This is my cute little code:
function initial() {
var form = FormApp.getActiveForm();
form.getItemById(1589920082).asMultipleChoiceItem().setChoiceValues([new Date()]);
}
I have set the trigger as OnOpen but surprisingly, it does not renew the timestamp every time I open the form. Theoretically, it should record the time when the form was open but I think I am missing something here.
On the other hand, if I change the trigger to OnSubmit, it starts recording fresh timestamps every time I submit the form. But I don't want that as we get submission timestamps anyway. What I am trying to record is the time the form was open.

I know this question was asked a year ago, but here is a workaround I just came up with.
Have 2 forms! The first form can be a disclaimer, instructions, etc. The second form is the original form you intended for the users to fill out. Turn on email collection for both, so you can know which user submitted the test.
Get the sharing link and in the settings of the first form add the link in the confirmation message:
Then link both forms into a spreadsheet and you will see the time stamp of each submissions. From there you can easily do a calculation to determine the duration.

Unfortunately you cannot have any code executed when user opens form for filling in, as onOpen trigger is not supposed to be run in this case:
https://developers.google.com/apps-script/guides/triggers/#onopen
The onOpen() trigger runs automatically when a user opens a spreadsheet, document, or form that he or she has permission to edit. (The trigger does not run when responding to a form, only when opening the form to edit it.)
There is an open feature request in Google Issue Tracker to introduce some kind of onOpen trigger when form opened for response, you can "star" it to make it more possible to appear: https://issuetracker.google.com/issues/63985842

Related

onFormSubmit Trigger occurs twice randomly about every few weeks

Firstly, I want to say that this issue is different from all previous Form Submit Trigger errors with Google Apps script. This one triggers twice, while still having access to all form submit info of the previous form submission and triggers the program again.
Furthermore, the additional form submit trigger glitch does not add a row to the spreadsheet form responses, this is because the user isn't submitting the form twice, it's on Google for triggering it. This has happened randomly about 3 times in the past few months and it is extremely critical that my program only executes once for each job. After hours of research, I was unable to find a problem similar to mine. The problem also persisted even after adding the following line of code:
if(e.values && !e.values1){return;} //To fix error when onFormSubmit
triggers multiple times under one form submission //Doesn't work/do
anything 6/8/2020
Posting this for documentation purposes.
This behaviour was very recently reported on Issue Tracker, probably by the OP:
#158892709: onFormSubmit Trigger occurs twice randomly about every few weeks for one form submission
Anyone who might be experiencing this can click the star on the top left in order to subscribe to it and also to prioritize it.

Google Script MailApp - Setting Timer

Are there any possibilities that I can control how long it takes after a form has been submitted before it will send an email out?
Currently, my code is working with a onFormSubmit function. So whenever a form is submitted and I have google script that has IF statements which checks for a range of values.
I want to improve the code but I can't think of any for the past few days. My plan is to send an email lets say, 10 minutes after the form is submitted. Let say, the form submitted has typos in it and one of the values isn't supposed to trigger an email send but instead it does due to the typo.
So now, I want to edit that value to the correct value which is within the accepted range. If this is being edited within that 10 minutes and is being checked again that it is actually within the range, then the email will not be sent. Is there any way this can be done? I have read up a bit about the function onEdit but it does not really cater for my requirements.
A solution I can think of:
onFormSubmit run a Clock trigger builder that will, lets say after the chosen amount of time run a function. Within this function you can verify if an edit to the form has taken place (retrieve the form submit data from the destination spreadsheet) and send an email if required.

Multiple OnFormSubmit trigger to specific spreadsheet

So, we have this task where we have a Google Form where a user submits data as a sign up for a mentoring service that gets passed on to a Spreadsheet and the user gets an e-mail as a sort of like confirmation that the sign-up did indeed happen. During this, the mentor that the user specified also gets an e-mail with some of the information that the user has sent, and a link that redirects them to a pre-filled Google Form that contains some of the information that the user submitted, and a drop-down question that basically serves as an attendance check.
Now, all of that mentioned already works. The problem is, the way it is set-up is that there are 2 Google Forms that direct to the same spreadsheet, just in different tabs. If I try to use OnFormSubmit as a trigger for the functions that allows for the emails to be sent, what happens is both OnFormSubmit triggers occur at the same time. I've seen multiple solutions to this problem here but none of them seem to work for me. Is there a way to manually script the OnFormSubmit function to let it work only when a specific form is passed?

Google Form-side form trigger not firing

This is a trigger and script coded directly into the form rather than on the spreadsheet that gets the form, I'm not sure if that is why this isn't working or not.
The trigger is set:
Owned by: Me
Last Run: null
Deployment: Head
Event: From Form-On form submit
Function: sendEmailFromSheet
Things I've tried: having the person who originally owned the form create a trigger yesterday, me create one today.
Filling out the form DOES result in the answers on the result spreadsheet, but it doesn't fire this onSubmitForm trigger, and I have no idea why. Again, it isn't that the function is failing but that it isn't firing at all.
Given that the function label is 'sendEmailFromSheet' then I suspect that its waiting for the sheet to receive data. But because the function is in the form it will never run because your trigger references the form and not the sheet.
If you can show us the code behind it then we might could tell you better. But without knowing that part then I think that is the mostly reason.

How to get onFormSubmit to trigger automatically?

The "fix" (test_onformsubmit) code you gave, I have to manually run it every time there is new data in the spreadsheet. I was wanting it to automatically send the pdf to email when Form is submitted. Is there a way? Because the manual way runs the code exactly like its supposed to, but I want this as an automatic event so I don't have to do anything.
See parent thread of original problem/question
Read Understanding Triggers. This function is an Installable Trigger, so you need to set it up to run when a form is submitted. It's easy - I would have thought a Forms tutorial would have walked through it.
In the Script Editor:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (That's onFormSubmit(), in this case.)
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
From this point on, the function will be triggered whenever a form is submitted to the spreadsheet.
If you plan to share your script, each recipient will need to repeat these steps.
As an aside, you should change the email setting in your script, so it will work for ANYONE.
var email_address = Session.getActiveUser().getEmail();