电子邮件通知问题

时间:2014-02-24 20:47:22

标签: email google-apps-script notifications google-sheets

我刚开始使用谷歌文档脚本编辑器,所以请善待。这是我到目前为止所做的:

function onEdit(e) {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  //Get Active cell
  var mycell = ss.getActiveSelection();
  var cellcol = mycell.getColumn();
  var cellrow = mycell.getRow();

  //Define variables from sheet by column
  //Column count starts at 0, not 1.
  var timestamp = e.values[cellrow,14];
  var yourName = e.values[cellrow, 0];
  var email = e.values[cellrow, 1];
  var plot2013 = e.values[cellrow, 5];
  var plotrequest = e.values[cellrow, 6];
  var sharing = e.values[cellrow, 7];
  var totalprice = e.values[cellrow, 8];
  var paid = e.values[cellrow, 13];
  var subject = "TSF Payment Confirmation"

  //email body
  var emailBody = "Thank you for your payment submitted on " + timestamp +
    "\n\nThe details you entered were as follows: " +
    "\nYour name: " + yourName +
    "\nYour plot #: " + plot2013 +
    "\nNumber plots requested: " + plotrequest +
    "\nSharing plot with: " + sharing +
    "\nTotal payment: " + totalprice;

  //html version of email body
  var htmlBody = "Thank you for your payment submitted on <i>" + timestamp +
    "</i><br/>&nbsp;<br/>The details you entered were as follows: " +
    "<br/><font color=\"red\">Your Name:</font> " + yourName +
    "<br/>Your Email: " + plot2013;
    "<br/>Plots requested: " + plotrequest;
    "<br/>Sharing plot with: " + sharing +
    "<br/>Total payment: " + totalprice;

  //sends email if cell contents are 'yes'
  if (e.values[13,cellrow] == "yes") {
    //Sends email
    MailApp.sendEmail(email, subject, emailBody);
  }
}

我希望的是,一旦用户点击第13列(假设它从左边的0开始计数,而不是可变数字)并且类型为“是”,它将按照指定通过电子邮件发送来自该用户行的信息。

我收到一条错误消息,说:

  

var timestamp undefined

我从这个例子中得到了这个: http://alamoxie.com/blog/tech-tips/sending-confirmation-emails-google-docs-form/

1 个答案:

答案 0 :(得分:0)

你试图以一种没有意义(或工作)的方式引用细胞。请尝试使用此测试/工作代码:

function onEdit(e) {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Ensure the event was triggered by typing "yes" in the appropriate column
  if(e.range.getColumn() == 14 && e.value.toUpperCase() == "YES"){
    // Get the corresponding row
    var row = sheet.getRange(e.range.getRow(), 1, 1, sheet.getLastColumn()).getValues()[0];

    // Define variable from row by column
    var timestamp = row[14];
    var yourName = row[0];
    var email = row[1];
    var plot2013 = row[5];
    var plotrequest = row[6];
    var sharing = row[7];
    var totalprice = row[8];
    var paid = row[13];
    var subject = "TSF Payment Confirmation"

    // Construct the email body
    var emailBody = "Thank you for your payment submitted on " + timestamp +
      "\n\nThe details you entered were as follows: " +
      "\nYour name: " + yourName +
      "\nYour plot #: " + plot2013 +
      "\nNumber plots requested: " + plotrequest +
      "\nSharing plot with: " + sharing +
      "\nTotal payment: " + totalprice;

    // Construct an HTML version of the email body
    var htmlBody = "Thank you for your payment submitted on <i>" + timestamp +
      "</i><br/>&nbsp;<br/>The details you entered were as follows: " +
      "<br/><font color=\"red\">Your Name:</font> " + yourName +
      "<br/>Your Email: " + plot2013;
      "<br/>Plots requested: " + plotrequest;
      "<br/>Sharing plot with: " + sharing +
      "<br/>Total payment: " + totalprice;

    // Send email
    MailApp.sendEmail(email, subject, emailBody);
  }
}

注意我把所有内容放在条件语句中;如果N列中的单元格未更改为“是”,则尝试解析所有数据是没有意义的。紧接着,我从已编辑的行中检索所有值,并用结果填充所有变量。其余的几乎和你写的一样。

祝你好运!