当不同列中的值增加时发送电子邮件通知

时间:2017-06-27 11:06:02

标签: google-apps-script google-sheets

我有一张包含从A1到BK288的值数组的工作表。此数组是基于另一个每5分钟更新一次的电子表格计算的百分比。现在,我希望收到一封电子邮件通知,当不同列中同一行上的一个值增加并变得大于固定值时。然后,电子邮件应包含正文上的该卷和主题列的标题。 我写了这个脚本,但是对于单个单元格,我不知道是否有办法为每一列扩展它,除了为每一列写一个函数。此外,有没有办法触发自动通知?考虑表单每5分钟更新一次值。感谢

function getValue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Perc");
  var value = sheet.getRange("A288").getValue();
  var title = sheet.getRange("A1").getValue();
  if(value >= "2") sendEmail(value, title)
};


function sendEmail(value, title){
  var recipient="someemail@gmail.com";
  var subject=title + " price";
  var body=title + " price has changed by " + value + "%";
  MailApp.sendEmail(recipient, subject, body);
};

1 个答案:

答案 0 :(得分:0)

这是一个解决方案。

将数据一个接一个地放在数组和进程列上。我还在多个产品更改时添加了一个解决方案,因此只发送一封包含所需信息的邮件,以预先提供每日配额。

编辑:仅分析第288行

function compareValue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Perc");
  var data = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
  var list = []; // List of change greater than 2

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

    var title = data[0][i]; // Get the title of the element
    var value = parseFloat(data[data.length -1][i]); // Get the value on last row (288th)

    if(value >= 2){ // If the value greater than 2, add it on a list

      var element = [title, value];

      list.push(element)

    }
  }

  if(list.length == 1){ // If only 1 element changing, send classic mail
    sendEmail(list[0][1], list[0][0]);
  }
  if(list.length >= 2){ // If 2 or more element changing, send mail with all the change
    sendEmail(list);
  }
}

function sendEmail(value, title){
  var recipient="pierre.marie.richard@gmail.com";
  var subject=title + " price";
  var body=title + " price has changed by " + value + "%";
  MailApp.sendEmail(recipient, subject, body);
}

function sendEmail(list){
  var recipient="pierre.marie.richard@gmail.com";
  var subject="Multiples prices have changed";
  var body = "";
  for(var i=0; i<list.length;i++){ // Get throught the list to write the body
    body=body + list[i][0] + " price has changed by " + list[i][1] + "\%\n";
  }
  MailApp.sendEmail(recipient, subject, body);
}
相关问题