oldValue
These triggers are really useful. But, there's one annoying feature which that
unexpectedly not available in sheets. Let's say, Cell A1 has a numerical value
of 4. Then you edited Cell A1 to change value to 5. In the onEdit(e), if you
call the function e.oldValue, you get 4. This is totally fine just how'd you
expect it to be. What if from value of 4 in Cell A1, you deleted it. So
basically, it is now blank. If you call the range("A1").oldValue. What would
you think is the return value?...(blank) or null?...Actually it's undefined.
Does it even makes sense?
It doesn't and that's a problem. We are supposed to capture every value, if
they have that oldValue method in the first place. So, is there a solution?
Actually there is. But, to implement this, we need to add a mirror tab or
sheet of the target sheet being edited. Let's examine the implementation
flow.
It's a very simple solution, by the way. Every Edit, we need to write the same
value, to a same range in the mirror tab. But, retrieving oldValue if we are
deleting a cell value, we need to retrieve firs the mirror value before
writing the new value which is now blank.
Demo - Let's do above scenario
- On "Main" tab and Range("A1"), write the number 4. In doing so, the script write's the same number in "Main_Mirror" tab.
- Now, let's change the value in Range("A1") to 5. We have below in the Log.
- So now, let us delete 5 from Range("A1") to 5. We should expect NEWVALUE as empty or blank(""). While, OLDVALUE is now 5.
There you have it. It's a simple and quick solution. Please see the actual script below. Comment below and subscribe to my for more Google Apps Script tricks.
The script (Please click LINK if the script text doesn't show up):
No comments:
Post a Comment