Need to sendNotification for multiple columns and sheets - google-apps-script

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!

Related

Auto Email google sheet response

Can anyone help with a google app script to trigger an auto email to applicants that failed the quiz - scoring lower than 130 on column A on google sheets?
Thanks in advance for the help!
Quiz Results Response
Do you want the mail to go automatically as soon as the form is filled? In that case, you can use the onFormSubmit trigger to send the mail to him automatically.
If you are processing the answers and then creating the result sheet, you can send the mail from the result sheet by processing each row in the sheet. You can easily find many examples how to send a mail from a sheet based on value in a column.
I am enclosing a similar code which sends the response to a user automatically as soon as he submits a form. I am storing his email ID (based on employee number) and sending him some data (based on his employee number) as soon as requests it. It works as a "employee self help portal".
function onFormSumbit(e) {
var emp = e.source.getActiveSheet().getRange(e.range.rowStart,2).getValue();
var lvss = SpreadsheetApp.openById("xxxx"); // blanks https://docs.google.com/spreadsheets/d/1kjOjhH0dvz4j3FsB_1UR5astpy8-ZxszNI4BZo4XJP0/edit#gid=0
SpreadsheetApp.setActiveSpreadsheet(lvss);
var lvsht = lvss.getSheetByName("Blanks");
var lvlr = lvsht.getLastRow();
var vA =lvsht.getRange("A:L").getValues();
var html="<style>th,td{border:1px solid black;}</style><table>";
html+='<tr>';
for(var i=0;i< 4 ;i++) {//make title
html+=Utilities.formatString('<th>%s</th>',vA[0][i]);
}
html+='</tr>';
for (i=1;i<lvlr;i++){
if (vA[i][0]==emp){
html+='<tr>';
for (j=0;j<4;j++) {
if ( j==2 ){
vA[i][j]=Utilities.formatDate(vA[i][j],"GMT+05:30", "dd-MM-yy");
}//if date
html+=Utilities.formatString('<td>%s</td>',vA[i][j]);
}//for emp
html+='</tr>';
}//if
}//for
html+='</table><br>';
var empss = SpreadsheetApp.openById("xxxx"); // emp master
SpreadsheetApp.setActiveSpreadsheet(empss);
var empsht = empss.getSheetByName("Emp");
var emplr = empsht.getLastRow();
var empvals =empsht.getRange("A:H").getValues();
for (i=1;i<emplr;i++){
if (empvals[i][0]==emp){
var em1=empvals[i][7];
var name=empvals[i][1]
break;
}//if
}//for
var subject="Your punch blanks "+name;
if (em1=="") {} else {
//MailApp.sendEmail(em1, subject, html);}
MailApp.sendEmail({
to: em1,
subject: subject,
htmlBody: html
});
}
}
//
//
Hope it helps !!
This function processes the full spreadsheet data from first row to last row and sends failure notification emails where the score is less than 130 ...
function notifyFailedCandidates ()
{
//Get the active sheet from the current spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get all the rows from the spreadsheet
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2,1, lastRow-1, lastColumn);
var rows = searchRange.getValues();
//loop through each of the rows
for (var rowNumber in rows)
{
//Get each of the values from the row
var quizScore = rows[rowNumber][0];
var firstName = rows[rowNumber][1];
var lastName = rows[rowNumber][2];
var email = rows[rowNumber][3];
//Send a failure notification if the score is less than 130
if (parseInt (quizScore) < 130)
{
MailApp.sendEmail
(
email, //candidate email address
'Notification of test failure', //Subject of failure notification email
//Contents of failure notification email
'Hi ' + firstName + "\n\n" +
'This email is to let you know that unfortunately you failed the test, with a score of ' + quizScore + ' / 170' + "\n\n" +
'Better luck next time'
);
}
}
}

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
}

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);
};

How to send email from Google Spreadsheet based on change within a specific sheet and column

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){
...

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.