Google App Script to trigger on cell value change with email notification - google-apps-script

looking for a help to fix my script in Google spreadsheets.
I want to build a function that sends an email every time a certain cell in the list is updated.
Here is my code, but it doesn't work.
Looking for your help to find and fix my issue
function onEdit(e) {
const specificSheet = "Inventory"
const specificCell = "B1"
let sheetCheck = (e.range.getSheet().getName() == specificSheet)
let cellCheck = (e.range.getA1Notation() == specificCell)
if (!(sheetCheck && cellCheck)) {
return
}
else {
sendEmail(){
var subject = "New update";
var emailAddress = "example#gmail.com";
var message = "new update: link";
MailApp.sendEmail(emailAddress, subject, message)
}
}
}

Send email on an Edit
function onMyEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() = "Inventory" && e.range.columnStart == 2 && e.range.rowStart == 1) {
var subject = "New update";
var emailAddress = "example#gmail.com";
var message = "new update: link";
MailApp.sendEmail(emailAddress, subject, message)
}
}
I think this needs to be an installable trigger in order to send an email.

Sometimes Sheet can not process complex operations related to onEdit(e), so maybe this could help:
function onEdit(e) {
const specificSheet = "Inventory";
const specificCell = "B1";
var range = e.range;
var ss = e.source;
var sheetName = ss.getActiveSheet().getName();
var a1Notation = range.getA1Notation();
if ((sheetName==specificSheet) && (a1Notation==specificCell)) {
var props = PropertiesService.getScriptProperties();
props.setProperty("CHECKED","TRUE");
}
}
function sendEmail(){
var subject = "New update";
var emailAddress = "example#gmail.com";
var message = "new update: link";
var props = PropertiesService.getScriptProperties();
var checked = props.getProperty("CHECKED");
if (checked == "TRUE"){
GmailApp.sendEmail(emailAddress, subject, message);
Utilities.sleep(3000);
props.setProperty("CHECKED","FALSE");
}
}
Another thing you should do is setting sendEmail() function is activated by time trigger every minute or so...

Related

Google Sheet Appscript only collects my username and no other users

I have a changelog script that only collects my username and does not collect for any other user. I'm trying to figure out the bug but unable to find a solution.
function changelog_script(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameTracker = ss.getActiveSheet().getName();
if (sheetNameTracker !== "Changelog") {
let sheet = ss.getSheetByName('Changelog');
var range = e.range && e.range.getA1Notation();
var user = e.user.getUsername();
var function_source = "changelog_script";
var time = new Date();
var changeType = e.changeType || 'EDIT';
console.log(user);
var oldValue = e.oldValue;
var newValue = e.value;
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow + 1, 1, 1, 8).setValues([
[time.toLocaleString(), function_source, changeType, sheetNameTracker, range, user, oldValue, newValue]]);
}
}
function onChange(e) {
if (e.changeType == "EDIT") return;
changelog_script(e);
}
Example Google Sheet with attached App script.
Thanks in advance. 🙏

Checkbox to approve entry on Google sheets and trigger an approval email

Currently im having a prompt window popup that collects the S. No of the entry and then approves that entry and sends out an approval email. Instead of this, i wanna have a check box that one checks to approve the entry and a function is triggered
function function1() {
var sheet= SpreadsheetApp.openById("xxxx").getSheetByName("payments");
var ui = SpreadsheetApp.getUi();
var result = ui.prompt('Please enter the S. No of the entry you want to approve', ui.ButtonSet.OK_CANCEL);
var button = result.getSelectedButton();
var sno = result.getResponseText();
for (var i=1; i<=100 ; i++)
{
Logger.log(sheet.getRange(i,1).getValue());
if(sno == sheet.getRange(i,1).getValue())
{
var index = i;
}
}
Logger.log(index);
var invitem = sheet.getRange(index,5).getValue();
var invdesc = sheet.getRange(index,6).getValue();
var reqby = sheet.getRange(index,3).getValue();
var amount = sheet.getRange(index,8).getValue();
if (button == ui.Button.OK)
{
var subject = 'Payment release request approved';
var body = '<p>Hello</p> <p>This particular payment release is approved from my end</p> <p>Invoice Item: '+ invitem +'</p> <p>Description: '+ invdesc +'</p> <p>Requested By: '+reqby+' </p> <p>Requested By: '+amount+' </p> <p> </p> <p><em>This is an automated email triggerred post approval</em></p>'
var email= 'finance#xxxx.xxx';
GmailApp.sendEmail(email, subject, "Requires HTML", {
htmlBody: body})
sheet.getRange(index,16).setValue("Approved").setBackground('#008000');
}
}
Okay here's a function and an installable trigger create function
function onMyEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == 'Your sheet name' && e.range.columnStart == '15' && e.value == "TRUE") {
let snum = sh.getRange(e.range.rowStart,1).getValue();
ApproveAndSendEmail({snum:snum});
}
}
function createTrigger() {
const ss = SpreadsheetApp.getActive();
if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "onMyEdit").length == 0) {
ScriptApp.newTrigger('onMyEdit').forSpreadsheet(ss).onEdit().create();
}
}

How do I call a mailApp function within an onEdit function?

