0
votes

I'm having some issues regarding this script for sending an email after the submit of google form.

So, when I run my code I receive this error message: ReferenceError: getValues is not defined at onFormSubmit

Below you can find the script

function onFormSubmit(e) {
  Logger.log("[METHOD] onFormSubmit");
  
  sendEmail(e.range);
}

function sendEmail(range) {
  Logger.log("[METHOD] sendEmail");

  // FETCH SPREADSHEET //
  var values = range.getValues();
  var row = values[0];

  // EXTRACT VALUES //
  var DateRequest = values[0]
  var TypeRequest = values[1]
  var StartDate = values[2]
  var EndDate = values[3]
  var Observation = values[4]
  var TLemail = values[5]

  // CLEAN VARIABLES //
  if (Observation.length == 0) Explain = "None";
  
  // PREPARE EMAIL //
  var emailRecipients = +TLemail+"@google.com";
  var emailSubject = TypeRequest;
  var emailBody = "<h3>Vacation Request:</h3><hr /> \
  <p> \
  <strong>Type or request:</strong><br /> \
  "+TypeRequest+" \
  </p><p> \
  <strong>Date of Request:</strong><br /> \
  "+DateRequest+" \
  </p><p> \
  <strong>Start Date:</strong><br /> \
  "+StartDate+" \
  </p><p> \
  <strong>End Data:</strong><br /> \
  "+EndDate+" \
  </p><p> \
  <strong>Aditional comment:</strong><br /> \
  "+Observation+" \
  </p><p> \
  </p>";


  // SEND EMAIL //
  MailApp.sendEmail({
  to: emailRecipients,
  subject: emailSubject,
  htmlBody: emailBody
 });
}

Thanks in advance

2

2 Answers

1
votes

You did not define range nowhere in your code

  • Either you define it manually
  • Or, you make usage of the event objects the trigger onFormSubmit provides

One option would be to use the event object range:

function onFormSubmit(e) {
 var values = e.range.getValues();

which would be equivalent to

function onFormSubmit(event) {
  var values = event.range.getValues();

or

function onFormSubmit(hello) {
  var values = hello.range.getValues();
  • Or, much easier, you directly retrieve e.values

So:

function onFormSubmit(e) {
  var values = e.values;
  var DateRequest = values[0];
  var TypeRequest = values[1];
  ...
0
votes

Replace:

var values = range.getValues();

with

var values = e.range.getValues();