Google Sheets Notifications When Cell is Edited?

柔情痞子 提交于 2020-03-05 07:45:13

问题


I have a spreadsheet that I would like to have send an e-mail to a specified user whenever a particular cell is edited. So, for example, the trigger column is column J. My goal is that when a cell (J4, for example) is edited, it will send an automatic e-mail to the e-mail address that is provided in cell A4. I know this will require a script.


回答1:


Here's what I came up with:

function onEditTrigger(e){
  var range = e.range;
  var intCol = range.getColumn();

  if (intCol == 10)                                           // Check for column J
  {
    var intRow = range.getRow();                              // Get the row number of the edited cell
    var sheet = SpreadsheetApp.getActiveSheet();
    var dataRange = sheet.getRange(intRow, 1, 1, 3);          // Select columns A to C on the same row as the edited cell
    var dataValues = dataRange.getValues();                   // Get the values in the selected range and store them in the dataValues array

    MailApp.sendEmail(dataValues[0][0], "Notification", "Value in column C: " + dataValues[0][2]); // dataValues[0][0] = e-mail address, dataValues[0][2] = additional information
  }
}

Then manually set the trigger to onEdit on the Resources menu of the Script Editor.

The script is pretty much self-explanatory as I put some comments there. But basically, the script first checks if a user edited the "trigger" column. If it is the "trigger" column, it gets the row number of that edited cell and gets the corresponding e-mail address in column A, and the additional information in column C of the same row. A notification e-mail is then sent to that e-mail address with the subject "Notification" and the value in column C as the body of the e-mail message.



来源:https://stackoverflow.com/questions/31778653/google-sheets-notifications-when-cell-is-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!