In my script, I want to compare the value that has just been entered into a cell with whatever value was previously in that cell, if any.
Per https://developers.google.com/apps-script/guides/triggers/events, oldValue should be defined if (a) the edited range is a single cell and (b) the cell did actually previously contain a value. But this assertion does not bear out in practice, at least not per my attempts: instead, the value of oldValue is ALWAYS undefined.
I selected a cell that contained the word "Rome", I then typed the word "Paris" and hit the Return key.
My onEdit function contains, for debugging purposes, the following lines of script: .
function onEdit(event) {
...
const valueBeforeEdit = event.oldvalue;
const valueAfterEdit = event.value;
...
outputDebuggingMsg("valueAfterEdit = " + valueAfterEdit +
"\n valueBeforeEdit = " + valueBeforeEdit);
...
*outputDebuggingMsg is a function I wrote to write useful data to successive rows of my "Debugging Messages" sheet, so I can later view a long series of such data.
The trouble is, the debugging output message is always as follows:
valueAfterEdit = Paris
valueBeforeEdit = undefined (I expected it to say "Rome" not "undefined")
Ths same thing happens whether I try this in Chrome [version 131.0.6778.108 (Official Build) (64-bit)] OR Edge [version 131.0.2903.86 (Official build) (64-bit)].
You have an error typo that's why the output is always undefined
, change your function valueBeforeEdit = event.oldvalue to oldValue
.
From this:
const valueBeforeEdit = event.oldvalue;
To this:
const valueBeforeEdit = event.oldValue;
You can read more about simple triggers and installable triggers on this link - Event Objects.