Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 893
Kalle Kanon Avatar asked Dec 08 '25 11:12

Kalle Kanon


2 Answers

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

like image 80
Riyafa Abdul Hameed Avatar answered Dec 12 '25 00:12

Riyafa Abdul Hameed


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);
}
like image 22
Bill Pier Avatar answered Dec 12 '25 01:12

Bill Pier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!