如何将光标移动到文档末尾?

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

我想在我的脚本开头将光标移动到文档的末尾。我该怎么做?

我已经知道如何按照here所述将光标移动到文档的开头。

您可以尝试这样的操作:

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

虽然这可能不是最有效的方法

对于一个有点像日志的文档,内容总是添加到底部,我使用了一个onOpen函数并将光标定位在最后一段:

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