【问题标题】:Problems with email notification电子邮件通知问题
【发布时间】:2014-02-24 20:47:22
【问题描述】:

我是使用 google doc 脚本编辑器的新手,所以请善待。到目前为止,这是我所拥有的:

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 时间戳未定义

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

【问题讨论】:

  • 您为什么不与我们共享一个文档?

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


【解决方案1】:

您试图以一种没有意义(或工作)的方式引用单元格。试试这个经过测试/工作的代码:

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 列中的单元格未更改为“是”,则尝试解析所有数据是没有意义的。紧接着,我从编辑的行中检索所有值并用结果填充所有变量。其余的和你写的差不多。

祝你好运!

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 2015-02-15
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 2018-01-07
    • 2011-12-16
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多