3

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166

2 Answers2

2

You can try something like this:

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

It might not be the most efficient way though

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
1

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);
}
Bill Pier
  • 11
  • 1
  • Your way is good, thanks. It does not edit the document (does not create a new paragraph). But how to stand not at the beginning of the last paragraph, but at its end? – Boris Baublys Aug 06 '20 at 03:01