Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

I need clarification from someone.

I understand that multiple forms can send their responses to a single spreadsheet in the new Google Sheets.

So I created two Google forms and had them both send their responses to a new Google spreadsheet. I then created two scripts in the spreadsheet (not in the forms). Each script is set to trigger when a form is submitted. Here are the two simple scripts.

function form2() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  sheet.getRange(sheet.getLastRow(),3).setValue("This is for test 2");
}

function form1() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 2");
  sheet.getRange(sheet.getLastRow(),3).setValue("This is for test 1");
}

Somehow, Google is running the correct script for the appropriate form! How can that be? That is, form1() is automatically triggered when someone submits form1 and form2() is automatically triggered when someone submits form2.

So my question is, "How does Google know which script to run when a form is submitted?".

Thanks. I want to send all my form data to one spreadsheet if possible, but I don't want to set something up that will be unreliable.

-jay


Thanks wchiquito for your answer. I think that I figured this out with your help. Here is what I think is going on. If someone can confirm or disprove my conclusions it would be a big help to me.

There are two ways of creating a form in Google Apps.

  • Create a Form Directly.
  • Create a spreadsheet first and then create the form by selecting "Form" and then "Create Form" from within the menu bar of the spreadsheet.

But no matter how you create your form, you will end up having two files. One will be for the form and one will be for the spreadsheet which receives the data from the form. If you put the script in the form and in a spreadsheet, both will run when the form is submitted. The difference is that any script in the form will be limited in permissions. That is, any script in the form will not be able to access the spreadsheet where-as the script in the spreadsheet will have all of the permissions that you have (as long as the trigger is set under your account).

Now...

The new Sheets in Google Drive now has the ability to receive responses from more than one form while the old sheets in Google Drive only had the ability to receive one form responses. This eventually lead to my confusion and the answer from wchiquito is correct. It appears that the receiving spreadsheet will run as many onsubmit scripts as you like, but whether it is one or more than one, they will all run on all form data being received by the spreadsheet. That is, when the receiving spreadsheet receives form data from any form, ALL onsubmit scripts within the spreadsheet will be executed.

like image 625
user2284680 Avatar asked Nov 01 '22 01:11

user2284680


1 Answers

Both scripts should be executed, perhaps by using literal strings, the change is not visible.

Try running with the following changes:

function form2(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
  sheet.getRange(sheet.getLastRow(), 3).setValue(e.values);
}

function form1(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 2');
  sheet.getRange(sheet.getLastRow(), 3).setValue(e.values);
}
like image 52
wchiquito Avatar answered Jan 04 '23 13:01

wchiquito