简体   繁体   English

如果Google表单中写入了特定的值,请发送电子邮件

[英]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". 我正在使用Gforms,我需要向“朋友”发送电子邮件。当最后提交的表单在“ J”列中具有某个值时,在本例中为“ 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. 要访问发送到函数的事件信息,您需要包括e作为参数。 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. 例如, e.values是由触发函数的表单提交的值的数组。 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.) 由于它是一个数组,所以e.values从0开始编号。因此,要访问列J,您需要元素9。(J是电子表格上的第10列。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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