编辑特定范围时更新日期或发送电子邮件

时间:2013-05-13 19:00:49

标签: google-apps-script google-api google-sheets google-spreadsheet-api

如何告知单元格C2设置更新单元格A2或B2的日期? 此外,如何更新A2或B2时如何触发发送电子邮件功能?

我的问题是onEdit会在任何时候编辑文档时触发,但我只想在编辑特定范围时执行操作。

对于发送电子邮件,我发现并编辑了几乎可以正常运行的脚本,但只要文档中的任何单元格发生更改而不是仅在更改G列时,它就会向我发送电子邮件通知。有什么建议吗?

function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = ss.getActiveCell().getA1Notation();
  var row = sheet.getActiveRange().getRow();
  var cellvalue = ss.getActiveCell().getValue().toString();
  var recipients = "me@email.com";
  var message = 'Cell value has been changed';
  if(cell.indexOf('G')!=-1){ 
    message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
  }
  var subject = 'Update to '+sheet.getName();
  var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view     the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' +     message + '»';
  MailApp.sendEmail(recipients, subject, body);
};

这是否与我的onEdit()函数关闭有关?

对于任何需要最终脚本的人

我最终将它分成两个独立的功能。这是完成的脚本。

第一个是电子邮件通知

/* This function send an email when a specified range is edited
 * The spreadsheets triggers must be set to onEdit for the function
*/

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 = ENTEREMAILHERE;
      var subject = "Update to "+ss.getName();
      var body = ss.getName() + "has been updated.  Visit " + ss.getUrl() + " to view the changes.";
  //Check to see if column is A or B to trigger
      if (cellcol == EDITMECOLUMN)
      {
  //check for row to trigger
        if (cellrow == EDITMEROW)
        {
  //Send the Email
      MailApp.sendEmail(recipients, subject, body);
      }
  //End sendNotification
 }
}

这是时间戳的一个

/* This function saves the date in a cell
 * every time a specific row or column is edited
 * The spreadsheets triggers must be set to onEdit for the function
*/

function setDate() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
  //Get Active cell
      var mycell = ss.getActiveSelection();
      var cellcol = mycell.getColumn();
      var cellrow = mycell.getRow();
  //Check to see if column is A or B to trigger
      if (cellcol == EDITMECOLUMN)
      {
  //check for row to trigger
        if (cellrow == EDITMEROW)
        {
  //Find cell and set date in a defined cell
      var celldate = sheet.getRange(EDITMEROW, EDITMECOLUMN);
      celldate.setValue(new Date());
  //end set date
        }
      }
 }

2 个答案:

答案 0 :(得分:0)

您的onEdit(event)需要检查event.range并根据该决定做出决定。

请参阅Understanding Events

Here's an answer提出了类似的问题。

答案 1 :(得分:0)

这应该起作用或至少给你一个想法,因为我做了一些非常相似的事情。我通常将触发器设置为onEdit()

   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();
      //Check to see if column is A or B to trigger email
      if (cellcol == 1 || cellcol ==2)
      {
        //check for row to trigger email
        if (cellrow ==2)
        {
      //Find cell and set date
      var celldate = sheet.getRange(2, 3);
      celldate.setValue(new Date());
     //end set date
      var cellvalue = mycell.getValue().toString();
      var recipients = "me@email.com";
      var message = 'Cell value has been changed';
      if(cell.indexOf('G')!=-1){ 
        message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
      }
      var subject = 'Update to '+sheet.getName();
      var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view     the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' +     message + '»';
      MailApp.sendEmail(recipients, subject, body);
    }
    }
    }