简体   繁体   中英

How to send a notification based on changed cell on google spreadsheet

I know it is similar to email notification if cell is changed

but I would like to do the following:

In column AI have the reminder Name, column BI have the Telephone, and column F the status

The status values are 1,2,3,4,5

So Basically I want to send an email when Column F changes to 4,5 with the information of Column A and column B.

Example: Reminder Name has just been changed to status 4, please give them a call: Phone number

function checkReminder() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  var sheet = spreadsheet.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var startRow = 2;
  var range = sheet.getRange(2,5,lastRow-startRow+1,1 );
  var numRows = range.getNumRows();
  var sV = range.getValues();
  range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
  var reminder_info_values = range.getValues();

  var warning_count = 0;
  var msg = "";


  for (var i = 0; i <= numRows - 1; i++) {
    var statusC = sV[i][0];
    if(statusC > 3) {
      var reminder_name = reminder_info_values[i][0];

      msg = msg + "The reminder: "+reminder_name+" changed status to "+statusC +" ";
      warning_count++;
    }
  }

  if(warning_count) {
    MailApp.sendEmail("email@email.com",
        "CC Changed status of "+ reminder_name, msg);
  }

};

I don't know how to make it work, please help

I also tried this Send Email when value changes in Google Spreadsheet

your script was missing an important feature : you should remember if a mail has already been sent before sending it otherwise each time you read the cell value a mail will be sent.

There are many ways to achieve that, adding a column with a "mail sent" flag is probably the most common but definitely not the one I prefer ...

Here is a version that colorizes the cell when a mail is sent, I choose red but you can change to a more discrete color of course. (I added a logger so you can see the colors hex code easily)

function checkReminder() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  var sheet = spreadsheet.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var startRow = 2;
  var range = sheet.getRange(2,5,lastRow-startRow+1,1 );
  var numRows = range.getNumRows();
  var sV = range.getValues();
  var bGColors = range.getBackgroundColors();// get range background colors
  var range_reminder = sheet.getRange(2, 1, lastRow-startRow+1, 1);
  var reminder_info_values = range_reminder.getValues();

  var warning_count = 0;
  var msg = "";


  for (var i = 0; i <= numRows - 1; i++) {
    var statusC = sV[i][0];
    Logger.log(bGColors[i][0]);
    if(statusC > 3 && bGColors[i][0] != '#ff0000') { // if not already sent
      var reminder_name = reminder_info_values[i][0];
      bGColors[i][0] = '#ff0000';
      msg = msg + "The reminder: "+reminder_name+" changed status to "+statusC +" ";
      warning_count++;
    }
  }
  range.setBackgroundColors(bGColors);// update sheet with colors
  if(warning_count>0) {
    MailApp.sendEmail("email@email.com",
                      "CC Changed status of "+ reminder_name, msg);
  }

};

You can set a trigger calling this script either onEdit or on a timer, both method will work, it's more a matter of choice (unless you absolutely need to have an instantaneous reaction).

You can use the onEdit()

If the value is in Column F and it now is 4 or 5 then send the email.

https://developers.google.com/apps-script/understanding_events

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