简体   繁体   中英

Google Sheets - Script to send email when the cells in column R are less than or equal to 0

Premise: I have read similar posts on here but none seem to solve the problem, so please do not flag as a duplicate. Thanks.

I am trying to set up a script in GS that would send an email to someone if one of the cells in column R are less than zero.

(The values in column R are dependent on what is put in column A)

I have been trying with the following script but no email has been sent... Can you help me out with this please?

function sendEmailonEdit()
{
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var cellValue = ss.getActiveSheet().getActiveRange().getA1Notation();
    var getColumn = ss.getActiveSheet().getActiveRange().getColumn();
    var sheetname = ss.getActiveSheet().getName();
    var user = Session.getActiveUser().getEmail();
    var Toemail = 'userid@company.com';
    var subject = 'Warning -' + ss.getName();
    var body = 'Your file has a new entry in - ' + sheetname + ' Updated by - ' + 
        user + ' New Value in - '+ cellValue + '= '+ss.getActiveCell().getValue() +
        ' check file- ' + ss.getUrl();

    if(user !='abc@company.com' && Number(ss.getActiveCell().getValue()<= 0) && getColumn == 18)
    {
        MailApp.sendEmail(Toemail,subject, body);
    }
};

EDIT:

Simple triggers like onEdit(), onOpen() or onFormSubmit() have a limited set of possible actions because they run without authorization. For that reason a simple trigger cannot send an email.

You can add an UI button to manually see if there is some values to check.

If you want to send an email only for the newly edited fields, you should add something like a "status" column (column S in the example below). Something like this:

在此处输入图像描述

function workflow (){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getDataRange();
  var values = range.getValues();

  var cellValue;
  var sheetname = ss.getActiveSheet().getName();
  var user = Session.getActiveUser().getEmail();

  for (var i=0; i < values.length; i++){
    //[17] == Column R || [18] == Column S
    if (values[i][17] <= 0 && values[i][18] != "processed"){

    cellValue = values[i][17];
    var cell = 'R'+(i+1)
    var statusCell = 'S'+(i+1)

    var Toemail = 'mail@domain.com';
    var subject = 'Warning -' + ss.getName();
    var body = 'Your file has a new entry in ' + sheetname + '.\n'+'Updated by ' + user + '.\n New Value in '+ cell + '= '+cellValue+ '.\n check file ' + ss.getUrl();

    MailApp.sendEmail(Toemail,subject, body);

    ss.getSheetByName(sheetname)
      .getRange(statusCell)
      .setValue('processed')
    } else {
      var statusCell = 'S'+(i+1)
      ss.getSheetByName(sheetname)
        .getRange(statusCell)
        .setValue('processed')
    }
  }
}

This could works but i don't know if it's what you really want. It checks the column R looking for <= 0 values. When it founds one of them, an email is sended.

function workflow (){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getDataRange();
  var values = range.getValues();

  var cellValue;
  var sheetname = ss.getActiveSheet().getName();
  var user = Session.getActiveUser().getEmail();

  for (var i=0; i < values.length; i++){
    //[17] == Column R
    if (values[i][17] <= 0){

      cellValue = values[i][17];
      var cell = 'R'+(i+1)

      var Toemail = 'mail@domain.com';
      var subject = 'Warning -' + ss.getName();
      var body = 'Your file has a new entry in ' + sheetname + '.\n'+'Updated by ' + user + '.\n New Value in '+ cell + '= '+cellValue+ '.\n check file ' + ss.getUrl();

      MailApp.sendEmail(Toemail,subject, body);
    }
  }
}

In this case:

在此处输入图像描述

We will send 3 mails like this:

在此处输入图像描述

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