在非手动更改/编辑单元格时触发脚本-Google脚本

时间:2018-08-30 00:08:55

标签: google-apps-script google-sheets

每次使用“ ”更改为特定列中的“ ”时,我都会使用以下脚本发送电子邮件。

但是,只有在将单元格手动更改为“ ”并键入单元格时,才会发生这种情况。并基于on edit UI触发器触发。

但是该单元格是根据引用另一个工作表的公式进行更新的,该表引用了“ ”和“ ”,因此无需手动编辑/在此工作表的单元格上进行更新。

仅在从“ ”更改为“

的情况下,如何在不进行任何手动更改的情况下发送电子邮件

任何帮助将不胜感激。

function sendNotification(e){
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var ss = s.getSheetByName("Order Details")
  if(e.range.getColumn()==3 && e.value=='Yes'){
  var cell = ss.getActiveCell();
  var row = cell.getRow();

  var ordernumber = ss.getRange(row,4).getValue();  //Column D
  var sku = [{}]; sku = ordernumber.toString().split("-")

 var sizewidth = ss.getRange(row,5).getValue();  //Column E
 var sizeheight = ss.getRange(row,6).getValue();  //Column F
 var qty = ss.getRange(row,8).getValue();    //Column H
 var country = ss.getRange(row,10).getValue();  //Column J
 var tube = ss.getRange(row,9).getValue();   //Column I
 var paintingimage = ss.getRange(row,7).getValue();  //Column G
 var orderlink = ('http://testly/Km345TS');



 MailApp.sendEmail({
  to: "xxx@gmail.com",
  subject:  country + " New Order No. " + ordernumber, // note the spaces between the quotes...
  //attachment: [file,blob],
  htmlBody: "Hello XYZ,  <br><br>"+
   "Please find order details below. <br><br>"+
    sku[1] + "<br><br>" +
   "Size - Width: " + sizewidth + " x " + "Height: " + sizeheight + "<br><br>"+
   "Quantity - " + qty + "<br><br>"+
   "- It needs to be tube rolled"+ "<br>" +
   "- Shipment to " + country + "<br>" +
   "- Order image is " + paintingimage + "<br><br>" +
   "Please fill in cost and delivery details at this link " + orderlink + "<br><br>" +
   "The order is for a customer from " + country + "<br><br>" +
   "Thanking you, <br>" +
   "ABC",            
                   })
 }
} 

更新:解决方案-非常感谢罗恩。

function sendNotification2(){
  var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet 1");
  var data = sSheet.getRange(2, 1, sSheet.getLastRow(), sSheet.getLastColumn()).getValues(); //get the values of your table, without the header
  var EMAIL_SENT = 'EMAIL_SENT';

  for (var i = 0; i < data.length; i++) {
    var row = data[i];

    var send = row[16];  //Column Q
    var emailSent = row[17];   //Column R
    var ordernumber = row[4];  //Column E
    var country = row[10];  //Column K
    var orderlink = ('http:/testly/Khghgy');
    var shipaddress = row[18];  //Column S

 if (send == "Yes"){
    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates

 MailApp.sendEmail({
   to:  "xx@gmail.com",
  subject:  country + " Ship Order No. " + ordernumber, // note the spaces between the quotes...
  htmlBody: "Hello XYZ,  <br><br>"+

   "Thanking you, <br>" +
   "ABC",            
    })
  sSheet.getRange(i+2, 18).setValue(EMAIL_SENT);
   }
  }
 }
} 

1 个答案:

答案 0 :(得分:1)

如果要在编辑更改时触发电子邮件,则可以创建一个单独的功能来监视另一张纸上的编辑,然后调用上述功能来发送电子邮件。

类似:

function onEdit(){
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var ss = s.getSheetByName("Other Sheet Name");
  if(e.range.getColumn()==3 && e.value=='Yes'){  //change this if the data setup is different on the other sheet
    sendNotification();
  }
}

问题在于此设置随后将向电子邮件表上的每个“是”发送一封电子邮件。可以使用“ Email_Sent”指示符对此进行纠正(您可以查找一下-许多可用示例)。

**正如我在the last question中提到的,另一种选择是每隔5分钟,5分钟,10分钟或???触发sendNotification函数。这不会提供即时电子邮件,但实际上会如此。