I have an onEdit function where if executed should send an email. However, some research has turned up that the mailApp functions don't work with onEdit. I'm currently toying around with workarounds but wondering if others have solutions they've come up with.
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var sheetName = sheet.getName();
//if editing specific sheet
if (sheetName == "draft_test" ) {
//if editing 6th column
var r = e.source.getActiveRange();
if (r.getColumn() == 6 ) {
var player = sheet.getActiveCell().getValue();
// Display a dialog box with a message and "Yes" and "No" buttons.
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Do you want to draft '+player+'?', ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
//***********FOR SOME REASON EXECUTING THIS FUNCTION ISNT WORKING******************
emailLeague();
//sortAutoRoster();
} else {
}
}
}
}
function emailLeague() {
var draftSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("draft_test");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test_email");
var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Manager Info");
//actual email address omitted
var recipient = 'emailaddress#gmail.com';
//get array of all picks on "draft test" tab, and find latest one
var picks = draftSheet.getRange(3,6,146,1).getValues();
for (var i=0; i<picks.length; i++){
if (picks[i] == ""){
var latestPick = i;
break;
}
}
var subject = sheet.getRange(i+1, 2).getValue();
var body = sheet.getRange(i+1, 1).getValue();
MailApp.sendEmail(recipient, subject, body);
}

Run OnEdit only when edit is made to a specific sheet

All:
I am new... very new to coding, so I apologize that my coding probably makes people cringe. I have a script that will show and hide sheets depending on the value of cells on a sheet called "Teacher Information." The script is working great except it is running any time an edit is made to ANY of the sheets in the file. I only want it to run if a change is made to the "Teacher Information" sheet. Please advise. Thank you for sharing your expertise. Here is my current code:
function HideSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Teacher Information");
var sheet2 = ss.getSheetByName("SGO1 Plan");
var sheet3 = ss.getSheetByName("SGO1 Data");
var sheet4 = ss.getSheetByName("SGO1 Report");
var sheet5 = ss.getSheetByName("SGO2 Plan");
var sheet6 = ss.getSheetByName("SGO2 Data");
var sheet7 = ss.getSheetByName("SGO2 Report");
var sheet8 = ss.getSheetByName("SGO 1 Plan");
var sheet9 = ss.getSheetByName("SGO 1 Data");
var sheet10 = ss.getSheetByName("SGO 1 Report");
var sheet11 = ss.getSheetByName("SGO 2 Plan");
var sheet12 = ss.getSheetByName("SGO 2 Data");
var sheet13 = ss.getSheetByName("SGO 2 Report");
var cell1 = sheet1.getRange('C22');
var cell2 = sheet1.getRange('AJ22');
if (cell1.getValue() == "") {
sheet2.showSheet();
sheet3.showSheet();
sheet8.showSheet();
sheet9.showSheet();
}
if (cell1.getValue() == "0") {
sheet2.hideSheet();
sheet3.hideSheet();
sheet8.hideSheet();
sheet9.hideSheet();
}
if (cell1.getValue() == "1") {
sheet2.showSheet();
sheet3.showSheet();
sheet8.hideSheet();
sheet9.hideSheet();
}
if (cell1.getValue() == "2") {
sheet2.hideSheet();
sheet3.hideSheet();
sheet8.showSheet();
sheet9.showSheet();
}
if (cell2.getValue() == "") {
sheet5.showSheet();
sheet6.showSheet();
sheet11.showSheet();
sheet12.showSheet();
}
if (cell2.getValue() == "0") {
sheet5.hideSheet();
sheet6.hideSheet();
sheet11.hideSheet();
sheet12.hideSheet();
}
if (cell2.getValue() == "1") {
sheet5.showSheet();
sheet6.showSheet();
sheet11.hideSheet();
sheet12.hideSheet();
}
if (cell2.getValue() == "2") {
sheet5.hideSheet();
sheet6.hideSheet();
sheet11.showSheet();
sheet12.showSheet();
}
}
You can check the sheet name and proceed depending on that.
Something like this :
function HideSheets(e) {
if (e.range.getSheet().getName() != "Teacher Information") {
// If the sheet name is not equal to "Teacher Information" then Back Off..!!
return
}
// Put your other codes below
// More codes
}
This will check the sheet name before proceeding and if it's not equal to "Teacher Information" then it will return/break and not execute the code following it
Reference and further read : https://developers.google.com/apps-script/guides/triggers/events#edit (as per techhowch comment, credits to him)
You don't say if this is an On edit trigger which could be done slightly differently but you could also do this.
var ss = SpreadsheetApp.getActiveSpreadsheet();
if( ss.getActiveSheet().getName() !== "Teacher Information" ) return;

Send automated email for specific value of a drop down column

I have tried many different codes to send an email alert when column F in my Google sheet has the value "Yes". I am selecting this value from a drop down. But no emails are sent. I have tried different emails also. Nothing works. This is the code I am trying right now.
function Email(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("FY");
var row = s.getActiveRange().getRow();
var status = s.getRange(row,6).getValue();
var subject = 'subject'; var message = 'body';
var email ="";
if (e.value === "Yes" && e.range.getColumn() === 6) {
GmailApp.sendEmail(email,subject,message);
}
};
You need to use an installed trigger see here to use this on edit script.
Go to 'Edit' -> 'Current project's triggers' and set a trigger as shown below.
Code:
function onEdit(e){
var getRange = e.range;
var getValue = getRange.getValue();
var getCol = getRange.getColumn();
if(getValue == "Yes" && getCol == 6){
var subject = 'subject';
var message = 'body';
var email = "---------------";
MailApp.sendEmail(email, subject, message);
}
}