Send email if certain value is written in Google Forms

牧云@^-^@ 提交于 2019-12-08 11:37:00

问题


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");
  }
}

回答1:


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");
  }
}


来源:https://stackoverflow.com/questions/17498186/send-email-if-certain-value-is-written-in-google-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!