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);
}
};
Related
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.
I'd like to send a notification to someone when a particular cell (ie document) is updated. Specifically, when a cell in one row is updated, it would send a notification to the owner listed in the same row. For example B47 gets updated, it send an email to B3.
I've gotten the script to work when it's pointed to one sheet. But when I try to ask the script to run over multiple sheets/tabs, I get error 'Script function not found: sendNotification' Is it possible to have this script work for multiple sheets/tabs in the same google doc? Updates are pulling from a different source so I'd like to keep using getSheetbyName instead of getActiveSheet.
Any help would be much appreciated. Here's my code where it stands:
function shellFunction() {
var sheets = ['Arabic', 'Portuguese'];
for (var s in sheets) {
toTrigger(sheets[s]);
}
function toTrigger(sheetName) {
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheets[s]);
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
var message = '';
if (cell.indexOf('B') != -1) {
message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
MailApp.sendEmail(recipients, subject, body);
}
}
}
Should this line
var sheet = ss.getSheetByName(sheets[s]);
be changed to
var sheet = ss.getSheetByName(sheetName);
since that is the name of the passed sheet?
EDIT
There were a couple other items which may have caused issue. I made edits for this result:
function shellFunction() {
var sheets = ['Arabic', 'Portuguese'];
for (var s in sheets) {
toTrigger(sheets[s]);
}
}
function toTrigger(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
var message = '';
if (cell.indexOf('B') != -1) {
message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
MailApp.sendEmail(recipients, subject, body);
}
My first change was to not nest the functions. To do this I added a closing curly bracket before the toTrigger function and removed one of the last in your code. After that there was another nested function which actually made up all of the toTrigger function. It was called sendNotifocation which matched your error. I removed that by removing the line after the toTrigger function declaration and another curly bracket at the end of your code.
Last, I made the above edit. In my test it seemed to work fine, except that I commented out the call to actually send an email and only debugged to the line just prior to that point.
I could really use some help, and I'd really appreciate any pointers anyone can give. I know there are some other similar questions out there, but from what I can find, no one else has this specific issue/request.
I have a Google Sheet that three different people collaborate on. I have this notification script that will notify specific users via email about any changes to any of the cells in the F column. Importantly, this email notification also contains the value of the A cell in that row that has had it's F cell value changed.
However, what I'm really trying to achieve is a notification script that will email a specified person only when any cell in column F has a new value of "Yes" in Sheet2 of the master sheet.
The code I have so far is (that sends a notification when any change happens in the F column):
function sendNotification(e) {
var received = e.value; //Gets the value of the edited cell
};
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet 2");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var received = ss.getRange('F2:F').getValue();
var recipients = "example#gmail.com";
var message = '';
if(received==="Yes"){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
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)};
Logger.log('received value: ' + received);
};
If it helps, below is a code that works in my tests : (set up an installable trigger to make it work)
note : I added a couple of Logger.log here and there to check the variables and a Browser message to avoid sending mails while testing.
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet 2");
var cell = ss.getActiveCell();
var row = sheet.getActiveRange().getRow();
var col = cell.getA1Notation().replace(/[0-9]/g,'').toLowerCase();
Logger.log('col = '+col);
var cellValue = cell.getValue().toString().toLowerCase();
Logger.log(cellValue);
var recipients = "example#gmail.com";
var message = '';
Logger.log(cellValue=="yes" && col=='f');
if(cellValue=="yes" && col=='f' ){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
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)
Browser.msgBox('mail sent');
}
Logger.log('message body: ' + body);
};
To set up an event object use:
function sendNotification(e) {
var received = e.value; //Gets the value of the edited cell
};
The code that is sending the email, is outside of the if body. So the email is being sent no matter what.
Change to this:
if(received==="Yes"){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
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);
};
In this line of code:
if(received="Yes"){
You are using an assignment operator to check for equality. Equality checks are done with double or triple equal signs.
Should be:
if(received==="Yes"){
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.
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.