在表单提交触发器上发送电子邮件脚本的问题

时间:2018-12-12 18:39:23

标签: email google-apps-script google-sheets form-submit

更新: 我现在有一个工作脚本...主要是。除了我将CC:添加到sendEmail字符串中之外,电子邮件都没有任何错误地发送。由于某种原因,脚本调用该字段时,该字段将始终显示为“未定义”。我认为这可能与以下事实有关:它依赖于公式来进行计算,因此其中存在Utilites.sleep函数。

我确实需要能够计算此电子邮件地址,因为它会根据表单响应而改变。

function sendEmails2(e) {
  Utilities.sleep(5000);
  SpreadsheetApp.flush();
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName('Smoke');
  var dataRange = sheet.getRange(sheet.getLastRow(), 1, 1, 19);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var dispatcherName = e.namedValues["Dispatcher Name"];
    var dateofReport = e.namedValues["Date of Report"];
    var timeofReport = e.namedValues["Time of Report"];
    var descriptionofSmoke = e.namedValues["Description of Fire/Smoke"];
    var location = e.namedValues["Location"];
    var comments = e.namedValues["Comments"];
    var nameofCaller = e.namedValues["Name of Caller/Reporting Party"];
    var callerAgency = e.namedValues["Caller Agency or Organization"];
    var callbackNumber = e.namedValues["Callback Number (if given)"];
    var dispatchCenter = e.namedValues["Dispatch Center(s) Notified"];
    var notificationTime = e.namedValues["Time of Notification"];
    var personNotified = e.namedValues["Name of Person(s) Receiving Notification"];
    var addlNotification = e.namedValues["Additional Notification(s) Made (if applicable)"];
    var addlnotificationTime = e.namedValues["Time of Additional Notification(s)"];
    var emailSent = e.namedValues["EMAIL_SENT"];
    var emailAddress = e.namedValues["Email"];
    var EMAIL_SENT = "EMAIL_SENT";
    var ccAddress = row[16];
    var subject = 'ACTION REQUIRED: SMOKE REPORT';
    var message = 'Dispatcher Name: '+dispatcherName+'\n'+
      'Date of Report: '+dateofReport+'\n'+
      'Time of Report: '+timeofReport +'\n'+'\n'+
      'Description of Fire/Smoke: '+descriptionofSmoke +'\n'+
      'Location: '+location +'\n'+
      'Comments: '+comments +'\n'+'\n'+
      'Name of Caller/Reporting Party: '+nameofCaller +'\n'+
      'Caller Agency or Organization: '+callerAgency +'\n'+
      'Callback Number (if given): '+callbackNumber  +'\n'+'\n'+
      'Dispatch Center(s) Notified: '+dispatchCenter+'\n'+
      'Time of Notification: '+notificationTime+'\n'+
      'Name of Person(s) Receiving Notification: '+personNotified +'\n'+'\n'+
      'Additional Notification(s) Made (if applicable): '+addlNotification+'\n'+
      'Time of Additional Notification(s): '+addlnotificationTime;
      
    if (emailSent != "EMAIL_SENT") { // Prevents sending duplicates
     Utilities.sleep(3000);
      MailApp.sendEmail(emailAddress,subject, message,{cc: ccAddress});
      sheet.getRange(i+1, 19).setValue("EMAIL_SENT");


我看了看这里,还没有找到确切的解决方案。我的脚本不会在“表单提交”触发器上发送电子邮件,但我也没有收到错误消息。我不是专家,所以我确定这是一个我没有看到的简单问题。注意:工作表上第16列和第17列中有2个数组公式需要在脚本运行之前完成。

谢谢!

https://docs.google.com/spreadsheets/d/1c4pR78gYE8xpWGiWVjX627YNWftViQBHSP1CR6C_2AI/edit?usp=sharing

function OnSubmit(e) {  
  SpreadsheetApp.flush(); 
  var sheet = SpreadsheetApp.openById('1c4pR78gYE8xpWGiWVjX627YNWftViQBHSP1CR6C_2AI').getSheetByName('Smoke Reports');
  var dataRange = sheet.getRange(sheet.getLastRow(), 1, 1, 18);
  var data = dataRange.getValues();
  var EMAIL_SENT = "EMAIL_SENT";
  var startRow = 2
  var row = data[0];
  for (i in data) {
    var rowData = data[i];
    var emailAddress = rowData[15];
    var dispatcherName = rowData[1];
    var dateofReport = rowData[5];
    var timeofReport = rowData[6];
    var descriptionofSmoke = rowData[7];
    var location = rowData[8];
    var comments = rowData[9];
    var nameofCaller = rowData[2];
    var callerAgency = rowData[3];
    var callbackNumber = rowData[4];
    var dispatchCenter = rowData[10];
    var notificationTime = rowData[12];
    var personNotified = rowData[11];
    var addlNotification = rowData[13];
    var addlnotificationTime = rowData[14];
    var emailSent = rowData[17];
    var subject = 'ACTION REQUIRED: SMOKE REPORT';
    var message = 'Dispatcher Name:'+ " " +dispatcherName +"\n"+
      'Date of Report:'+ " "+dateofReport +"\n"+
      'Time of Report:'+ " "+timeofReport +"\n\n"+
      'Description of Fire/Smoke:'+ " "+descriptionofSmoke +"\n"+
      'Location:'+ " "+location +"\n"+
      'Comments:'+ " "+comments +"\n"+"\n"+
      'Name of Caller/Reporting Party:'+ " "+nameofCaller +"\n"+
      'Caller Agency or Organization:'+ " "+callerAgency +"\n"+
      'Callback Number (if given):'+ " "+callbackNumber  +"\n"+"\n"+
      'Dispatch Center(s) Notified:'+ " "+dispatchCenter +"\n"+
      'Time of Notification:'+ " "+notificationTime +"\n"+
      'Name of Person(s) Receiving Notification:'+ " "+personNotified +"\n"+"\n"+
      'Additional Notification(s) Made (if applicable):'+ " "+addlNotification  +"\n"+
      'Time of Additional Notification(s):'+ " "+addlnotificationTime
                  ;
  
    
  if (emailSent != EMAIL_SENT && emailAddress) { // Prevents sending duplicates
  MailApp.sendEmail(emailAddress, subject, message);
  sheet.getRange(startRow + i, 17).setValue(EMAIL_SENT);
 
  }
  }

                  }

0 个答案:

没有答案