Google表单脚本发送电子邮件

时间:2020-04-24 13:49:23

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

我在提交Google表单后发送电子邮件有关此脚本的一些问题。

因此,当我运行代码时,会收到以下错误消息: ReferenceError:在onFormSubmit上未定义getValues

下面您可以找到脚本

function onFormSubmit(e) {
  Logger.log("[METHOD] onFormSubmit");
  
  sendEmail(e.range);
}

function sendEmail(range) {
  Logger.log("[METHOD] sendEmail");

  // FETCH SPREADSHEET //
  var values = range.getValues();
  var row = values[0];

  // EXTRACT VALUES //
  var DateRequest = values[0]
  var TypeRequest = values[1]
  var StartDate = values[2]
  var EndDate = values[3]
  var Observation = values[4]
  var TLemail = values[5]

  // CLEAN VARIABLES //
  if (Observation.length == 0) Explain = "None";
  
  // PREPARE EMAIL //
  var emailRecipients = +TLemail+"@google.com";
  var emailSubject = TypeRequest;
  var emailBody = "<h3>Vacation Request:</h3><hr /> \
  <p> \
  <strong>Type or request:</strong><br /> \
  "+TypeRequest+" \
  </p><p> \
  <strong>Date of Request:</strong><br /> \
  "+DateRequest+" \
  </p><p> \
  <strong>Start Date:</strong><br /> \
  "+StartDate+" \
  </p><p> \
  <strong>End Data:</strong><br /> \
  "+EndDate+" \
  </p><p> \
  <strong>Aditional comment:</strong><br /> \
  "+Observation+" \
  </p><p> \
  </p>";


  // SEND EMAIL //
  MailApp.sendEmail({
  to: emailRecipients,
  subject: emailSubject,
  htmlBody: emailBody
 });
}

预先感谢

2 个答案:

答案 0 :(得分:1)

您没有在代码中的任何地方定义range

  • 您可以手动定义
  • 或者,您可以利用onFormSubmit触发器提供的event objects

一种选择是使用事件对象range

function onFormSubmit(e) {
 var values = e.range.getValues();

等同于

function onFormSubmit(event) {
  var values = event.range.getValues();

function onFormSubmit(hello) {
  var values = hello.range.getValues();
  • 或者,更容易地,您直接检索e.values

所以:

function onFormSubmit(e) {
  var values = e.values;
  var DateRequest = values[0];
  var TypeRequest = values[1];
  ...

答案 1 :(得分:0)

替换:

var values = range.getValues();

使用

var values = e.range.getValues();