18

How does one reference the default 'Submit' Forms button to link the 'onsubmit' trigger with another action? Thank you.

For example, with onsubmit,

Logger.log('do something');
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user2012403
  • 205
  • 1
  • 3
  • 8
  • Can you elaborate your question. It is not clear enough. Are you looking to run a function when someone submits a form ? If so, then you can call the function as onSubmit() – Srik Mar 28 '13 at 16:21

6 Answers6

25

Instead of adding a script to the Spreadsheet receiving responses from the Form (as in Mogsdad's answer), I added a script which gets triggered by the Form's Submit button.

Step 1: first create a Google Form

sample Google Form

Step 2: Then from the menu, click Tools->Script editor

Form editor menu bar

Step 3: give your function a name like onSubmit()

Script editor

Step 4: Write some code to send an email like this, then test by click Run button

function onSubmit() {
  MailApp.sendEmail("fred@email.com,alice@email.com",
                    "Subject",
                    "A new application has been submitted.\n\n" +
                    "Here is the link to all Applications:\n" +
                    "https://docs.google.com/spreadsheets/x/1-fdasjhFAKEfdaahfkhsa/",
                    {name:"From Name"});
}

Step 5: in the Script Editor's menu, click Run, or click the Play button on the toolbar to test your code (e.g. make sure you get the email)

Menu bar Run command

Step 6: in the Script Editor's menu, click Resources-> Current project's triggers

Script Editor menu bar

Step 7: select the settings Events From form On form submit

Google App Triggers

Step 8: Then from the Form Editor's menu, click Tools->Script manager and make sure your script is selected

Script manager

Step 9: try the form. You should get an email after clicking the form's submit button.

Google Apps live Form

JohnB
  • 18,046
  • 16
  • 98
  • 110
  • 1
    The assumption ** I added a script which gets triggered by the Form's Submit button.** is not true, your answer uses the same installable trigger as in Mogsdad's answer... you simply embed it in the form container but it changes just nothing at all. At the time the first answer was posted one couldn't embed scripts in forms. – Serge insas Aug 14 '14 at 16:41
13

One doesn't. The Form Service does not allow any programmatic control over the product. There is an open issue on the Issue Tracker for this general topic. Star it to "vote", and receive updates.

However, you're not lost. In the spreadsheet receiving your form responses, you can add a script with an Installable Trigger to fire "On form submit". From there, you can do many things to the submitted data, including Logging as you wish... but nothing to the Form itself.

Triggers

These references explain triggers and events.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
6

This is an old answer.

Currently (Jan 2014) there are two ways for onSubmit. One is simply to make a function onSubmit() which supposedly (it doesn't work for me...) allows a limited set of actions under the permission of the current submitting user only. You cannot access his submitted email, for example, or modify the underlying form for next submit.

Then there is is the trigger on submit which you can add and attach to any function and do whatever you want under your own permissions. looks same as screenshot of trigger adding in the answer above, except that its the first column shows methods in you forms script, the next column reads: From-Form and in the third column you choose: On Submit.

Typically your method will receive an event e , who's e.values are the same as the values saved in the spreadsheet. So

function formSubmitted(e){ ...

pashute
  • 3,965
  • 3
  • 38
  • 65
6

There seems to be two applications which trigger code: (1) the form and (2) the spreadsheet linked to the form responses.

(1) There is not much one can do programmaticly from the form as the event is not sent to functions, as noted by Mogsdad and JohnB above. One can, for example, send an email "On form submit" or "On open" but without the event object, one can't even record the number or rows nor anything about the document or submission.

(2) However, if a spreadsheet is linked to the responses of the form, then a trigger can be set on the spreadsheet, where one has access to the event and thus row, column, and other data.

From linked response Google Spreadsheet:

  • Step 1: Tools > Script editor
  • Step 2: write some code, for example:

Code.gs:

function onSpreadsheetSubmit(e) {
     var row = e.range.getRow();
     MailApp.sendEmail("me@example.com",
                "Your subject, rows: "+ row,
                "A new application has been submitted on row: "+
                row,
                {name:"From your friendly spreadsheet"});
}
  • Step 3: Run > Run function > onSpreadsheetSubmit (or |> icon)
    • The event likely doesn't have an actual row because the test is not associated with a real submit.
  • Step 4: You'll be asked to choose a user and authenticate yourself
  • Step 5: Edit > Current project's triggers (or clock-like icon)
  • Step 6: Add a new trigger
  • Step 7: onSpreadsheetSubmit | From spreadsheet | On form submit
  • Step 8: Authenticate and Save (or reverse)
  • Step 9: Test by waiting (or executing) a real form submission
  • Step 10: Check your email

For more info: https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events

alex
  • 111
  • 1
  • 2
  • Trigger onFormSubmit from a form has an event object too: https://developers.google.com/apps-script/guides/triggers/events#form-submit_1 – Max Makhrov Oct 27 '20 at 07:11
2

JohnB's HOWTO above is excellent. Update (Nov 2017):

  • Step 2: "Script Editor..." is in the vertical "..." burger menu top right of form
  • Step 6: "Project Triggers" is found in the (clock-like icon) (second icon displayed above).
  • Step 7: Click "Add a new trigger", ignore the "no myFunctions" warning.
  • Step 8: doesn't seem to exist, may not be necessary, works for me without.

Many of the steps above require validation/acceptance by the form owner.

alex
  • 111
  • 1
  • 2
0

I'd recommend using trigger from Forms. In your form open script editor and paste the code:

function onFormSubmit(e) {  
  // for tests
  if (!e) { 
    // test object
    var form = FormApp.getActiveForm()
    var responses = form.getResponses();
    e = {"authMode":"FULL",
         "response": responses[responses.length - 1], // last response
         "source": form,
         "triggerUid":"5125265"};
  };
  // do your job here
  try {
    // paste useful code here...
    //
    //
  }
  catch (err) {
    MailApp.sendEmail("testemail@test.com",
                      "Error in the app",
                      "New error, please check.\n\n" +
                      "Here is the event object:\n" +
                      e + "\n" + 
                      JSON.stringify(e) + "\n\n" + 
                      "Here's an error:\n" + err,
                      {name:"From Name"});     
    throw err;  
  }
  return 0;   
}

Set this function as an installable trigger.

Ref:

Max Makhrov
  • 17,309
  • 5
  • 55
  • 81