我正在为Google文档添加一个插件,以允许用户从固定文本示例列表中进行选择以插入到他们的文档中。我希望文本插入如下:
1st_text_insertion
2nd_text_insertion
3rd_text_insertion
但是,我的代码导致以下结果:
3rd_text_insertion
2nd_text_insertion
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,但它们不会产生所需的输出。谢谢你的帮助!
这会将光标放在插入文本的末尾。
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);
}
库珀,谢谢!你的答案奏效了。
我发现我需要处理段落和列表项的特殊情况。我扩展了你的代码,我正在分享这个解决方案以帮助其他人。
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);
}