【问题标题】:How do I move the cursor to the end of the document?如何将光标移动到文档末尾?
【发布时间】:2017-07-20 17:08:00
【问题描述】:

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

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

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    你可以试试这样的:

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

    这可能不是最有效的方法

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 你的方法很好,谢谢。它不编辑文档(不创建新段落)。但是如何不站在最后一段的开头,而是站在结尾呢?
      猜你喜欢
      • 1970-01-01
      • 2011-12-17
      • 2019-09-28
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2020-01-27
      • 2011-11-08
      • 1970-01-01
      相关资源
      最近更新 更多