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

岁酱吖の 提交于 2019-12-11 01:16:29

问题


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.


回答1:


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




回答2:


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);
}


来源:https://stackoverflow.com/questions/45221103/how-do-i-move-the-cursor-to-the-end-of-the-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!