简体   繁体   中英

Send email from Google Sheet when cell is certain value

Here is an example of the sheet I am trying to make this work for: https://docs.google.com/spreadsheets/d/1M79ki9QVRkfkwy1uWyNAVddeyPpcvqDppi3Et-b-cLw/edit?usp=sharing

The goal is that an email is sent when column I is greater than 2 and column K is null. Column I is a count formula based on columns CH, which are manually filled in.

Here is the script I have, but it doesn't seem to be working:

function sendEmail() {
  
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Workable");

  var listing_id = sheet.getRange("B:B").getValues();
  var flag_count = sheet.getRange("I:I").getValues();
  var action_type = sheet.getRange("K:K").getValues();

  var subject = 'New Listing Flagged';
  var message = 'Listing' + listing_id + 'has been flagged. Please resolve ASAP in the QA Google Sheet. Thank you!';
  var email_address = 'allyson@hipcamp.com';

  if (flag_count > 2 && action_type == ""){MailApp.sendEmail(email_address, subject, message)}
}

What do I need to change to make it work?

I got your latest code in the sample sheet. Since you've decided to use onEdit trigger, you need to use the installable onEdit trigger because you are using MailApp.sendEmail() service which requires authorization.

Pre-requisite (installable trigger):

在此处输入图片说明

在此处输入图片说明


Sample Code:

function sendNotification1(e) {
  Logger.log(JSON.stringify(e));
  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var sheet = ss.getActiveSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var startRow = 2;
  var startCol = 3; //Column C
  var endCol = 8; //Column H

  if(sheet.getName() == "Workable"
    && col >= startCol
    && col <=  endCol
    && row >= startRow
    && sheet.getRange(row,9).getValue() > 2 //flag_count column > 2
    && sheet.getRange(row,11).getValue() == "" //action column null
    )
  {
    var campground_id = sheet.getRange(row,2).getValue();
    var email = 'your@email.com';
    var subject = "New Listing Flagged for Fraud";
    var body = 'Campground ' + campground_id + ' has been flagged for fraud. Please resolve ASAP in the QA Google Sheet. Thank you!';
    MailApp.sendEmail(email, subject, body);
  }
 }

What it does?

  1. Your column I value depends on the value provided in column CH. Hence your trigger should be the change in values done in column CH (column index 3-8). I include an additional condition to check the current active sheet to verify that the modified cells are in sheet Workable . Check the value in column I if greater than 2. Lastly, check if column K is null/empty
  2. Send the email

Output:

在此处输入图片说明

在此处输入图片说明

(Update)

Checkbox column included that will act as a checker button to trigger the email notification.

Sample Code:

function sendNotification(e) {
  Logger.log(JSON.stringify(e));
  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var sheet = ss.getActiveSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var startRow = 2;

  if(sheet.getName() == "Workable"
    && col == 9
    && row >= startRow
    && sheet.getRange(row,9).isChecked()
    && sheet.getRange(row,10).getValue() > 2 //flag_count column > 2
    && sheet.getRange(row,12).getValue() == "" //action column null
    )
  {
    var campground_id = sheet.getRange(row,2).getValue();
    var email = 'your@email.com';
    var subject = "New Listing Flagged for Fraud";
    var body = 'Campground ' + campground_id + ' has been flagged for fraud. Please resolve ASAP in the QA Google Sheet. Thank you!';
    MailApp.sendEmail(email, subject, body);
  }
 }

Sample Sheet:

在此处输入图片说明

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