How do I move the cursor to the end of the document?

Viewed 1329

I want to move the cursor to the end of the document in the beginning of my script. How do I do that?

I already know how to move the cursor to the beginning of the document as described here.

2 Answers

For a document that is used somewhat as a log, where content is always added to the bottom, I use an onOpen function and position the cursor at the last paragraph:

function onOpen() {
  var doc = DocumentApp.getActiveDocument();
  var paragraphs = doc.getBody().getParagraphs();
  var position = doc.newPosition(paragraphs[paragraphs.length-1], 0);
  doc.setCursor(position);
}
Related