2017-11-27 7 views
0

Googleドキュメントのアドオンを作成して、ユーザーがドキュメントに挿入するためにcanned textサンプルのリストから選択できるようにしています。Google Apps Script for Docsで挿入されたテキストの末尾にカーソルを移動するにはどうすればよいですか?

1st_text_insertion

2nd_text_insertion

3rd_text_insertion

しかし、私のコードの結果、次の中:

3rd_text_insertion

2nd_text_insertion

私は、テキストは次のように挿入したいです0

1st_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

1st_text_insertion

2nd_text_insertion

3rd_text_insertion

existing_text_C

私はそのようなのappendTextやGETNEXTSIBLINGを使用するなど、いくつかのアプローチを、試してみたが、彼らはそうではありません所望の出力を生成する。助けてくれてありがとう!

+0

[この回答](https://stackoverflow.com/a/28419565/7215091)を参照してください。 – Cooper

答えて

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

クーパー、ありがとう!あなたの答えはうまくいった。

私は、段落とリスト項目で特殊なケースを処理する必要があることを発見しました。私はあなたのコードを拡張し、私は他の人を助けるためにこのソリューションを共有しています。

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); 
} 
関連する問題