发送电子邮件正确触发设置

时间:2018-04-11 20:27:35

标签: javascript google-apps-script google-sheets

我想帮助设置一个触发器...我有一张表连接到一个获得调查的表单,但在另一个选项卡中我只从2个特定用户提取数据,我试图设置一个触发器当这2个用户提交表单时发送电子邮件。

我已经尝试过:从电子表格(在更改时,在编辑和在表单上提交)但没有...我不想设置时间驱动或日历因为或他们将收到相同的电子邮件很多次,或者如果他们一个接一个地提交,只有最后一个会收到电子邮件。

脚本如下:

function SendEmail() {
  var Spreadsheet = SpreadsheetApp.getActive();
  var Sheet = Spreadsheet.getSheetByName("Email test");
  var counter = Sheet.getRange("AB1").getValue();
  var email = ""+ Sheet.getRange(counter, 25).getValue();
  Logger.log(email);
  var Subject = "Summary Of Your Coaching Session";
  var Session = Sheet.getRange(counter, 15).getValue();
  var KPI = Sheet.getRange(counter, 9).getValue();
  var exception = Sheet.getRange(counter, 6).getValue();
  var casenum = Sheet.getRange(counter, 8).getValue();
  var wins = Sheet.getRange(counter, 10).getValue();
  var coachwins = Sheet.getRange(counter, 11).getValue();
  var timeframe = Sheet.getRange(counter, 14).getValue();
  var smartplan = Sheet.getRange(counter, 18).getValue();
  var nickname = Sheet.getRange(counter, 27).getValue();
  var evenbetter = Sheet.getRange(counter, 16).getValue();

  var type = Sheet.getRange(counter, 26).getValue();
  Logger.log(type);

  if (type == 'Win'){

    var body = ('Hello '+ nickname +','+'<br><br>Here is the summary of your '+ Session +':'+'<br><br>Your coaching was based on <b>'+ KPI +'</b> and case number '+ casenum +' and we identified the following wins '+ wins +', '+ coachwins +' and we are committed to '+ smartplan +' that we will be following on ' + timeframe +'.<br><br>');

  } else if (type == "Full"){

    var body = ('Hello '+ nickname +','+'<br><br>Here is the summary of your '+ Session +':'+'<br><br>Your coaching was based on <b>'+ KPI +'</b>, from case number '+ casenum +' and the skill that we are focusing on mastering is '+ evenbetter +'. We are committing to "'+ smartplan +'" that we will be following on ' + timeframe +'.<br><br>');

  } else if (type == "Exception"){

    var body = ('Hello '+ nickname +','+'<br><br> A Coaching for Excellence exception has been submitted due to <b>'+ exception +'</b>, so we will meet next week. Your development is highly important for us, we encourage you to keep on working on your previous action plan. <br><br> If you have any questions regarding your previous action plan contact me.<br><br><br><br>');

  }

  MailApp.sendEmail(email, Subject, body, {htmlBody : body, noReply : true})

}

1 个答案:

答案 0 :(得分:1)

您应该使用“on form response”触发器,然后使用该触发器提供的event object。事件对象提供您需要的所有表单响应数据。如果您需要访问未在表单中提交但与表单数据位于同一行的信息,则可以使用formResponse.range.getRow()获取行号。

这正是您的代码在正确使用事件对象时的样子:

function SendEmail(formResponse) {
  var email = formResponse.values[24];
  Logger.log(email);
  var subject = "Summary Of Your Coaching Session";
  var session = formResponse.values[14];
  var kpi = formResponse.values[9];
  var exception = formResponse.values[5];
  var casenum = formResponse.values[7];
  var wins = formResponse.values[9];
  var coachwins = formResponse.values[10];
  var timeframe = formResponse.values[13];
  var smartplan = formResponse.values[17];
  var nickname = formResponse.values[26];
  var evenbetter = formResponse.values[15];
  var type = formResponse.values[25];
  Logger.log(type);
  if (type == 'Win'){
    var body = ('Hello '+ nickname +','+'<br><br>Here is the summary of your '+ session +
                ':'+'<br><br>Your coaching was based on <b>'+ kpi +'</b> and case number '+
                casenum +' and we identified the following wins '+ wins +', '+ coachwins +
                ' and we are committed to '+ smartplan +' that we will be following on ' +
                timeframe +'.<br><br>');
  } else if (type == "Full") {
    var body = ('Hello '+ nickname +','+'<br><br>Here is the summary of your '+ session +
                ':'+'<br><br>Your coaching was based on <b>'+ kpi +'</b>, from case number '+
                casenum +' and the skill that we are focusing on mastering is '+ evenbetter +
                '. We are committing to "'+ smartplan +'" that we will be following on ' +
                timeframe +'.<br><br>');
  } else if (type == "Exception"){
    var body = ('Hello '+ nickname +','+'<br><br> A Coaching for Excellence exception has been submitted due to <b>'+
                exception +'</b>, so we will meet next week. Your development is highly important for us, we encourage '+
                'you to keep on working on your previous action plan. <br><br> If you have any questions regarding your '+
                'previous action plan contact me.<br><br><br><br>');
  }
  MailApp.sendEmail(email, Subject, body, {htmlBody : body, noReply : true})
}