Search code examples
google-apps-scriptgoogle-formsgoogle-sheets-api

google app script: on form response, check which tab of the sheet collects the responses


I am trying to automate some extra data generated to a form response using Google App Script; (in particular, a unique identifier). I can trigger a script on a form response, but I have multiple forms connected to this sheet document, so I need to make sure that the correct form is triggering the response, and then I would like to write the values to the correct sheet of the document.

How do I check which form is being triggered, and how can I find the sheet that collects it's responses?


Solution

  • I assume you've already set up the function on the sheet to capture the "on submit" event. You can get the [sheet] object from the e (event) argument.

    //make sure to set up the form submit trigger from
    //your script menu: Edit -> Current Project's Triggers
    
    function onSubmit(e) {
       var range = e.range
       var sheet = range.getSheet();
    
       //then do something with [sheet] object
    
    }
    

    I'm pretty sure you can only connect one form to a sheet, so knowing which sheet got written to will tell you which form was submitted.

    Check out more about event triggers in the documentation.