【问题标题】:Google Apps Script personalised Email notificationsGoogle Apps 脚本个性化电子邮件通知
【发布时间】:2017-01-24 09:51:21
【问题描述】:

我想要一个脚本,当输入新的数据行时,它会从电子表格中发送一封电子邮件。我希望将其中一些数据包含在主题中,并希望在正文中包含更多数据和指向电子表格的链接。

我目前正在使用一个脚本,该脚本会在电子表格中的某个列更新时发送电子邮件,但我现在想对其进行更多个性化设置,以便收件人更快地查看,而不必总是打开电子表格。

This 是我的电子表格:

这是我当前的脚本:

function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Requests");
  var cell = ss.getActiveCell().getA1Notation();
  var row = sheet.getActiveRange().getRow();
  var cellvalue = ss.getActiveCell().getValue().toString();

  if (cell.indexOf('F')!=-1)
  {
    MailApp.sendEmail({
      to: "email@yourdomain.co.uk",
      subject: "Request",
      htmlBody: "There has been a new request. <br \> <br \> To review it, use the link below. <br \> <br \> " +
           "<a href=\"https://docs.google.com/spreadsheets/d/1Eq8Kpn0oOBItaGaKaOOLa1qLZZ2_l2gg6wOkLa6GMWo/edit#gid=872382993\">Requests</a>"
    });
  }
}

我希望在电子邮件主题中包含数量(F 列)和日期(E 列),以及数量 (F) 、date (E) 和 depot (D) 包含在正文中,并带有指向电子表格的链接。

编辑:G、H 和 I 列将在稍后由电子邮件收件人填写。

提前致谢

编辑:

我的脚本现在看起来像这样:

function getActiveRowValues(sheet){
  var cellRow = sheet.getActiveRange().getRow();
  // get depot value
  var depotCell = sheet.getRange("D" + cellRow);
  var depot = depotCell.getDisplayValue();
  // get date value
  var dateCell = sheet.getRange("E" + cellRow);
  var date = dateCell.getDisplayValue();
  // get quantity value
  var quantCell = sheet.getRange("F" + cellRow);
  var quant = quantCell.getDisplayValue();
  // return an object with your values
  return {
    depot: depot,
    date: date,
    quantity: quant
  } 

if (cell.indexOf('F')!=-1)
{
  var rowVals = getActiveRowValues(sheet);
  MailApp.sendEmail({
   to: "email@yourdomain.co.uk",
   subject: "Request" + " date " + rowVals.date + " quant " + rowVals.quant,
   htmlBody: "There has been a new request. <br \> <br \> To review it, use the link below. <br \> <br \> " + "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Depot</th><th>Date</th><th>Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quant+"</td></tr></table>" + 
       "<a href=\"https://docs.google.com/spreadsheets/d/1Eq8Kpn0oOBItaGaKaOOLa1qLZZ2_l2gg6wOkLa6GMWo/edit#gid=872382993\">Requests</a>"
  });
}}

但得到错误:

TypeError:无法调用未定义的方法“getActiveRange”。 (第 2 行, 文件“代码”)

【问题讨论】:

  • 对不起,有什么问题?
  • 我希望将日期和数量包含在电子邮件主题中,并在电子邮件正文中包含日期、数量和仓库,以及指向电子表格的链接。

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


【解决方案1】:

在您的脚本中添加此函数,该函数将从相应列中提取您所需的详细信息:

/**
* get values of depot, date and quantity from their respective cells.
* @returns    {Object.<string, string>}
*/
function getActiveRowValues(sheet){
  var cellRow = sheet.getActiveRange().getRow();
  // get depot value
  var depotCell = sheet.getRange("D" + cellRow);
  var depot = depotCell.getDisplayValue();
  // get date value
  var dateCell = sheet.getRange("E" + cellRow);
  var date = dateCell.getDisplayValue();
  // get quantity value
  var quantCell = sheet.getRange("F" + cellRow);
  var quant = quantCell.getDisplayValue();
  // return an object with your values
  return {
    depot: depot,
    date: date,
    quantity: quant
  } 
}

然后在您的 if 条件中调用 getActiveRowValues 并更新您的主题和 htmlBody 变量:

if (cell.indexOf('F')!=-1)
{
  var rowVals = getActiveRowValues(sheet);
  MailApp.sendEmail({
   to: "email@yourdomain.co.uk",
   subject: "Request" + " date " + rowVals.date + " quant " + rowVals.quantity,
   htmlBody: "There has been a new request. <br \> <br \> To review it, use the link below. <br \> <br \> " + "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Depot</th><th>Date</th><th>Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>" + 
       "<a href=\"https://docs.google.com/spreadsheets/d/1Eq8Kpn0oOBItaGaKaOOLa1qLZZ2_l2gg6wOkLa6GMWo/edit#gid=872382993\">Requests</a>"
  });
}

你的整个脚本应该是这样的:

function sendNotification(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //var sheet = ss.getSheetByName("Requests");
  var sheet = ss.getActiveSheet();
  var row = sheet.getActiveRange().getRow();
  var cellvalue = ss.getActiveCell().getValue().toString();
  var emailAdd = "email@yourdomain.co.uk";
  if(event.range.getA1Notation().indexOf("F") > -1 && sheet.getRange("F" + row).getDisplayValue() && emailAdd.length > 1)
  {
     var rowVals = getActiveRowValues(sheet);
     MailApp.sendEmail({
       to: emailAdd,
       subject: "Request" + " date " + rowVals.date + " quant " + rowVals.quantity,
       htmlBody: "There has been a new request. <br \> <br \> To review it, use the link below. <br \> <br \> " + "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Depot</th><th>Date</th><th>Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>" + 
       "<a href=\"https://docs.google.com/spreadsheets/d/1Eq8Kpn0oOBItaGaKaOOLa1qLZZ2_l2gg6wOkLa6GMWo/edit#gid=872382993\">Requests</a>"
      });
  }
}

/**
* get values of depot, date and quantity from their respective cells.
* @returns    {Object.<string, string>}
*/
function getActiveRowValues(sheet){
  var cellRow = sheet.getActiveRange().getRow();
  // get depot value
  var depotCell = sheet.getRange("D" + cellRow);
  var depot = depotCell.getDisplayValue();
  // get date value
  var dateCell = sheet.getRange("E" + cellRow);
  var date = dateCell.getDisplayValue();
  // get quantity value
  var quantCell = sheet.getRange("F" + cellRow);
  var quant = quantCell.getDisplayValue();
  // return an object with your values
  return {
    depot: depot,
    date: date,
    quantity: quant
  } 
}

【讨论】:

  • 我无法通过它向我发送电子邮件,但在我看来,它就像我所追求的那样。作为一个完整的新手,我不确定我是否输入正确 - 因为我不确定你所说的“在你的情况下”是什么意思。
  • 从上面你写的代码;这是您发送电子邮件的条件:if (cell.indexOf('F')!=-1){ //send email }。这将在 cell.indexOf('F') != -1 为真时触发,这意味着您的活动单元格应位于 F 列中,然后才会触发此条件。对我来说,这种情况毫无意义,但我不知道整个情况。虽然如果您上面的代码工作正常,那么我认为通过将 if (cell.indexOf('F')!=-1){ } 中的代码块替换为我上面写的代码块也应该可以正常工作
  • cell.indexOf('F')!=-1 我从这篇文章中得到stackoverflow.com/questions/24374737/… 我刚刚修改了脚本以适合我,它主要是我在原始帖子中包含的那个.
  • 我相信这部分有错误:var cellRow = ss.getActiveRange().getRow();
  • 对不起,我的错!我在这一行犯了一个错误: var cellRow = ss.getActiveRange().getRow(); ss 应该是工作表。我已经编辑了我的答案。
【解决方案2】:

您可以在正文和主题的 HTML 上连接值

function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Requests");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();

var thisIsAVariable = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Requests").getRange("A1").getValue();

if (cell.indexOf('F')!=-1)
{
MailApp.sendEmail({
 to: "email@yourdomain.co.uk",
 subject: "Request" + + thisIsAVariable +,
 htmlBody: "concatenate like: "+ thisIsAVariable +"There has been a new request. <br \> <br \> To review it, use the link below. <br \> <br \> " + thisIsAVariable +
           "<a href=\"https://docs.google.com/spreadsheets/d/1Eq8Kpn0oOBItaGaKaOOLa1qLZZ2_l2gg6wOkLa6GMWo/edit#gid=872382993\">Requests</a>"
});
}}

【讨论】:

  • 这似乎与我已有的没有什么不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2020-12-15
  • 2011-12-14
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多