Sending an email whenever a cell is changed in a Google spreadsheet - google-apps-script

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.

Related

Send an e-mail when a new line is entered into Google Sheet

I have a spreadsheet that is a list of RFIs from a client. I want to be notified when a new questions is added to column F. I found this code but I haven't been able to make the correct changes to get exactly what I need. I have the trigger set to onEdit.
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('F')!=-1){
message = sheet.getRange('F'+ 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);
};
Ultimately what I need... an e-mail sent only when a new question is added into column F or when an existing question in column F is changed. The code above only sends an e-mail when I "run" from inside the script editor, and the e-mail contains data for the active cell rather than the new question in F or the edited question in F.
Any assistance is greatly appreciated,
Thank you.

Cannot call DriveApp from Google sheet via onEdit() trigger [duplicate]

really feel like I'm missing something with the Spreadsheet object scripts.
I'm trying to automatically email collaborators onEdit. I successfully emailed when explicitly runnign the script in test, but the onEdit event never seems to get fired (not seeing log messages even). Script seems pretty straightforward.
function onEdit(e) {
var sheet = e.source;
var viewers = sheet.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + sheet.getUrl() + ' to view the changes ' + e.range;
Logger.log('Running onedit');
MailApp.sendEmail(recipients, subject, body);
};
the simple onEdit function has a very limited set of possible actions since it runs without the authorization of the potential user. You have to create another function and set a specific trigger on this function.(installable trigger)
See this post as an example. It shows examples of simple onEdit and installable edit (for email send)
This is explained in the documentation here.
EDIT : here is your code working :
function sendAlert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation()
var viewers = ss.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell;
MailApp.sendEmail(recipients, subject, body);
};

Script sendNotification doens't work correctly

I'm trying to make a script to send an email every day , after checking in a spreadsheet, two conditions. If the first condition for True , the data to e- mail will be " subject1 ", otherwise, will be " subject2 ".
The script works correctly when I run manually , however, through the trigger, the value I would like to send via email, simply does not appear in the body.
I can not understand what could be wrong if manually , works perfectly .
The trigger is "time-driven/hour time/every 12 hours".
Can anyone help me?
This is the code:
function sendNotificationD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
var column = ss.getRange('o13');
var cell = sheet.getRange('o13');
var value = column.getValue();
var emailSheet = ss.getSheetByName("totais");
var emails = emailSheet.getRange("q1:s1").getValues();
var range_bill_com = sheet.getRange('o13');
if (range_bill_com.getValue() !='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject1 here';
var body = 'IMPORTANT: ' + '\n\n' + 'your goal is' + ' ' + value;
MailApp.sendEmail(recipients, subject, body);
}
if (range_bill_com.getValue() =='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject2 here';
var body = 'you have reached your goal';
MailApp.sendEmail(recipients, subject, body);
}
};
At this line:
var sheet = ss.getActiveSheet();
The method getActiveSheet() gets the the active sheet that is being displayed in the spreadsheet UI when a user opens it, but when it runs from a time-driven trigger there is no active sheet, so you need to indicate the sheet, you can use getSheetByName() or getSheets() the line should look like this:
var sheet = ss.getSheetByName("Sheet1");
or
var sheet = ss.getSheets()[0]; // Index of the tab, 0 will return the first one
The function will still work if you fire it manually or from a trigger.

email notification if cell is changed

need help with google script. I have multiple row spreadsheet.
Need a script that does the following:
If any cell in column G has been changed, then send email notification
to custom address with information from this row: information from
cell D and new value of cell G.
UPD
I found useful information:
function emailNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipient = "me#gmail.com";
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on cell: «' + cell + '» New cell value: «' + cellvalue + '»';
MailApp.sendEmail(recipient, subject, body);
};
This script tracks the changes in the entire table. And I would like track changes only in column G, and get values ​​from column D.
Question:
How to get the value of the cell in column D when the value has
changed cell in column G
Finally script — answer to my question
spreadsheet
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('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);
};
Set trigger on onEdit and script will work fine
You should search this forum before posting questions; I did search on email+cell and got a few results :
For example, this post answer does almost exactly what you want to do.
EDIT following your edit :
use an IF condition.
Something like this :
var cellG = ''
if(cell.indexOf('D')!=-1){ // = if you edit data in col D
cellG = sheet.getRange('G'+ sheet.getActiveCell().getRowIndex()).getValue()
// add more condition if necessary and/or send your mail (cellG contains the value in column G of the active row
}
Logger.log(cellG)
The correct way to track a cell-change event is to use the event object "e" with onEdit trigger. The event object contains the range that was edited and hence you can always get the content of the cell that was changed.
function sendNotification(e){
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
NOTE: The function name shouldn't be onEdit which is a special name in apps script. The onEdit function won't allow to send email because of LIMITED authMode.
Here is an apps script which allows to send an email if a cell is edited in a specific range. It also allows to send the entire row, entire column or a custom range to be sent in the notification email.

Google App script onEdit?

really feel like I'm missing something with the Spreadsheet object scripts.
I'm trying to automatically email collaborators onEdit. I successfully emailed when explicitly runnign the script in test, but the onEdit event never seems to get fired (not seeing log messages even). Script seems pretty straightforward.
function onEdit(e) {
var sheet = e.source;
var viewers = sheet.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + sheet.getUrl() + ' to view the changes ' + e.range;
Logger.log('Running onedit');
MailApp.sendEmail(recipients, subject, body);
};
the simple onEdit function has a very limited set of possible actions since it runs without the authorization of the potential user. You have to create another function and set a specific trigger on this function.(installable trigger)
See this post as an example. It shows examples of simple onEdit and installable edit (for email send)
This is explained in the documentation here.
EDIT : here is your code working :
function sendAlert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation()
var viewers = ss.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell;
MailApp.sendEmail(recipients, subject, body);
};