【问题标题】:How can I move the cursor to the end of the inserted text with Google Apps Script for Docs?如何使用 Google Apps Script for Docs 将光标移动到插入文本的末尾?
【发布时间】:2017-11-27 00:57:16
【问题描述】:

我正在为 Google Docs 编写一个插件,以允许用户从预设文本示例列表中进行选择以插入到他们的文档中。我想要这样插入的文本:

第一个文本插入

第二个文本插入

3rd_text_insertion

但是,我的代码结果如下:

3rd_text_insertion

第二个文本插入

第一个文本插入

发生反向排序是因为光标位置保持在同一位置,而不是更新到最后一个文本插入的末尾。

这是我正在使用的代码:

function insertText(text) {
  var doc = DocumentApp.getActiveDocument();
  var cursor = doc.getCursor();
  newPosition = cursor.insertText(text + '\r');
  doc.setCursor(newPosition);
}

代码需要足够灵活,以便在光标所在位置插入文本,然后在返回字符后添加新条目。例如,如果用户将光标放在现有文本项 B 和 C 之间的空白行上,则插入的文本应该出现在项 B 和 C 之间的新行上。

文本插入前的示例:

existing_text_A

existing_text_B

existing_text_C

文本插入后的期望输出:

existing_text_A

existing_text_B

第一个文本插入

第二个文本插入

3rd_text_insertion

existing_text_C

我尝试了几种方法,例如使用 appendText 或 getNextSibling,但它们不会产生所需的输出。感谢您的帮助!

【问题讨论】:

标签: google-apps-script google-docs


【解决方案1】:

这会将光标放在插入文本的末尾。

function insertTextAtCursor(txt){
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  var t=doc.getCursor().insertText('\n' + txt);
  var txtEl=doc.getCursor().getElement();
  var txtOff=doc.getCursor().getOffset();
  var pos=doc.newPosition(txtEl, txtOff + 1);//I can't actually explain this.  I figured out based upon the error I was getting in the console log
  doc.setCursor(pos);
}

【讨论】:

    【解决方案2】:

    库珀,谢谢!你的回答有效。

    我发现我需要处理带有段落和列表项的特殊情况。我扩展了你的代码,我正在分享这个解决方案来帮助其他人。

    function insertText(txt){
    // Summary: Locates the user's cursor, then inserts new text as either a 
      // new paragraph or a new list item, and then updates the cursor position 
      // after the inserted text.
      var doc=DocumentApp.getActiveDocument();
      var txtPos=doc.getCursor();
      var txtElement=txtPos.getElement();
      var txtElementType=txtElement.getType();
      parentElement = txtElement.getParent();
      childIndex = parentElement.getChildIndex(txtElement);
      // Determines if text is within a paragraph and adds a new paragraph
      if (txtElementType == DocumentApp.ElementType.PARAGRAPH) {
        var t = parentElement.insertParagraph(childIndex + 0,txt);
    
      // Determines if text is within a list item and adds a new list item
      } else if (txtElementType == DocumentApp.ElementType.LIST_ITEM) {
        txtGlyphType = txtElement.getGlyphType();
        var t = parentElement.insertListItem(childIndex + 0, txt).setGlyphType(txtGlyphType);
    
      // Determines if text is within a text element (e.g., within a multi-word block of text)
      // and then determines whether the text element is within a paragraph or a list item.
      } else if (txtElementType == DocumentApp.ElementType.TEXT) {
        parentElementType = parentElement.getType();
        parentparentElement = parentElement.getParent();
        parentparentIndex = parentparentElement.getChildIndex(parentElement);
    
        // Adds a new paragraph if text element is within a paragraph 
        if (parentElementType == DocumentApp.ElementType.PARAGRAPH) {
        var t = parentparentElement.insertParagraph(parentparentIndex + 0,txt); 
    
          // Adds a new list item if text element is within a list item
        } else if (parentElementType == DocumentApp.ElementType.LIST_ITEM) {
          parentGlyphType = parentElement.getGlyphType();
          var t = parentparentElement.insertListItem(parentparentIndex + 0, txt).setGlyphType(parentGlyphType);
        }
      }
      // Updates the position of the cursor to follow the inserted text
      newPosition = parentElement.getChild(childIndex + 1);
      var pos = doc.newPosition(newPosition, 0);
      doc.setCursor(pos);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 2011-11-08
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      相关资源
      最近更新 更多