Google Apps脚本个性化电子邮件通知

时间:2017-01-24 09:51:22

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

我想要一个脚本,当输入新的数据行时,它会从电子表格发送电子邮件。我想将一些数据包含在主题中,还有一些数据和主体电子表格的链接。

我目前正在使用一个脚本,当电子表格中的某个列更新时会发送电子邮件,但我现在想要对其进行个性化设置,让收件人更快地进行审核,而无需经常打开电子表格。

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) 日期(E)软件仓库(D)包含在主体中,并带有指向电子表格的链接。

编辑:G,H和G列。我将在以后填写电子邮件的收件人。

提前致谢

编辑:

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

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行,   文件“代码”)

2 个答案:

答案 0 :(得分:0)

您可以在身体的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>"
});
}}

答案 1 :(得分:0)

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

/**
* 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
  } 
}