Techtonic
Techtonic

Reputation: 11

Is there a way to specify which form to use as onFormSubmit trigger when multiple forms linked to one sheet?

I Currently have two completely different forms providing two different functions linked to the same spreadsheet (and they need to be because I use data from both).
I want to be able to distinguish functions which are triggered, one function called emailQuote() for instance when one of the forms is submitted, but if the other form is submitted, I want to trigger the scanForQuoters() function that I already have, is there a way to do this please?

I am simply using Google App Script with script tool to create my own functions, and there is the ability to trigger them on several parameters such as onEdit or onFormSubmit. It is being able to use onFormSubmit and distinguish on which form submit, which is what I am concerned about.

See Image of triggers here

Upvotes: 1

Views: 459

Answers (2)

Alan Wells
Alan Wells

Reputation: 31320

The Form that is linked to the Sheet that just got the answers can be retrieved as follows.

function myFormSubmitFnc(e) {
  var form,sheet,urlOfLinked;
  
  sheet = e.range.getSheet();//Get the sheet tab that the Form answers were written to  
  urlOfLinked = sheet.getFormUrl();//Returns the URL for the form that sends its 
  //responses to this sheet, or null if this sheet has no associated form

  form = FormApp.openByUrl(urlOfLinked);//The the Form that set the answers in the Sheet
};

Upvotes: 2

Wicket
Wicket

Reputation: 38435

No, there isn't but you can add a condition based on the destination sheet. The following example use switch but you could use if..else or any other way to handle conditional expressions.

NOTE: e is used for the on form submit event object. e.range is available for on form submit trigger for a spreadsheet.

function respondToOnFormSubmit(e){
  switch(e.range.getSheet().getName()){
    case 'Sheet1':
      // Stuff to be done when the submissions responses are sent to Sheet1
      break;
    case 'Sheet2':
      // Stuff to be done when the submissions responses are sent to Sheet2
      break;
    default:
    // do nothing
  }
}

Resources

Upvotes: 2

Related Questions