Email 谷歌应用程序脚本个性化电子邮件通知

Email 谷歌应用程序脚本个性化电子邮件通知,email,google-apps-script,google-sheets,Email,Google Apps Script,Google Sheets,我想要一个脚本,当输入一行新数据时,它将从电子表格发送电子邮件。我想在主题中包含一些数据,还有一些数据和正文中电子表格的链接 我目前正在使用一个脚本,当电子表格中的某个列被更新时,它会发送一封电子邮件,但我现在想对它进行更多的个性化设置,使收件人能够更快地查看,而不必总是打开电子表格 这是我的电子表格: 这是我目前的剧本: function sendNotification() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s

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

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

这是我的电子表格:

这是我目前的剧本:

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>"
    });
  }
}
函数sendNotification(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName(“请求”);
var cell=ss.getActiveCell().getA1Notation();
var row=sheet.getActiveRange().getRow();
var cellvalue=ss.getActiveCell().getValue().toString();
if(cell.indexOf('F')!=-1)
{
MailApp.sendmail({
至:email@yourdomain.co.uk",
主题:“请求”,
htmlBody:“有一个新请求。要查看它,请使用下面的链接。”+
""
});
}
}
我希望数量(F列)日期(E列)包含在电子邮件主题中,并且数量(F)日期(E)仓库(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>"
  });
}}
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
  } 
}
函数getActiveRowValues(工作表){
var cellRow=sheet.getActiveRange().getRow();
//获取仓库价值
var depotCell=sheet.getRange(“D”+cellRow);
var depot=depotCell.getDisplayValue();
//获取日期值
var dateCell=sheet.getRange(“E”+cellRow);
var date=dateCell.getDisplayValue();
//获取数量值
var quantCell=sheet.getRange(“F”+cellRow);
var quant=quantCell.getDisplayValue();
//返回带有值的对象
返回{
仓库:仓库,
日期:日期:,
数量:定量
} 
if(cell.indexOf('F')!=-1)
{
var rowVals=getActiveRowValues(表);
MailApp.sendmail({
至:email@yourdomain.co.uk",
主题:“请求”+“日期”+rowVals.date+“数量”+rowVals.quant,
htmlBody:“有一个新的请求。若要查看它,请使用下面的链接。“+”DepotDateQuantity“+rowVals.depot+”“+rowVals.date+”+rowVals.quant+”
""
});
}}
但是得到错误:

TypeError:无法调用未定义的方法“getActiveRange”。(第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>"
});
}}
函数sendNotification(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName(“请求”);
var cell=ss.getActiveCell().getA1Notation();
var row=sheet.getActiveRange().getRow();
var cellvalue=ss.getActiveCell().getValue().toString();
var thisisaviable=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“请求”).getRange(“A1”).getValue();
if(cell.indexOf('F')!=-1)
{
MailApp.sendmail({
至:email@yourdomain.co.uk",
主题:“请求”++thisisaviable++,
htmlBody:“像这样连接:“+thisIsAVariable+”有一个新请求。要查看它,请使用下面的链接。”+thisIsAVariable+
""
});
}}

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

/**
* 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
  } 
}
您的整个脚本应该如下所示:

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>"
  });
}}
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
  } 
}
函数发送通知(事件){
var ss=SpreadsheetApp.getActiveSpreadsheet();
//var sheet=ss.getSheetByName(“请求”);
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”+行)。getDisplayValue()&&emailAdd.length>1)
{
var rowVals=getActiveRowValues(表);
MailApp.sendmail({
致:emailAdd,
主题:“请求”+“日期”+rowVals.date+“数量”+rowVals.quantity,
htmlBody:“有一个新请求。若要查看该请求,请使用下面的链接。“+”DepotDateQuantity“+rowVals.depot+”+rowVals.date+”+rowVals.quantity+”
""
});
}
}
/**
*从各自的单元格中获取仓库、日期和数量的值。
*@返回{Object.}
*/
函数getActiveRowValues(工作表){
var cellRow=sheet.getActiveRange().getRow();
//获取仓库价值
var depotCell=sheet.getRange(“D”+cellRow);
var depot=depotCell.getDisplayValue();
//获取日期值
var dateCell=sheet.getRange(“E”+cellRow);
var date=dateCell.getDisplayValue();
//获取数量值
var quantCell=sheet.getRange(“F”+cellRow);
var quant=quantCell.getDisplayValue();
//返回带有值的对象
返回{
仓库:仓库,
日期:日期:,
数量:定量
} 
}

对不起,有什么问题吗?我希望在电子邮件主题中包括日期和数量,在电子邮件正文中包括日期、数量和仓库,以及电子表格的链接。这似乎与我已有的没有什么不同。我无法将此邮件发送给我,尽管对我来说,它看起来像是我想要的。作为一个完全的新手,我不确定我是否正确地输入了它——比如,我不确定你所说的“在你的条件内”是什么意思;这是您发送电子邮件的条件:if(cell.indexOf('F')!=-1){//send email}。当cell.indexOf('F')!=-1将为真表示活动单元格应位于F列中,然后才会触发此条件。对我来说,这种情况毫无意义,但我不知道整个情况。虽然如果你上面的代码运行良好,那么我认为这也应该运行良好