robisy
New Coder
Code:
function sendemail(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Sheet1"
var sheet = ss.getSheetByName(sheetname);
// parameters to get a data range
var datafirstrow = 2;
var dataColU = 4; // Column D = 4
var dataColTarget = 8; // Column Z = 26
// get the last row
var LR = sheet.getLastRow();
// Logger.log("DEBUG: the last row = "+LR)
if (LR <= datafirstrow){
// Logger.log("DEBUG: no data")
return;
}
// get range for column U
var sourcerange = sheet.getRange(datafirstrow, dataColU, LR-datafirstrow+1)
// Logger.log("DEBUG: the range = "+sourcerange.getA1Notation())
var sourcevalues = sourcerange.getValues();
// get the target range (assume column V)
var targetrange = sheet.getRange(datafirstrow, dataColTarget, LR-datafirstrow+1)
// Logger.log("DEBUG: the target range = "+targetrange.getA1Notation())
var targetvalues = targetrange.getValues();
// calculate the number of rows of data
var numrows = LR-datafirstrow+1;
// Logger.log("DEBUG: the number of rows = "+numrows)
for (var i=0;i<numrows;i++){
if (sourcevalues[i][0] != targetvalues[i][0]){
// values do not match, so Column U has changed
// so send email
var rownumber = i+datafirstrow;
GmailApp.sendEmail("[email protected]","Subject", "New Name Added - See Cell U" + rownumber + " Sheet Name")
//Logger.log("DEBUG: i="+i+", send an email for row number "+rownumber);
}
}
// update the revised source data to target range
targetrange.setValues(sourcevalues);
SpreadsheetApp.flush();
return;
}
How can I add a cell value in addition to the rownumber in the email body? For example: if a cell contains the number 6, I would like to see the value 6 in the body of the email
I would also like the email body to include the values from adjacent cells in a given row. Can anyone help me how to do this?