简体   繁体   中英

OnEdit with Mailapp on Google Script

I have a Google Spreadsheet with 4 columns including Products, Salesperson, Category and Status. What I am trying to reach is whenever a user choose Yes option on Status column and if that product category is also G, then sending an email using the MailApp. The e-mail should include the product value as well.

So far, I was able to sending an email. But I've really confused about the offset concept here and was not able to send the product in the email

The spreadsheet: https://docs.google.com/spreadsheets/d/1wVr0SGryNNvorVdDZEY1E6UDgh25_A5A2LhN1UNbIHE/edit?usp=sharing

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var sheetName = sheet.getName();
  var r = sheet.getActiveCell();
  var cell = sheet.getActiveCell().getA1Notation();
  if(ss.getActiveCell().getColumn() !== 5) return;//
  var row = sheet.getActiveRange().getRow();
  var cellvalue = sheet.getActiveCell().getValue().toString();
  var prod = sheet.getRange().offset(0, -2).getValues();
  var cat = cellvalue.offset(0, -1).getValues();
  var recipients = "email@email.com";
  var message = '';
  if(cellvalue === 'Yes' && cat === 'G') { 
    message = cell + ' in Sheet ' + sheetName + ' was changed to YES.';
    var subject = 'Cell Changed to YES';
    var body = message + ss.getUrl() + ' to view the changes' + prod;
    MailApp.sendEmail(recipients, subject, body);
   }

}

It's probably easier to use the object passed with the onEdit event instead of manipulating active cells and offsets.

This event object gives you the new value, so you can check if it is the one that you want ('Yes'). It also gives you the range that was edited, so you can use it to check if the change was in the correct column ('D') and to get the row that was modified. Once you have the row, you can use it to get the values of the other columns ('Products' and 'Cat') in that row.

Something like this should work:

function onEdit(event) {
  const statusColumnNumber = 4; // Indices of rows and columns start from 1, so column D is 4.
  if (event.range.getColumn() === statusColumnNumber && event.value === 'Yes') {
    const sheet = event.range.getSheet();
    const rowValues = sheet.getRange(event.range.getRow(), 1, 1, sheet.getLastColumn()).getValues().flat();
    const categoryColumnIndex = 2; // Indices of JavaScript arrays start from 0, so column C is in position 2 of the array.

    if (rowValues[categoryColumnIndex] === 'G') {
      const prodColumnIndex = 0;
      const prodValue = rowValues[prodColumnIndex];
      const recipients = "email@email.com";
      const subject = 'Cell Changed to YES';
      const message = event.range.getA1Notation() + ' in Sheet ' + sheet.getName() + ' was changed to YES. ' 
      const body = message + '\n' + sheet.getParent().getUrl() + ' to view the changes for ' + prodValue;

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

As mentioned in the comments of your question, since you use a service that needs permissions ( MailApp ), you'll need to create an installable trigger that calls this function.

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