从Google Form更新单元格后,从Google Sheet发送电子邮件

时间:2019-08-06 18:31:55

标签: javascript google-sheets google-form

嗨,我目前有以下代码可以发送电子邮件:

function sendNotification(e){
  
if(e.range.getColumn()==4 && e.value=='Air Filter'){
 var recipients = "xx@email.com";
 var subject = "Update on "+e.range.getSheet().getName();
 var body = "Air filter 50751-123 needs ordered for the Hurco"
 MailApp.sendEmail(recipients, subject, body)
 }
  
if(e.range.getColumn()==4 && e.value=='Coolant'){
 var recipients = "xx@email.com"&&"xyx@email.com";
 var subject = "Update on "+e.range.getSheet().getName();
 var body = "Coolant is needed for the Hurco"
 MailApp.sendEmail(recipients, subject, body)
 
}  
  
}

我的问题是,当Google表单中提交了Google表单,然后某列中提交了某个单词时,我希望Google向所需的收件人发送电子邮件。

当我亲自进入图纸并编辑该列时,我可以使用它,但是我希望它在输入表单时可以使用。 (仅当我具有onEdit的触发器,当它处于onChange时,此方法才起作用,我收到“ TypeError:无法调用未定义的方法“ getColumn”的错误。(第3行,文件“ Email Notif”))”

它将要查找的列将包含四个可能要查找的单词,每个单词可能链接到不同的收件人。 “空气过滤器”分配给一个人,而“冷却液”分配给两个不同的人。

谢谢

1 个答案:

答案 0 :(得分:1)

要在提交表单时发送电子邮件,您需要使用"on form submit" installable trigger

必须更新代码以使用正确的event properties。您使用的是“编辑时”事件的.value属性,但是“提交表单时”事件的.values是值数组,或者是.namedValues是对象,表单问题是关键(即响应表的列标题)。

该范围将是表单提交的内容,因此检查第4列的范围没有太多意义,就像您需要执行编辑事件一样。

function sendNotification(e) {
  var recipients = "xx@email.com";
  var subject = "Update on " + e.range.getSheet().getName();
  var body = "";
  if (e.namedValues["Item that is needed."] == 'Air Filter') {
    body = "Air filter 50751-123 needs ordered for the Hurco";
  } else if (e.namedValues["Item that is needed."] == 'Coolant') {
    recipients += ",xyx@email.com";
    body = "Coolant is needed for the Hurco";
  }
  if (body.length > 0) {
    MailApp.sendEmail(recipients, subject, body);
  }
}

编辑:

根据评论中的要求,进行了更新,其中包括根据选定的列号检查先前提交的重复项。

function sendNotification(e) {
  var recipients = "xx@email.com";
  var subject = "Update on " + e.range.getSheet().getName();
  var body = "";
  if (checkForSameSubmission(e, 24, [2, 3])) { // event, hours to look back, columns to check (zero index!)
    subject += " DUPLICATE";
    body += "WARNING: DUPLICATE!\n";
  }
  if (e.namedValues["Item that is needed."] == 'Air Filter') {
    body += "Air filter 50751-123 needs ordered for the Hurco";
  } else if (e.namedValues["Item that is needed."] == 'Coolant') {
    recipients += ",xyx@email.com";
    body += "Coolant is needed for the Hurco";
  }
  if (body.length > 0) {
    MailApp.sendEmail(recipients, subject, body);
  }
}

// JSON.stringify will let us compare array values as strings
function checkForSameSubmission(event, hours) {
  var sheetData = event.range.getSheet().getDataRange().getValues();
  sheetData.shift(); // remove header row
  sheetData.pop(); // remove just submitted row
  var byColumns = function (_, index) { // for Array.prototype.filter
    return columns.indexOf(index) > -1;
  }
  var toStrings = function (value) { // for Array.prototype.map
    return String(value); // ensure all numbers become strings for final comparison
  };
  var thisSubmissionData = JSON.stringify(event.values.filter(byColumns).map(toStrings));
  var isSameSubmission = function (dataRow) {
    return thisSubmissionData === JSON.stringify(dataRow.filter(byColumns).map(toStrings));
  }
  return checkPreviousDataByHours(hours, sheetData, isSameSubmission);
}

function checkPreviousDataByHours(hours, data, someCallback) {
  var minusHours = Date.now() - 1000*60*60*hours;
  var lastHoursData = data.filter(function (row) { return +row[0] > minusHours; });
  return lastHoursData.some(someCallback);
}