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.
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.
In an attempt to automate some of my work, I am beginning to learn basics of google scripts.
I have a spreadsheet in which I want to send an email notification when data is input into one column or another. There are also two tabs within this spreadsheet in which I would like this to occur.
The current result from the script is an email on the second 'sendnotification' function.
Question: How do I get the script to consider both functions?
I know this code can be condensed by likely using an IF fucntion in a better way but I am at a loss.
For some context: This is used in a manufacturing operation. The production is done by an offsite company and when they enter quantity into column 10 on either sheet, I want it to send a group of people an email that I work with. Similarly, when the product quality testing is done I want to be able to input into Column 12 and it send the offsite company an email.
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 = "ryan.helms#company.com";
var subject = "Disc production was entered on the "+ss.getName();
var body = ss.getName() + " has been updated with an amount produced. Visit " + ss.getUrl() + " to view the quantities entered.";
//Check to see if column is A or B to trigger
if (cellcol == 10)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
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 = "ryan.helms#company.com";
var subject = "A lot of disc has been RELEASED by XYZ Company";
var body = ss.getName() + " has been updated with a lot of disc that were released by XYZ Company. Visit " + ss.getUrl() + " to view this updated information.";
//Check to see if column is A or B to trigger
if (cellcol == 12)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
You'll need to set up an onEdit installable trigger (if you haven't already) and assign it to the following function:
function onEditEmailSender(e) {
var sheetName = e.range.getSheet().getName();
if (sheetName === "tab1" || sheetName === "tab2") {
var col = e.range.getColumn();
if (col === 10)
//send col 10 email
else if (col === 12)
//send col 12 email
}
}
The onEdit trigger passes a parameter with all sorts of information, the most useful in your case being e.range. This Range object corresponds to the cell that was edited to trigger the onEdit event. You can use it to get the name of the sheet (what you call tab) and the column that was edited.
Using that information you can send the appropriate email. Good luck!
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);
}
};
I am trying to automate sending an email from a spreadsheet when a user form is submitted. The form covers a number of processes (ordering fuel, receiving fuel etc) and I only want the email sent out on fuel orders - so need to restrict the alert to a specific column.
I have the pulled the below together from other examples, pretty sure I'm close but am probably missing something obvious out...
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if(sheet.getName()=='Form Responses 1'){
var cell = ss.getActiveCell().getA1Notation();
var cellcol = cell.getColumn();
if(cellcol == 4){
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "email#email.com";
var subject = 'New fuel order for '+cellvalue;
var body = 'A new fuel order has been placed for ' + cellvalue + '. Please action asap: «' + ss.getUrl() + '»';
MailApp.sendEmail(recipients, subject, body);}
}
}
I had it working until I added the lines to find the cell column and the if function to continue only if the edit was in column D - previously any edit to the form responses sheet triggered a notification.
Many thanks
This line var cell = ss.getActiveCell().getA1Notation(); makes cell a String from google apps script perspective, not a cell, so .getColumn() does nothing.
Instead, I suggest having simply
var cell = ss.getActiveCell();
var cellcol = cell.getColumn();
if(cellcol == 4){
...
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.