Distinguish between forms when using "on form submit" - google-apps-script

With the new Google Sheets, responses from more than one form can be sent to the same spreadsheet. This is very convenient.
However, it appears that when using the On Form Submit trigger, there is no way to distinguish between forms. This creates a major problem.
For example, I have one form that users submit to signup for a mailing list. I have a second form that users use to submit reports. I have a script that takes the reports and sends them to the mailing list recipients according to their preferences. I need to correlate variables in the mailing list and the reports in order to send the reports according to preferences. I set up the script such that whenever a report is submitted, it is automatically sent. However, when a signup form is submitted, it is triggering the script and sending emails (using information from the signup form).
Is there a way to tell Google to only trigger the script when a specific form is submitted, not when any of the forms linked to the spreadsheet is submitted?
Thanks!

I have found a roundabout way to distinguish between forms through onFormSubmit(). Taking the fact that while there is a many-to-one relationship between Forms to a Spreadsheet, there is a one-to-one relation between a Form and a Sheet. Also, all forms append a row in their unique sheet for the response value. That row of data (as a Range) is passed as a value in e, as e.range. To get the URL of the form that was used, e.range.getSheet().getFormUrl() can be used. If the need was just to distinguish between forms, e.range.getSheet() could be used.
With that, a switch in your onFormSubmit() function could be added as a control flow towards specific (for each form) functions.
I was having the same concern. I hope this is still relevant, and for posterity sake.
TL;DR: e.range.getSheet()

Related

How can I trigger a Sheets script from a Google Form Submission?

I wrote a script for a Google Sheet that emails a user some specific info relevant to them alone. It works fine with the onOpen trigger, but I don't want them to have to open the sheet each time.
It would be easier to submit a form request and then receive the email but I'm not sure how to do that. I'm sure this is very basic so apologies for that, I'm new to this.
Basically I would like to form to ask if they want to receive an email with the info, and if they say yes, it triggers the script.
There are two ways to do this. The first is to:
Link the form responses to the spreadsheet
Add the installable trigger on form submit to the spreadsheet on the function you want to run.
Get the necessary information from the event object to see who submitted the form.
The second way is to:
Place a script on form.
Put an onFormSubmit() trigger on the form
Have the form open the spreadsheet - SpreadsheetApp.openById()
Copy paste the spreadsheet code into the form, making adjustments, so it will run (will mostly be how to get the user data)
If you would like this to only occur if the respondent asks, you can place a yes/no question on the form and check the answer. You can also have a questionless form that only collects emails, if you just want them to submit it to trigger an email.
there might be useful to do a template with a form with two buttons, 'Yes' or 'No'. If they hit 'Yes' you can hit an API in your platform to run script.
Maybe you can use a redis or some database to save the preferences of this user.
Hope it helps.

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?

In constructing a Google Form dropdown is it possible for the response that gets stored to be a coded value of what the user chooses?

In my Google Form I want to show the user a dropdown list consisting of several values from which he can choose one value. Such as:
Guernsey
Hereford
Jersey
But when his response gets stored into the response sheet I want it to be stored as a corresponding coded value, such as:
301
371
422
Is there any way to do this when I build a Form? Can it be accomplished with Google App Script?
Google Forms doesn't include form responses codification and the built-in feature that send form responses to an spreadsheet either. One alternative is that you use on form summit installable trigger. There are one for scripts bounded to forms and another for scripts bounded to spreasheet, each one having their own event object. For details see https://developers.google.com/apps-script/guides/triggers/events.

Update Google Forms field based on dynamic data from a Google Sheets spreadsheet

In a Google Form we have a pull down selection list.
The list of items may change from minute to minute. The master copy of the data is in a column in a google docs spreadsheet.
When a user accesses the form, we want them to see the latest copy of the pull down list as is current in the spreadsheet.
I don't see an obvious way to do this with triggers. onOpen, for example, only triggers for administrators of the form, not users. Is there a way I can keep this form up-to-date automatically?
This is my first time in google docs, but I'm very experienced in excel scripting.
The work flow needs to be a bit different than what you've described. Since there is no ability for Google Apps Script to interact with an end user accessing a Form, there's no way to react to that. However, you can react to changes in the spreadsheet, and update the form options in preparation for the next end user.
Depending on how the options get updated in the spreadsheet, you can use an installable trigger there, to react to an edit or a change.
If the form is associated with the spreadsheet containing the choices, you can get a handle on the form using Spreadsheet.getFormUrl(), then pass the URL to FormApp.openByUrl(). Otherwise you will need to provide the ID or URL explicitly to FormApp.openById() or .openByUrl(), respectively.
That spreadsheet trigger function can modify the available selections for the relevant question by using ListItem.setChoices() or ListItem.setChoiceValues().

Use Apps Script to change the response of a single form item directly (programmatically) rather than using the edit response urlj

I am using a form to collect booking details and have the script attached to the form (so that the form submit trigger will trigger when the response is edited, which the spreadsheet bound version does not allow) which sends an email, schedules calendar events, creates confirmation documents etc on form submit. Using if statements I have provided the user the ability to avoid 'click the box to not send the customer an email on submit' functionality so that the form responses can be edited without triggers the automated functions, but that relies on the user remembering to click those boxes.
A better solution would be to be able to have the script, as its final function, change the item response, so that when the form is edited in future, the automatic functions are already disabled.
I do not want to create a new form response (although it would be possible to tweak the pre-populated url to create a duplicate with the appropriate changes)
FormResponse.withItemResponse(response) specifies that it doesn't work on stored responses
It is so easy to view form responses, and with the native edit response url functionality I figured editing a single item response would be doable, but so far it has eluded me.
Is there a way to do this?
Thanks
At this time Form responses could only be created programmatically, they can't be edited programmatically. Consider to submit a feature request through Google Apps Script Issues