Sending an email whenever Google Sheets specific range is edited

1,026 views
Skip to first unread message

isanka rangana

unread,
Sep 11, 2022, 12:12:39 AM9/11/22
to Google Apps Script Community

I have a simple app script that will send an email whenever my G-sheet is edited. This works fine but it will send an email for every edit in any cell. So, I need to define a specific range as B3:E7 in my Gsheet and the email should trigger only after B3:E7 range is edited. No emails should send for other edits. Please help me on this. I took this code from this tutorial https://spreadsheet.dev/send-email-when-google-sheet-is-edited#:~:text=Step%201%3A%20Create%20your%20spreadsheet,whenever%20your%20spreadsheet%20is%20edited.

//@OnlyCurrentDoc function processEdit(e) { var sheet = SpreadsheetApp.getActive(); var rows = sheet.getRangeByName("signups").getValues(); var headerRow = rows.shift(); var editedRow = e.range.getRow(); var template = HtmlService.createTemplateFromFile("Template"); template.headerRow = headerRow; template.editedRow = editedRow; template.rows = rows; var html = template.evaluate().getContent(); MailApp.sendEmail({ to: "mye...@gmail.com ", subject: "This is test mail", htmlBody: html }); }

cbmserv...@gmail.com

unread,
Sep 11, 2022, 1:03:45 AM9/11/22
to google-apps-sc...@googlegroups.com

Hi Isanka,

 

You just need to add one if statement in your script to quit if the cell being edited is not in your chosen range.

 

B3 is row 3, column 2

E7 is row 7, column 5

 

So get column number and then add the if statement in the position below:

 

//@OnlyCurrentDoc 4

function processEdit(e)

{

var sheet = SpreadsheetApp.getActive();

var rows = sheet.getRangeByName("signups").getValues();

var headerRow = rows.shift();

var editedRow = e.range.getRow();

 

var editedCol = e.range.getColumn();

if (  (  ( row < 3 ) || (row >7) ) || ( (editedCol <2)  || (editCol > 5) ) ) {return;}  // do nothing if outside the range

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3666c088-3d08-4420-a13b-5b217c749691n%40googlegroups.com.

Isanka Rangana

unread,
Sep 11, 2022, 2:07:59 AM9/11/22
to Google Apps Script Community
Hey. Thank you very much for helping me with this. I tried this but no luck. Could you kindly have a look at my Gsheet below

Laurie J. Nason

unread,
Sep 11, 2022, 3:03:39 AM9/11/22
to google-apps-sc...@googlegroups.com
I think that the if statement needs to have && (AND) instead of || (OR) and the <> signs need to be reversed - 

if (  (  ( row < 3 ) || (row >7) ) || ( (editedCol <2)  || (editCol > 5) ) ) {return;}

Should Read

if (((row < 3) || (row >7)) && ( (editedCol <2)||(editCol > 5))) {return;}


Also - I would say (personal preference here) that it’s bad form to have a “return” in the middle of a function - a function should only have one exit - but I know that can be a divisive issue :-)

Laurie

------ Original Message ------
From "Isanka Rangana" <isa...@gssintl.biz>
To "Google Apps Script Community" <google-apps-sc...@googlegroups.com>
Date 11/09/2022 09:07:59
Subject Re: [Apps-Script] Sending an email whenever Google Sheets specific range is edited

CBMServices Web

unread,
Sep 11, 2022, 4:05:16 AM9/11/22
to google-apps-sc...@googlegroups.com
Laurie's change is correct (had a logic error).

But there is also another error with variable name.

 Try this:

//@OnlyCurrentDoc 4

function processEdit(e)

{

var sheet = SpreadsheetApp.getActive();
var rows = sheet.getRangeByName("signups").getValues();
var headerRow = rows.shift();
var editedRow = e.range.getRow();

var editedCol = e.range.getColumn();
if (  (  ( editedRow < 3 ) || (editedRow >7) ) && ( (editedCol <2)  || (editCol > 5) ) ) {return;}  // do nothing if outside the range

CBMServices Web

unread,
Sep 11, 2022, 2:46:11 PM9/11/22
to google-apps-sc...@googlegroups.com
Actually thinking about this more, I think initial logic of OR is correct. If either row or col is out of range, it should just return.

if (  (  ( editedRow < 3 ) || (editedRow >7) ) || ( (editedCol <2)  || (editCol > 5) ) ) {return;}  // do nothing if outside the range

So please change it back to the || instead of &&.

Isanka Rangana

unread,
Sep 11, 2022, 9:34:37 PM9/11/22
to Google Apps Script Community
Hi All,

First of all let me thank you all for your wonderful support. However still no luck. I keep getting e-mails for every single edit. Could you kindly have a look in the below G sheet?

CBMServices Web

unread,
Sep 11, 2022, 9:39:09 PM9/11/22
to google-apps-sc...@googlegroups.com
I will take a peak in a bit and see what is going on.

Please make sure you provide me with edit access so that I can look at the script.


isanka rangana

unread,
Sep 11, 2022, 9:45:51 PM9/11/22
to google-apps-sc...@googlegroups.com
Thank you very much. You are awesome. I have granted edit access to all.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/6LNrS4E4URg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAFX%2Bz3UTLVWFtioY6DwF5jo2hZC0YbuDmF30Gm%3DLwSUdGvk68g%40mail.gmail.com.

cbmserv...@gmail.com

unread,
Sep 11, 2022, 10:13:35 PM9/11/22
to google-apps-sc...@googlegroups.com

Hi,

 

I checked the script and it is working correctly.

 

I highlighted the area in red where any changes will cause the email to go out. Any changes outside the red area will not trigger the email. I think this is what you asked for..

 

I added an alert when the trigger occurs in the right zone. You will not see the alert if any cell is edited outside the zone.

 

Once you are happy testing, just delete the alert (which is these two commands):

 

var ui = SpreadsheetApp.getUi();

ui.alert("Triggered at cell: row: " + editedRow + " Column: " + editedCol);

isanka rangana

unread,
Sep 11, 2022, 11:53:32 PM9/11/22
to google-apps-sc...@googlegroups.com
Hey

Great, it worked. It is out of verbal definitions to say thank you. You are awesome   😍😍😍😍😍😍😍😍

CBMServices Web

unread,
Sep 11, 2022, 11:55:25 PM9/11/22
to google-apps-sc...@googlegroups.com

game over

unread,
Feb 19, 2023, 4:42:49 PM2/19/23
to Google Apps Script Community
Reply all
Reply to author
Forward
0 new messages