0

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("me@example.com",
                "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?

gordon613
  • 2,770
  • 12
  • 52
  • 81
  • Are you pasting this code into the form or into the spreadsheet? – J. G. Jun 24 '19 at 21:04
  • Is this thread useful for your situation? https://stackoverflow.com/q/43429161 – Tanaike Jun 25 '19 at 00:22
  • Possible duplicate of [How to get form values in the submit event handler?](https://stackoverflow.com/questions/43429161/how-to-get-form-values-in-the-submit-event-handler) – TheMaster Jun 25 '19 at 04:26
  • The code is pasted into the google apps script associated with the spreadsheet. The links are very interesting and useful, but I feel that my precise question was only answered here – gordon613 Jun 25 '19 at 04:47

1 Answers1

1

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.
ross
  • 2,684
  • 2
  • 13
  • 22