需要发送多个列和床单的信息



为了使我的某些工作自动化,我开始学习Google脚本的基础知识。

我有一个电子表格,当数据输入一列或另一列时,我想在其中发送电子邮件通知。我希望在此电子表格中也有两个选项卡。

脚本的当前结果是第二个" sendnotification"功能上的电子邮件。

问题:如何获得脚本来考虑这两个功能?

我知道可以通过更好的方式使用if fucntion来凝结此代码,但我很茫然。

在某些情况下:这用于制造运营。生产是由一家异地公司完成的,当他们将数量输入第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
}

您需要设置onEdit可安装触发器(如果还没有)并将其分配给以下功能:

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。该范围对象对应于触发onEdit事件的编辑的单元格。您可以使用它来获取表格的名称(您称之为标签)和已编辑的列。

使用这些信息,您可以发送适当的电子邮件。祝你好运!