简体   繁体   中英

Send email if certain value is written in Google Forms

I am working with Gforms and I need to send an email to "A Friend" When the last form submitted has in the "J" Column a certain value, in this case: "Roberto". This is my script. But it seems not to be working, I get no email when that value is submitted in a form. I did setup the trigger to run the script when a form is submitted.

function myNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ranchE = e.range.getRow();
  var Kjota = "J";
  var rangex = Kjota + ranchE;
  var value = ss.getSheetByName("Respuestas de formulario").getRange(rangex).getValue();
  var email = "heregoes@myemail.com";
  if( value == "Roberto" ) {
      MailApp.sendEmail(email, "Test", "Yes is Roberto");
  }
}

To access the event information that's sent to your function, you need to include e as a parameter. See Understanding Events to learn about the information that is passed to your trigger function.

For example, e.values is an array of values that were submitted by the form that triggered the function. You don't need to read the spreadsheet - the information is handed to you for free.

Since it's an array, e.values starts numbering from 0. So to access column J, you need element 9. (J is the 10th column on the spreadsheet.)

If we wanted to calculate column numbers from letters, we could do it this way:

var Kjota = "J";
var colIndex = Kjota.charCodeAt(0) - 'A'.charCodeAt(0);  // colIndex = 9

One more thing - if it's your script, and you want the email sent to you, it's easy to get your own email using the Session service. That makes your script easier to share, too.

So, here's all you need:

function myNotification(e) {
  if( e.values[9] == "Roberto" ) {
      var email = Session.getUser().getEmail();
      MailApp.sendEmail(email, "Test", "Yes is Roberto");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM