简体   繁体   中英

Send an email from Google Sheets after a change to one column

I have a simple google sheet collecting data. We use the sheet to follow up events. I want to send an email based on a change to a cell (column W) which will always be the same column. The email is always to the same person in the business.

I have got to the point of being able to make a change to ANY cell in the sheet and the first email is sent. But I cannot figure out how to lock it down to only look at one column.

Ideally the event will leave a footprint so I wanted to add a comment in the cell. It is working also.

What I need - how do I limit the script to only look at column W??? Thank you.

The columns also pull through tot he email fine.

function onEdit(event)
{
  var ss = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  r.setComment("Email Sent To Warrens: " + (new Date()));
}
function CustomEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A2:W13");
var UserData = range.getValues();
var text = 'm******n@gmail.com';
for (i in UserData) {
var row = UserData[i];
var Title = row[6];
var SURNAME = row[7];
var EmailAddress = row[9];
var Rentalagreementnumber = row[3];
var Startofhiredate = row[4];
var Invoicereason = row[5];
var Amountdue = row [2];
var Rentallocation = row [8];  
MailApp.sendEmail(text, "a***@*****.co.uk", "SUBJECT", "EMAIL TEXT");
}
}

Get the range from the event object , then use the function range#getA1Notation() to check which column was edited.

function onEdit(event)
{
  var range = event.range; 
  if(range.getA1Notation().charAt(0) === 'W'){
      sendEmail();
      range.setComment("Email Sent To Warrens: " + (new Date()));
  }
}

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