需要为多个列和工作表发送通知

时间:2017-02-20 14:11:33

标签: google-apps-script

为了尝试自动化我的一些工作,我开始学习谷歌脚本的基础知识。

我有一个电子表格,我希望在将数据输入一列或另一列时发送电子邮件通知。此电子表格中还有两个选项卡,我希望这样做。

脚本的当前结果是第二个“发送通知”功能的电子邮件。

问题:如何让脚本考虑这两个功能?

我知道这个代码可以通过更好的方式使用IF功能来缩小,但我不知所措。

对于某些情况:这用于制造业务。生产由一家非现场公司完成,当他们在任何一张纸上输入数量到第10栏时,我希望它向一组人发送一份我工作的电子邮件。同样,当产品质量测试完成后,我希望能够输入第12列,并向异地公司发送电子邮件。

function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
//Get Active cell
  var mycell = ss.getActiveSelection();
  var cellcol = mycell.getColumn();
  var cellrow = mycell.getRow();
//Define Notification Details
  var recipients = "ryan.helms@company.com";
  var subject = "Disc production was entered on the "+ss.getName();
  var body = ss.getName() + " has been updated with an amount produced.  Visit " + ss.getUrl() + " to view the quantities entered.";
//Check to see if column is A or B to trigger
  if (cellcol == 10)
  {
//Send the Email
  MailApp.sendEmail(recipients, subject, body);
  }
//End sendNotification
}
function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
//Get Active cell
  var mycell = ss.getActiveSelection();
  var cellcol = mycell.getColumn();
  var cellrow = mycell.getRow();
//Define Notification Details
  var recipients = "ryan.helms@company.com";
  var subject = "A lot of disc has been RELEASED by XYZ Company";
  var body = ss.getName() + " has been updated with a lot of disc that were released by XYZ Company.  Visit " + ss.getUrl() + " to view this updated information.";
//Check to see if column is A or B to trigger
  if (cellcol == 12)
  {
//Send the Email
  MailApp.sendEmail(recipients, subject, body);
  }
//End sendNotification
}

1 个答案:

答案 0 :(得分:0)

您需要设置onEdit installable trigger(如果您还没有)并将其分配给以下函数:

function onEditEmailSender(e) {
  var sheetName = e.range.getSheet().getName();

  if (sheetName === "tab1" || sheetName === "tab2") {
    var col = e.range.getColumn();

    if (col === 10)
      //send col 10 email
    else if (col === 12)
      //send col 12 email
  }
}

onEdit触发器传递带有各种信息的参数,在您的情况下最有用的是e.range。此Range对象对应于已编辑以触发onEdit事件的单元格。您可以使用它来获取工作表的名称(您称之为选项卡)和已编辑的列。

使用该信息,您可以发送相应的电子邮件。祝你好运!