Need a script to send an email if Cell Value Changes - google-apps-script

Need Help to create a watchdog. I'm importing a webpage with some data. and want constantly check if imported data is the same whats my reference.
So I want the script to constantly compare 2 cells and if values became different, send me (the author) the email.
Want to compare H4 and E4
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Pricechecker");
var checkprice = sheet.getRange(E2:E2);
var currentprice = sheet.getRange(H4:H4);
if (currentprice <= checkprice) {
var emailAddress = "email#example.ge";
var message = "Discount";
var subject = "There is a Discount on your item";
MailApp.sendEmail(emailAddress, subject, message);
}
}

Solution:
You would need an Installable Trigger to automatically send email upon comparison of E4 and H4.
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('compare')
.forSpreadsheet(ss)
.onEdit()
.create();
}
function compare(e) {
if (e.range == "E4" || "H4") {
var sheet = SpreadsheetApp.getActiveSheet();
var checkPrice = sheet.getRange("E4").getValue();
var currentPrice = sheet.getRange("H4").getValue();
if (currentPrice <= checkPrice) {
var emailAddress = "email#example.ge";
var message = "Discount";
var subject = "There is a Discount on your item";
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Note that you only have to run createSpreadsheetEditTrigger() in Apps Script. The created trigger will run compare() everytime you make an edit to the spreadsheet. It will automatically check E4 and H4 and send email whether the condition is satisfied.
Console Output:

Related

Google Sheets app script : How to send email on change in dropdown values?

We have two sheets :
Sheet1 - It has one column & 10,000 rows with drop-down values - "approved" and "not approved".
Sheet2 - It has email-related details i.e. email, subject, message-approved, message-not-approved.
I want to send an automatic email if the drop-down value is changed. I want to send message-approved if someone changes Sheet1 dropdown selection to "approved" and message-not-approved if it is changed to "not approved".
I have been able to successfully send the email message when i run it in script editor but it is not triggering on drop down change. I understand that i need to use onedit trigger but not sure how to do that. Any help is appreciated. Here is the code.
function sendEmail(){
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss1.getSheetByName('Sheet1');
var valueToCheck = sheet1.getRange("A2").getValue();
if(valueToCheck == "approved"){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2=ss.getSheetByName('Sheet2');
var emailAddress = sheet2.getRange(2,1).getValue();
var subject = sheet2.getRange(2,2).getValue();
var message = sheet2.getRange(2,3).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
else{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2=ss.getSheetByName('Sheet2');
var emailAddress = sheet2.getRange(2,1).getValue();
var subject = sheet2.getRange(2,2).getValue();
var message = sheet2.getRange(2,4).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
}
Explanation:
Make sure you take full advantage of the event object e.
Because your function uses MailApp which is a service that needs authorization, you can only use an installable onEdit trigger to execute as an onEdit function.
You can either create the installable trigger manually or programmatically. In the solution below, I show you the way to execute it programatically.
Solution:
Execute only and once the function create_onEdit and then upon user edits on cell A2 in Sheet1, the script will send emails.
// execute create_onEdit only and once
function create_onEdit(){
ScriptApp.newTrigger('sendEmail')
.forSpreadsheet(SpreadsheetApp.getActive())
.onEdit()
.create();
}
// this function should never be executed manually!
function sendEmail(e){
const ss = e.source;
const arng = e.range;
const ash = arng.getSheet();
const row = arng.getRow();
const col = arng.getColumn();
// trigger upon edits in Sheet1 and A2 till A10000
if(ash.getName()=="Sheet1" && row>=2 && row<=10000 && col == 1){
if(arng.getValue() == "approved"){
const sheet2=ss.getSheetByName('Sheet2');
const emailAddress = sheet2.getRange(2,1).getValue();
const subject = sheet2.getRange(2,2).getValue();
const message = sheet2.getRange(2,3).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
else{
const sheet2=ss.getSheetByName('Sheet2');
const emailAddress = sheet2.getRange(2,1).getValue();
const subject = sheet2.getRange(2,2).getValue();
const message = sheet2.getRange(2,4).getValue();
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
Be careful:
You should never execute sendEmail(e) manually as it will drop undefined errors. This function is a trigger function and it is meant to be automatically executed (triggered) upon user edits.
The function create_onEdit is responsible for creating the trigger, so you only need to execute that once and you are ready to go.

Send Daily Email Trigger

I would like to send out an email every weekday. The list of addresses is specified in one of the sheets. When an event specific trigger is used, the email is sent out as it should. However, when I set it to an time driven trigger, it is not executed properly. I receive the following error regarding the numRows variable I created: Please select an active sheet first. (line 21, file "sendEmails")
// define how HTML object is being sent:
function convSheetAndEmail(rng, email, subj)
{
var HTML = SheetConverter.convertRange2html(rng);
MailApp.sendEmail(email, subj, '', {htmlBody : HTML});
}
// function 2:
function doGet()
{
var sh1 = SpreadsheetApp.getActive().getSheetByName('Dashboard')
var dataRange = sh1.getDataRange();
var sh2 = SpreadsheetApp.getActive().getSheetByName('control_panel')
var numRows = SpreadsheetApp.getActiveSpreadsheet('Sales/Ops Daily Report').getLastRow()-1;
var data = sh2.getRange(2,1,numRows,1).getValues()
// loop through the array with the mail addresses:
for (i in data) {
var row = data[i];
var emailAddress = row[0];
var subject = 'Sales/Ops Daily Report';
convSheetAndEmail(dataRange, emailAddress, subject);
}
}
// send email
function createTriggers() {
var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY];
for (var i=0; i<days.length; i++) {
ScriptApp.newTrigger(doGet())
.timeBased().onWeekDay(days[i])
.atHour(14).create();
}
}
Method ScriptApp.newTrigger() should use a string type argument, i.e. function name. You have used doGet() call as the argument instead. As a result the trigger is not installed correctly. If you intend to trigger this function you should write: ScriptApp.newTrigger("doGet") and so on.

Google Sheets Script MailApp include cell formatting

searched this and I need this code. The problem is I dont know how to run the code and I cannot also comment due to my low reputation.
I have a button on my google sheets where I will assign the code. My data range is
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Front").getRange("C2:Q14")
function convSheetAndEmail(rng, email, subj)
{
var HTML = SheetConverter.convertRange2html(rng);
MailApp.sendEmail(emailAddress, subject, '',{htmlBody : HTML});
}
function doGet()
{
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Front-User").getRange("C2:Q14")
var emailAddress = 'test#gmail.com'
var subject = 'Test'
convSheetAndEmail(dataRange, emailAddress, subject);
}

MailApp.SendEmail script for multiple tabs

I have been looking how to write the script to check and run on multiple tabs. What I have now, I would need to copy/paste and set execution triggers one by one.
Script I have:
function SendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("1");
var value = sheet.getRange("F3").getValue();
var subject = "subject";
var message = "message";
if (value >0){
MailApp.sendEmail("email", subject, message);
}
}
I have this running every 8 hours (trigger run).
I am looking to add more tabs so that for each tab the script can check and send an email if the value of individual tabs is more than 0.
Any ideas on how to do this?
The value in every tab is under F3. Tab names, for example, 1,2,3,4...
Thank you!!!
This should do what you looking for if I understood the issue correctly.
function SendEmail() {
//Get the Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Get all Sheets in the Spreadsheet
var sheets = ss.getSheets();
//Create a variable for the email address, subject and message
var email = "";
var subject = "";
var message = "";
//Can change the starting sheet by changing the value of "i" - 0 will be the first sheet.
for (var i = 0; i < sheets.length; i++){
var sheet = sheets[i];
var value = sheet.getRange("F3").getValue();
switch(sheet.getSheetName) {
case "Sheet1":
email = "email1#domain.com";
break;
case "Sheet2":
email = "email2#domain.com";
break;
default:
value = 1
email = "yourEmailAddress#domain.com";
subject = "Message Failed to Send";
message = "There was an error sending email, please investigate";
}
if (value >0){
MailApp.sendEmail(email, subject, message);
}//END OF IF STATEMENT
}//END OF FOR LOOP
}//END OF FUNCTION

How can I send an automated email from a Google sheet updated by Squarespace?

I have a form run from SquareSpace that inputs the customer data to a Google Sheet. I'm currently running the below script, but the trigger OnFormSubmit doesn't work. I have to go in and manually run the script.
function FormConfirmationEmail() {
var sheetname = "Request Financial Assistance"
var columnnumber = 6
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetname);
if (sheet.getRange(sheet.getMaxRows(),1).getValue() != "") {
var lastrow = sheet.getMaxRows()
} else {
var count = 0
for (var i = 0; i < sheet.getMaxRows(); i++) {
if (sheet.getRange(sheet.getMaxRows()-i,1).getValue() != "") {
var lastrow = sheet.getMaxRows()-i
break;
}
}
}
var email = sheet.getRange(lastrow,columnnumber).getValue();
var emailPattern = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|aero|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|name|museum|name|net|org|pro|tel|travel)\b/;
var validEmailAddress = emailPattern.test(email);
if (validEmailAddress == true) {
var message = "<HTML><BODY>"
+ "<P>This is an automated form completion email. Thanks for submitting!"
+ "</HTML></BODY>";
MailApp.sendEmail(email, "Form Completion Email", "", {htmlBody: message});
sheet.getRange(lastrow,13,1,1).setValue("Email Sent");
} else{
sheet.getRange(lastrow,13,1,1).setValue("Email not Sent - Invalid Email Address submitted");
}
}
Can anyone see what I'm doing wrong?
You can put the trigger in the Google sheet that is collecting the responses and the Form Submit trigger should work.
ScriptApp.newTrigger("FormConfirmationEmail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
Also, instead of using getMaxRow(), you can use the e parameter that is passed to the FormSubmit function to parse and email the user submitted data.
See: Get Google Forms Data in Email