I have a problem with my code. It seems fine but it doesn\'t work and I really don\'t know why. I tried everything.
I want sync my google excel with my google calen
The problem in your case is your middle condition, if(e.oldValue!=undefined)
This is going to pick up cases where e.value is also undefined, but e.oldValue was not, and create an event. So you are never getting to your final case where you expect a freshly erased cell to delete the event.
I think the logic you want here is if(e.oldValue!=undefined && e.value!=undefined)
I think part of the confusion is stemming from mixing of null and undefined.
When received from the OnEdit trigger, the value of an empty cell is undefined
, rather than null
.
Because javascript evaluates undefined as false, simply checking if(!e.value)
(if not value) should be sufficient, but if you want to be explicit you can check if(e.value === undefined)
instead.
Because javascript also evaluates null as false, null == undefined
as far as javascript is concerned. For clarity, I suggest changing all your checks to either if(!e.value) or if(!e.oldValue), or removing references to null
and using undefined
everywhere. This will make the script a lot more readable, since the current code makes it seem like null and undefined are expected to behave differently.