I currently have a working Google script that reads in the values on a dashboard in Google Sheets for swimming pool chemical monitoring. People will submit a form and if those numbers are greater than or less than a set number, it will send an email with a specific message.
This works fine but if there are multiple numbers that are off, I will receive multiple emails which gets annoying.
I am looking for one simple, clean output instead of multiple emails.
Here is part of my code (the rest is the same just different pools).
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
//----vvvvvPool
var IDPpHReading = sheet.getRange('A6:A7').getValue();
var IDPpHTest = sheet.getRange('B6:B7').getValue();
var IDPORP = sheet.getRange('A9:A10').getValue();
var IDPSetPoint = sheet.getRange('B9:B10').getValue();
var IDPCL = sheet.getRange('A12:A13').getValue();
var IDP_Filter1_INF = sheet.getRange('A16:A17').getValue();
var IDP_Filter1_EFF = sheet.getRange('B16:B17').getValue();
var IDP_Filter2_INF = sheet.getRange('A20:A21').getValue();
var IDP_Filter2_EFF = sheet.getRange('B20:B21').getValue();
// insert email here
var emailAddress = "pseudoMcFakeEmail#Battlecruiser.com";
//-------------------- IDP pH
if (IDPpHReading > 7.8) {
var subject = "High pH Reading IDP";
var message = " An Aquatic Specialist recently logged a chemical reading that was too high for the Indoor Pool. Please check the situation asap.";
MailApp.sendEmail(emailAddress, subject, message);
}
else if (IDPpHReading < 7.2) {
var subject = "Low pH Reading IDP";
var message = " An Aquatics Specialist recently logged a chemical reading that was too low for the Indoor Pool. Please check the situation asap.";
MailApp.sendEmail(emailAddress, subject, message);
}
if (IDPpHTest > 7.8) {
var subject = "High pH Test IDP";
var message = " An Aquatics Specialist recently logged a chemical reading that was too high for the Indoor Pool. Please check the situation asap.";
MailApp.sendEmail(emailAddress, subject, message);
}
else if (IDPpHTest < 7.2) {
var subject = "Low pH Test IDP";
var message = " An Aquatics Specialist recently logged a chemical reading that was too low for the Indoor Pool. Please check the situation asap.";
MailApp.sendEmail(emailAddress, subject, message);
}
};
My go for this would be to make each message unique :
var message 1;
var message 2;
etc
and then have the email - MailApp.SendEmail(emailAddress,subject, message1+message2....);
But I feel like that is a sloppy way to do it.
I have had to do this a few times and what I usually do is use string concatenation to build the message and then send it:
var subject = "Abnormal pH Reading IDP";
var message = 'An Aquatics Specialist recently logged a chemical reading at the indoor pool and found:\n';
var needsCheck = false;
if (IDPpHReading > 7.8) {
var message = message + "\nA reading that was too high.";
var needsCheck = true;
}
else if (IDPpHReading < 7.2) {
var message = message + "\nA reading that was too low.";
var needsCheck = true;
}
if (IDPpHTest > 7.8) {
var message = message + "\nA test that was too high.";
var needsCheck = true;
}
else if (IDPpHTest < 7.2) {
var message = message + "\nA test that was too low.";
var needsCheck = true;
}
if (needsCheck){
var message = message + "\n\nPlease check the situation asap.";
MailApp.sendEmail(emailAddress, subject, message);
}
Related
I have been trying to send auto Email through Google App-script. There are two similar scripts but have two different
Email Text
Email Body
Subject
I have also set a trigger to send Auto Email on edit and the script is send the Email when there is "Different" value in Col"E". (Script name is 2nd_Email)
I just want that if "New Request" value is come in Col"E" then 1st_Email script should follow.
I have tried at my end but sometimes 1st_Email scripts works and sometimes 2nd_Email works.
I want both of them to work according to Col"E" values.
Please visit the sheet.
https://docs.google.com/spreadsheets/d/1Eu-c5CPj6XKQSAkSumuprA41-Cx_jvOuUPHr9Zg8KyQ/edit#gid=797418690
Keep the trigger only for the First_email function and add the second code to your first one as an alternative to your if condition.
function First_email() {
var INITIALline = 2;
var columnSEND = 5;
var STATUScolumn = 16;
var textCONDITION = "New Request";
var textCONDITION2 = "Different";
var textSENT = "Mail_Sent"
var tab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var interval = tab.getRange(INITIALline,1,tab.getLastRow()-INITIALline+1,STATUScolumn);
var dice = interval.getValues();
var yousent = false;
var email,subject,message;
for (var i=0; i<dice.length; ++i) {
if((dice[i][columnSEND-1]==textCONDITION) && (dice[i][STATUScolumn-1]!=textSENT)){
var email = dice[i][9]
subject = dice[i][6]+" | New Request | "+dice[i][0];
var message = "<font size='3' face='Comfortaa'>Dear Different "+dice[i][6]+",<br/><br/>"+
"Thanks for New Request with us."+dice[i][0]+".<br/><br/>"+
"<i>Thanks & Regards</i><br/>"+
"<b> New Request </b>";
MailApp.sendEmail(email, subject, message,{ htmlBody: message});
tab.getRange(INITIALline+i,STATUScolumn).setValue(textSENT);
yousent = true;
SpreadsheetApp.flush();
}
else if((dice[i][columnSEND-1]==textCONDITION2) && (dice[i][STATUScolumn-1]!=textSENT)){
var email = dice[i][9]
subject = dice[i][6]+" | Different | "+dice[i][0];
var message = "<font size='3' face='Comfortaa'>Dear "+dice[i][6]+",<br/><br/>"+
"Thanks for Different with us."+dice[i][0]+".<br/><br/>"+
"<i>Thanks & Regards</i><br/>"+
"<b> Different </b>";
MailApp.sendEmail(email, subject, message,{ htmlBody: message});
tab.getRange(INITIALline+i,STATUScolumn).setValue(textSENT);
yousent = true;
SpreadsheetApp.flush();
}
}
}
I've built a script that will get the issue with a form submission (D2) then output the text with the information in an email. Right now the Email address location (B2) and issue (D2) are hard coded. How can I work the code to get the email and issue from only the last row submitted?
function SendNot() {
// Fetch the Issue
var reasonRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TriggerSettings").getRange("D2");
var reason = reasonRange.getValue();
// Check for Issue
if (reason==="nogps"){
// Fetch the email address
var emailRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PlayerInfo").getRange("B2");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'No GPS...';
var subject = 'Latest VDGL Entry...';
GmailApp.sendEmail(emailAddress, subject, message);
}
else if (reason==="nobarcode"){
// Fetch the email address
var emailRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PlayerInfo").getRange("B2");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'No Barcode...';
var subject = 'Latest VDGL Entry...';
GmailApp.sendEmail(emailAddress, subject, message);
}
}
You can calculate the last row with content using the following method:
sheet.getLastRow()
In your example you can calculate this as follows:
var d_size = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TriggerSettings").getLastRow();
var b_size = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PlayerInfo").getLastRow();
and then you can use d_size to grab the last row reason elements:
var reasonRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TriggerSettings").getRange("D"+d_size);
and b_size to grab the last row email elements:
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PlayerInfo").getRange("B"+b_size);
You can also use a different syntax to get the desired range:
Instead of getRange("D"+d_size) -> getRange(d_size,4)
Instead of getRange("B"+b_size) -> getRange(b_size,2)
References:
Sheet.getLastRow()
This is my first post so apologies in advance if I am posting to the wrong place or asking a question that has been answered elsewhere - go easy on me!
In a nutshell, I have a Google Form and a connected Google Sheet. I need to automate it so that, when a new form is submitted, an email is sent to a specific colleague (the student's supervisor). I have had a good go myself but am now totally stuck!
I have created the form, linked it to a sheet, written the code, added the trigger and tested by submitting a form. Nothing happened! No error messages, just... nothing!
Any advice hugely appreciated. I am very new to this and still taking baby steps.
The code I have cobbled together (which is probably full of errors!) is as follows:
function wa132657(e) {
//setup the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get the range from OnFormSubmit
var range = e.range;
Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG
// get the data for the range
var response = row.getValues();
// get the supervisor name from the form submission
var supervisor = response[0][1];
Logger.log("DEBUG: Supervisor = "+supervisor);// DEBUG
// get the emails list
var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SupEmails");
// get ALL the data from this sheet
var emaildata = emailSheet.getDataRange().getValues();
// check how many rows of data
var emailLastRow = emailSheet.getLastRow();
// start the loop through the emails data
for (var i=1; i<emailLastRow; i++){
// if the supervisor is equal to SupervisorEmail
if (supervisor == emaildata[i][0]){
// there is a match
//Next, get the email address
var emailSupervisor = emaildata[i][1];
Logger.log("DEBUG: supervisor = "+emaildata[i][0]+", email address: "+emailSupervisor);// DEBUG
// Finally, send the Email.
var theirName = e.values[2];
var theirProgramme = e.values[3];
var theAbsenceReason = e.values[8];
var theAbsenceStart = e.values[5];
var theAbsenceEnd = e.values[6];
var subject = "Student Absence Report";
var message = "New Absence Report: " + theirName + " \n Programme: " + theirProgramme; + " \n\n
Reason for Absence: \n" + theAbsenceReason + " \n Start of Absence" + theAbsenceStart + "\n End of Absence:" + theAbsenceEnd;
MailApp.sendEmail(emailSupervisor, subject, message);
}
}
}
I am using this code to send emails (composing email content getting text from the sheet named ranges:
//compose issue emails to student and admin
function composeIssueEmail() {
//student's name, last name and email
var email = ss.getRangeByName("CourseProgressEmail").getValue()
var name = ss.getRangeByName("CourseProgressName").getValue()
var lastName = ss.getRangeByName("CourseProgressStudentLastName").getValue()
var subj = ss.getRangeByName("SetUpIssueTitle").getValue()
var subject = subj.replace("*imya*", name)
var bodyText = ss.getRangeByName("SetUpIssueBody").getValue()
var body = bodyText.replace("*imya*", name)
var link = getChecksheetURL()
var text = body.replace("*link*", link)
//send email to student
var studentEmail = sendEmail(email, subject, text)
var adminEmail = "AGcourseSup#gmail.com"
var adminSubj = ss.getRangeByName("SetUpAdminIssueTitle").getValue()
var adminSubject = adminSubj.replace("*imya*", name)
var adminSubjectFinal = adminSubject.replace("*familia*", lastName)
var adminText = ss.getRangeByName("SetUpAdminIssueBody").getValue()
var adminTextReplace = adminText.replace("*imia*", name)
var adminBody = adminTextReplace.replace("*familia*", lastName)
var adminText = adminBody.replace("*link*", link)
//send email to admin
sendEmail(adminEmail, adminSubjectFinal, adminText)
}
//gets current checksheet URL
function getChecksheetURL() {
var Url = ss.getUrl()
var linkMiddle = "#gid="
var sheetID = sheet.getSheetId()
var shecksheetURL = Url + linkMiddle + sheetID
return shecksheetURL
}
//sends emails
function sendEmail(email, subject, body) {
GmailApp.sendEmail(email, subject, body)
}
Execution transcript:
[19-06-12 16:39:43:396 EEST] Execution succeeded [2.399 seconds total runtime]
It sends stably to the gmail account that is the same as spreadsheet's one.
But to another gmail account it sends about every other time.
Details:
This code is executed (I log the line after this code)
The emails are visible in my outbound box but not arriving to any of the boxes of the recepient gmail.
Not in spam etc.
I don't get any messages, error or bounce notifications.
I tried MailApp instead - it's even worse and sometimes doesn't send even to my own email.
I tried to change things in settings config, but didn't find anything to work.
I set up a filter "never send to spam" and "always star it" - didn't work.
I deleted a link from it so it has no link - didn't work.
What can be a solution?
I handled this issue. The issue is about anti-spam filters not about the code.
I gained more trust to the email account by adding "Reply To" option within GmailApp.sendEmail method. It magically solved the problem so each email reaches target now.
I appreciate if you could help.
I need to find all unread emails in my gmail 1#example.com and send them all to 2#example.com by using sendEmail(to,replyTo, subject, body) https://developers.google.com/apps-script/reference/mail/mail-app
I tried to write a script but unfortinately it does not want work.
I hope you could help
function RespondEmail(e) {
//send response email
var threads = GmailApp.search("to:(1#example.com) label:unread");
for (var i = 0; i < threads.length; i++) {
threads[i].sendEmail("1#example.com",
"2#example.com",
"TPS report status",
"What is the status of those TPS reports?")}
// mark all as read
var threads = GmailApp.search("to:(1#example.com) label:unread");
GmailApp.markThreadsRead(threads);
}
I also would be happy if you could advise me how I can change the subject of the ReplyTo email according to original email which I receive on 1#example.com
The problem in your script lies in the fact that sendEmail() belongs to the GmailApp Service, so it always needs to be called the following way:
GmailApp.sendEmail()
For your needs, it may be more appropriate to use the forward() method.
In the following example, I added a custom subject which you can edit and adapt to your needs.
function RespondEmail() {
//send response email
var threads = GmailApp.search("to:origin#gmail.com is:unread");
var subject = "";
var msg = "";
var c = 0; // will be used to count the messages in each thread
var t = "";
var attachment = "";
var forwarded = "";
for (var i = 0; i < 3 /*threads.length*/ ; i++) {
// I set 'i' to 3 so that you can test the function on your 3 most recent unread emails.
// to use it on all your unread email, remove the 3 and remove the /* and */ signs.
t = threads[i]; // I wanted to avoid repetition of "threads[i]" for the next 2 lines haha
c = t.getMessageCount() - 1;
msg = t.getMessages()[c];
forwarded = msg.getBody(); // that is the body of the message we are forwarding.
subject = msg.getSubject();
attachment = msg.getAttachments();
msg.forward("destination#gmail.com", {
replyTo: "origin#gmail.com",
subject: "TPS report status of [" + subject + "]", // customizes the subject
htmlBody: "What is the status of those TPS reports below?<br><br>" //adds your message to the body
+
"<div style='text-align: center;'>---------- Forwarded message ----------</div><br>" + forwarded, //centers
attachments: attachment
});
t.markRead(); // mark each forwarded thread as read, one by one
}
}