简体   繁体   中英

Send email from cell value google sheet, with other columns in email

Apologises as I know this has been asked a few times, which I have learnt from other questions, however I'm still stuck and not sure how to continue.

I'm trying to make a google script to send an email notification to more then one person, based on a cell change, also sending in the email multiple cell values in the same row?

eg If Column 'C' changes, can I send an email with data from Column 'A' and 'B' in the same Row of the change in column 'C'?

This is the script I've been trying to play around with but not sure how to add multiple e-mails, and values from two more cells.

function sendNotifications() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = email@domain.com";
var message = '';
var cellA = ''
if(cell.indexOf('B')!=-1){ 
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};

The code below checks if the edited cell was in column C and, if the condition is met, gets the values for columns A and B from the edited row. In Google Apps Script, onEdit() is the built-in function that fires automatically when you make changes in your spreadsheet. The 'e' parameter represents the event object that you can inspect to get more details about the context in which the event occurred

function onEdit(e){

  var editRange = e.range; // e is the event object generated by the Edit event
  var editRow = editRange.getRow();   // get row and column for the edited cell
  var editCol = editRange.getColumn();


  var sheet = SpreadsheetApp.getActiveSpreadsheet()
                            .getSheetByName("Sheet1"); // put your sheet name between the brackets

  if (editCol == 3) {   // checking if the edited cell was in column C

      var range = sheet.getRange(editRow, 1, 1, editCol - 1); // Take 1st cell in the edited row and make it span 1 row and 2 columns
      var values = range.getValues(); // get values array for the range
      Logger.log(values);

  }

}

The resulting variable ('values') is an array, so you must loop through it to get string values. As for the emails, you could store multiple emails in a cell, separated by comma. Calling split() method on a string will get you an array of values (emails) that you can then pass to the sendEmail method as the first parameter.

  var cellValue = "email@example.com,anotheremail@example.com,yetanotheremail@example.com";
  var recipients = cellValue.split(",");  
  MailApp.sendEmail(recipients, "From google-apps-script", "hello from google apps script")

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