I need to detect if changes made to a spreadsheet are being made within a certain range of data and if so, set the current update time.
The issue is, I have a spreadsheet that I edit headers and text on and I do not want the update time in a specific cell to be updated on the spreadsheet but when I edit the data in a range of cells, I DO want the update time changed.
Here's what I have to update the time.
function onEdit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("G10").setValue(new Date());
}
I only want the date in G10 set if I edit certain cells (in a range "B4:J6")
If you want very few lines of code, you could use:
However, changing the range could take a little longer than the other methods.
There is an Event provided as a parameter to your
onEdit()
function, and it contains the necessary information about what was edited. If you were wondering what that(e)
was all about, this is it.Since an
onEdit()
function is called for every edit, you should invest as little processing as possible in determining whether you should exit. By using the event that's passed in, you will require fewer Service calls, so will be more efficient. The way that Rasmus' answer converts the A1 notation to column and row numbers is good if you need to be flexible, but if the edit range is fixed, you can simply use constant values for comparisons - again, to reduce the processing time required.Easiest Way is to NAME your Trigger Range
By naming your "trigger" range, then you don't have to mess with the script once you've set the range name inside the script. By simply editing the range of your named range inside the standard google sheets interface, the script will still work even if you expand or decrease the size of your range.
Step 1: Name Your Range
Name the range of cells you are interested in acting as the trigger for your script should you edit any of the cells in your range as per the instructions here: https://support.google.com/docs/answer/63175
I named my range "triggerRange".
Step 2: Edit your range name into the following script:
PS: Thanks to the other posters for providing the basic framework for this basic script. I've obviously commented the script pretty heavily so you can easily test it. Remove the alerts that I created inside the script for a cleaner look...or just copy and paste this instead:
Just a quick fix to a possible problem to the previous answer by Rasmus:
On the lines:
it is adding (actually it is not subtracting the initial row and column. This results in a bigger range than what is expected by 1 row and 1 whole column. Just subtract 1 in the same row:
Code by Rasmus Fuglsnag and the correction should look like this:
Thanks to Rasmus Fuglsnag for providing the simplest way to do this. I can not believe there is a better simpler way to do this by code.
You can simply check whether the edit event occurred within the range.
I acknowledge that this is very verbose, but I still haven't found a simple method on range ala range.contains(range)