简体   繁体   中英

Google Script - Automatically send email IF a certain cell is edited?

Basically I'm creating a toner stock management sheet, and I've got the emails to send if a stock gets to 1. It does this with an installable onEdit trigger (I use a simple onEdit function to add the toners to a log sheet). My emails sending works correctly, but the problem is if a stock is down to one, it will send me an email every time I edit the stock of another. Is there a way to make the email function run only if the cell containing the relevant stock number is changed?

Here's my code

function onEdit(event) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

    if(s.getName() == "Toners" && r.getColumn() == 5 && r.getValue() > "") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Log");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
    }
}

//I included the above code just in case someone finds an easier way to put it in there and then call the EmailPrinterGroup1 functon..




function EmailPrinterGroup1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var PrinterGroupStock = ss.getSheetByName("Notification").getRange("B1").getValue();
  var EmailList = ["abc@123.org"];
  if(PrinterGroupStock <= 1){
      var email = Session.getActiveUser().getEmail();
      GmailApp.sendEmail(EmailList, 'Toner Stock Alert - Communal Areas',
                        'There is only 1 toner left in stock for the communal areas, please re-stock ASAP. Click the link below to go to the Toner Stock Management sheet. \n\n'+ss.getUrl(),
                        {from:"xyz@321.org"});
      PropertiesService.getScriptProperties().setProperty("last", PrinterGroupStock);
    }
}

In this case, the cell I want checked is B1 , which uses a COUNTIF formula (the email will send if the result of the calculation is on 1), and it's on the "Notification" sheet (The main sheet page which gets edited the most is called "Toners".) Thanks!

You can use a cell in the sheet to store a flag that remembers if value of B1 was 1 before. In the following example, I'm using A1 .

function onEdit(event) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

    if(s.getName() == "Toners" && r.getColumn() == 5 && r.getValue() > "") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Log");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
    }

  var stock_range = ss.getSheetByName("Notification").getRange(1, 2);
  var stock_is_one_flag = ss.getSheetByName("Notification").getRange(1, 1).getValue();

  if (stock_range.getValue() === 1 && stock_is_one_flag === 0){
    EmailPrinterGroup1();
    ss.getSheetByName("Notification").getRange(1, 1).setValue(1);
  } else if (stock_range.getValue() != 1 && stock_is_one_flag != 0){
    EmailPrinterGroup1();
    ss.getSheetByName("Notification").getRange(1, 1).setValue(0);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM