Update Date or Send Email when a Specific Range is Edited - google-apps-script

How do I tell cell C2 to set the date when cells A2 or B2 have been updated?
Further, how do I trigger the sending of my email function when A2 or B2 have been updated?
My issue is that onEdit fires anytime the document is edited at all, but I only want an action if a specific range is edited.
For the sending of emails, I've found and edited a script that almost works, but it sends me email notifications whenever any cell in the document is changed rather than just when column G is changed. Any suggestions?
function sendNotification() {
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 = "me#email.com";
var message = 'Cell value has been changed';
if(cell.indexOf('G')!=-1){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};
Does this have to do with my onEdit() function being off?
For anyone that needs the final scripts
I ended up splitting this up in two separate functions. Here are the finished scripts.
The first one is the email notifications
/* This function send an email when a specified range is edited
* The spreadsheets triggers must be set to onEdit for the function
*/
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Define Notification Details
var recipients = ENTEREMAILHERE;
var subject = "Update to "+ss.getName();
var body = ss.getName() + "has been updated. Visit " + ss.getUrl() + " to view the changes.";
//Check to see if column is A or B to trigger
if (cellcol == EDITMECOLUMN)
{
//check for row to trigger
if (cellrow == EDITMEROW)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
}
And here is the one for time stamps
/* This function saves the date in a cell
* every time a specific row or column is edited
* The spreadsheets triggers must be set to onEdit for the function
*/
function setDate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Check to see if column is A or B to trigger
if (cellcol == EDITMECOLUMN)
{
//check for row to trigger
if (cellrow == EDITMEROW)
{
//Find cell and set date in a defined cell
var celldate = sheet.getRange(EDITMEROW, EDITMECOLUMN);
celldate.setValue(new Date());
//end set date
}
}
}

This should work or at least give you the idea as I do something very similar. I usually set the trigger for this to be onEdit()
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Check to see if column is A or B to trigger email
if (cellcol == 1 || cellcol ==2)
{
//check for row to trigger email
if (cellrow ==2)
{
//Find cell and set date
var celldate = sheet.getRange(2, 3);
celldate.setValue(new Date());
//end set date
var cellvalue = mycell.getValue().toString();
var recipients = "me#email.com";
var message = 'Cell value has been changed';
if(cell.indexOf('G')!=-1){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
}
}
}

Your onEdit(event) needs to check the event.range and make decisions based on that.
See Understanding Events.
Here's an answer to a similar question.

Related

Sending email onEdit with the row data embedded into email in Google Sheets

I am creating a google script that when a person changes a column to "Approved" it sends an email out that grabs the cell that was changed. However, I am not a expert scripter at all and am having a hard time in figuring out how to get the script to call the information from the actual row into the email either.
Here is my script:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var cell = sheet.getActiveCell().getA1Notation();
var rowData = sheet.getActiveRange().getRow();getValue();
var row = sheet.getActiveRange().getRow();
var cellvalue = sheet.getActiveCell().getValue().toString();
var recipients = "test#sendemail.com";
var message = '';
if(cellvalue === 'Approved'){
message = 'Cell ' + cell + ' ' + ' ' + rowData + ' was changed to Approved.';
var subject = 'Cell Changed to Approved in Interns List';
var body = message;
MailApp.sendEmail(recipients, subject, body);
}
}
I couldnt find any solution thats been done already that would include the entire row that was triggered.
Try this:
function onMyEdit(e) {
const sh=e.range.getSheet();
var rowData = sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues()[0].join(',');
var recipients = "test#sendemail.com";
var message = '';
if(e.value == 'Approved'){
message = 'Cell ' + e.range.getA1Notation() + ' ' + ' ' + rowData + ' was changed to Approved.';
var subject = 'Cell Changed to Approved in Interns List';
MailApp.sendEmail(recipients, subject, message);
}
}

Specify Google Sheet tab in script

I am trying to get a script to only look at a specific tab/sheet when someone edits specific columns to send an automated email notification. It is however sending email notifications for any edits on all tabs. How do can I specify the sheet: "CT - Training"
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = activeSpreadsheet.getSheetByName('CT - Training');
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Define Notification Details
var recipients = "email#email.email";
var subject = "Update to "+ss.getName();
var body = ss.getName() + "has been updated. Visit " + ss.getUrl() + " to view the changes.";
//Check to see if column is A or B to trigger
if (cellcol == 17);
//Check to see if column is A or B to trigger
if (cellcol == 20);
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}

Google Script Failing on message variable

I have two scripts working on the same sheet, one works fine, the other fails every time. The catch? they're duplicates of each other, just pointing at different sheets.
The debuggers announce that the 'message' variable doesn't exist, but again, the first copy here works great, the second just never runs.
Posting below
Functioning:
function onEdit(e){
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 email = 'fakeemail#fake.com';
var updater = Session.getActiveUser();
var subject = 'Tracker Updated by ' + updater;
var text = sheet.getDataRange().getValues();
if(cell.indexOf('I')!=-1){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var body = 'Page Updated for ' + message + ' by; ' + updater;
GmailApp.sendEmail(email, subject, body);
}
;
and the failing one
function onEdit(e){
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 email = 'fakeemail#fake.com';
var updater = Session.getActiveUser();
var subject = 'Tracker Now Updated by ' + updater;
var text = sheet.getDataRange().getValues();
if(cell.indexOf('Votes!L')!=-1){
message = sheet.getRange('Votes!A'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var body = 'Page Updated for ' + message + ' by; ' + updater;
GmailApp.sendEmail(email, subject, body);
}
;
So what gives?

A simple YES in a cell in column A triggers EMAIL

I've searched through the forums and the answer is most likely out there but I need some assistance.
If the letter "Y" is input in column A, I need an automatic email triggered to a predetermined list of people. Ideally, this email would include an automatically generated email body according to the row data (Item, vendor, total, job, etc.).
This is what I have written so far. It sends an email out every time I make a change. I have tried, unsuccessfully, to refine the project's triggers and the if (Index of) bit.
function sendNotification() {
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 = "me#gmail.com";
var message = '';
if(cell.indexOf('A')!="Y"){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};
You're almost there!
In your if statment:
if(cell.indexOf('A')!="Y"){
You need to evaluate two things, first if the user is editing the column A and second if the value of the cell is "Y".
For the first one, you already have the value of the current cell in cellvalue, so just evaluate if cellvalue == "Y".
For the second one, you need to evaluate if the user is editing the column A, you can use .getColumnIndex()this will get the index of the range, in this case the cell, column A will return 1, so you evaluate sheet.getActiveCell().getColumnIndex() == 1
Now, you just need to use the logical operator && to check if both expressions evaluate to true.
Your line should look like this:
if(cellvalue == "Y" && sheet.getActiveCell().getColumnIndex() == 1){
Finally to make sure the email is sent only if the line above evaluates to true, move the subject, body and MailAppinside the if statement:
function sendNotification() {
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 = "me#gmail.com";
var message = '';
if(cellvalue == "Y" && sheet.getActiveCell().getColumnIndex() == 1){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue();
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
}
};

Sending an email whenever a cell is changed in a Google spreadsheet

I am trying to set up a script for a google spreadsheet that will email a specific person whenever a cell in Column M is modified to 'y'. I found this script,
email notification if cell is changed
and I am trying to modify it to suit my needs, but I am having an issue getting it to work. This is my script as it stands now.
function onEdit(e){
Logger.log(e)
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss)
var sheet = ss.getActiveSheet();
Logger.log(sheet)
var cell = ss.getActiveCell().getA1Notation();
Logger.log(cell)
var row = sheet.getActiveRange().getRow();
Logger.log(row)
var column = sheet.getActiveRange().getColumn();
Logger.log(column)
var cellvalue = ss.getActiveCell().getValue().toString();
Logger.log(cellvalue)
var recipients = "08cylinders#gmail.com"; //email address will go here
var message = '';
if(cell.indexOf('M')!=-1){
message = sheet.getRange('M'+ sheet.getActiveCell().getRowIndex()).getValue()
}
Logger.log(message)
Logger.log(cell)
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + to view the changes on row: ' + row; //New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
}
If anybody has an idea of what I'm missing, I would greatly appreciate any help.
Thank you,
Victor
i think this may solve your event problem:
function onEdit(e) {
var ssActive = e.source;
var ssActiveRange = ssActive.getActiveRange();
var ssActiveRangeVal = ssActiveRange.getValue();
var ActiveRow = ssActiveRange.getRow(); // if needed to put on body's message
var ActiveColumn = ssActiveRange.getColumn();
if((ssActiveRangeVal=='y' || ssActiveRangeVal=='Y') && ActiveColumn==13){ // ActiveColumn==13 for M
// write down your mail code here
}
}
You cannot send an email from the onEdit trigger. What you need to do is save the edits as a Document Property and then setup a 1-minute time based trigger that sends the content of the property in an email and flushes the queue.