Search code examples
google-apps-scriptgoogle-forms

Google Apps - on Form Submit - what is the name of the sheet


The following code from Alex's code on Google Forms onsubmit will send an email when a form attached to the spreadsheet is submitted.

function onSpreadsheetSubmit(e) {
     var row = e.range.getRow();
     MailApp.sendEmail("[email protected]",
                "Your subject, rows: "+ row,
                "A new application has been submitted on row: "+
                row,
                {name:"From your friendly spreadsheet"});
}

The code successfully returns the row number of the submission using e.range.getRow()

How can I get the name of the sheet that the form is connected to? E.g. something like e.range.getSheet()

Although this question How to get form values in the submit event handler? discusses retrieving the event values, I do not believe that it addresses directly the retrieval of the name of the sheet.

And how can I elegantly get the data of the new row?


Solution

  • If all you're trying to do is return the sheet name from your event object, use:

    var name = e.range.getSheet().getName();
    

    To access the values submitted to the form, you can use:

    var values = e.values;
    

    This returns the submitted values in an array.

    There are a lot of possibilities with event objects, you should really look into the documentation to find out how to use them to your advantage.


    References:

    1. Event Objects documentation.