Move to last line of Google Document when it is opened
05:48 30 Apr 2019

I'm writing my first Google Apps Script and my goal is to automatically move the cursor to the last line of the document when it is opened. So far my function looks like this:

function onOpen(e) {
  var doc = DocumentApp.getActiveDocument();
  var paragraph = doc.getBody().appendParagraph('');
  var position = doc.newPosition(paragraph.getChild(0), 2);
  doc.setCursor(position);
}

and I'm basing my solution off of this documentation.

This script matches the documentation almost exactly. But when I open the document, nothing happens.

What might I be doing wrong?

This is my first experience with Javascript

Edit: This is different from this question -- my hope is to execute the script automatically when the document is opened.

At Ruben's suggestion, I checked View > Executions. Sure enough, the script is failing with the following error message:

Child index (0) must be less than the number of child elements (0). at onOpen(Code:4)
javascript google-apps-script google-docs