Hey there,
The first code snippet you input into the HTML element works because the script has a reference to the element ID demo in the html where the function is executing and being displayed on.
The non-working code is trying to set the event.data on the GoogleSheetDocs element ID but this element hasn’t been created so it has no reference. An element with that id needs to be created in your html.
Hope this helps you out
Best regards,