How do I move the cursor to the beginning of a document using Google Apps Script for Docs?

I am writing a Google Apps script using my Google Doc and am wondering how to move the cursor at the very beginning of the document. What I'm trying to do at the end is simply replacing the first line with some line.

+5
source share
1 answer

It is very simple, you can use the method setCursor() described here .

Code example:

function setCursorToStart() {
 var doc = DocumentApp.getActiveDocument();
 var paragraph = doc.getBody().getChild(0);
 var position = doc.newPosition(paragraph.getChild(0), 0);
 doc.setCursor(position);
}

, , , , , , :

function insertTextOnTop() {
  var doc = DocumentApp.getActiveDocument();
  var top = doc.getBody().getChild(0);
  top.asParagraph().insertText(0,'text to insert');
  doc.saveAndClose();
}
+8

All Articles