Specifying form for 'on form submit' trigger

Asked
Active3 hr before
Viewed126 times

6 Answers

submit
90%

Problem is: when setting up a trigger onformsubmit, it does not allow you to specify which form.,Question: how can I restrict the functions from running only when the specific form is submitted?,Thanks for contributing an answer to Stack Overflow!, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

Here is a basic example to illustrate : it will send you an email to tell which form has been submitted but you can of course use the same condition test to select the action you want to run.

function formSubmitOriginTest(e) {
   //  Logger.log(JSON.stringify(e));
   // example value : {"namedValues":{"Untitled Question 2":["test"],"Timestamp":["8/17/2014 11:22:47"]},"values":["8/17/2014 11:22:47","test"],"source":{},"range":{"rowStart":3,"rowEnd":3,"columnEnd":2,"columnStart":1},"authMode":{}}
   if (e.namedValues["Untitled Question 2"] != undefined) { // only form B has one question with this exact title, that's enough to identify it.
      MailApp.sendEmail(Session.getActiveUser().getEmail(), 'form B has been submitted', ''); // optionally include the answers in the email body if you want
   } else {
      MailApp.sendEmail(Session.getActiveUser().getEmail(), 'form A has been submitted', ''); // optionally include the answers in the email body if you want
   }
}
load more v
88%

Problem is: when setting up a trigger onformsubmit, it does not allow you to specify which form.,I have set up a script with two functions. I would like the first function to run on submission of the first form. And, the second function to run on submission of the second form.,Question: how can I restrict the functions from running only when the specific form is submitted?,As I suggested in the comments, you can determine the origin of the form submission by analyzing the response object's content.

Also:

var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
ScriptApp.newTrigger('myFunction')
   .forForm(form)
   .onFormSubmit()
   .create();
load more v
72%

I have two google forms, which send data to two tabs in a single spreadsheet.,Therefore, whenever completing the first form, it runs both functions.,I have set up a script with two functions. I would like the first function to run on submission of the first form. And, the second function to run on submission of the second form.,And if there is an error, then release the lock. So, the following code adds a try/catch block to handle that.

Also:

var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
ScriptApp.newTrigger('myFunction')
   .forForm(form)
   .onFormSubmit()
   .create();
load more v
65%

Create a Form submit trigger using Apps Script,Create an Edit trigger using Apps Script,Create an Open trigger using Apps Script,Create a Change trigger using Apps Script

The objective of setting up a trigger is to run a function automatically. This means we need a function that will be run by the trigger. In this tutorial, I will assume that we want the trigger to run a function called runScript(). This function is simple and logs the event object that provides information about the event that caused the trigger to run the function.

function runScript(eventInfo) {
   Logger.log(JSON.stringify(eventInfo));
}
load more v
75%

Allow users to update your app’s data with Google Forms ,Google Forms and Google Sheets to allow your users to update (edit) your Awesome Table app’s content and,In this article, you will learn how to use Google Forms to allow users to update (edit) your Awesome Table app’s content. Examples using this setup include, but are not limited to:,Google Forms (with responses)

 

var formURL = 'https://docs.google.com/forms/d/yourId/viewform';
var sheetName = 'Form Responses 1';
var columnIndex = 8;

function getEditResponseUrls() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
   var data = sheet.getDataRange().getValues();
   var form = FormApp.openByUrl(formURL);
   for (var i = 2; i < data.length; i++) {
      if (data[i][0] != '' && data[i][columnIndex - 1] == '') {
         var timestamp = data[i][0];
         var formSubmitted = form.getResponses(timestamp);
         if (formSubmitted.length < 1) continue;
         var editResponseUrl = formSubmitted[0].getEditResponseUrl();
         sheet.getRange(i + 1, columnIndex).setValue(editResponseUrl);
      }
   }
}
40%

The submit event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript.,Then the submit event is not generated. It is assumed that if the programmer calls form.submit(), then the script already did all related processing.,Both actions lead to submit event on the form. The handler can check the data, and if there are errors, show them and call event.preventDefault(), then the form won’t be sent to the server.,The method form.submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.

<form onsubmit="alert('submit!');return false">
   First: Enter in the input field <input type="text" value="text"><br>
   Second: Click "submit": <input type="submit" value="Submit">
</form>
load more v

Other "submit-undefined" queries related to "Specifying form for 'on form submit' trigger"