如何在文档中的特定位置插入? (在文本项内?) [英] How do I insert at a specific position in a Doc? (Inside a text item?)
问题描述
我的目标是找到某个文本在文档中的位置,删除该文本,然后再放置其他内容.我以为我会先插入新文本开始,但是它并没有出现在我认为会出现的地方.
My goal is to find the position of some text in a document, delete that text, and put something else its place. I thought I would start by just inserting the new text first, but it doesn't go anywhere near where I thought it would.
相关代码行在这里:
matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().insertParagraph(matchPosition, "The new stuff");
在这里放东西"位于文档中间,其中包含各种其他文本和格式.
"put stuff here" is in the middle of a document with all sort of other text and formatting.
我猜想这不可能以我认为的方式完成.谁能指出我正确的方向?预先感谢您的帮助.
I'm guessing this can't be done the way I think it can. Can anyone point me in the right direction? Thanks in advance for your help.
推荐答案
您尝试使用的函数insertParagraph
将索引作为参数,这是 paragraph 的索引,而不是位置文本何时显示.
The function you tried using, insertParagraph
takes an index as parameter, which is the index of the paragraph, not the place when the text will go.
您可以简单地替换文本,而不是附加文本然后删除其中的内容
Instead of appending the text then removing what was there, you could simply replace it
theDoc.getBody().replaceText("put stuff here", "The new stuff");
或者,如果要将文本放置在精确的位置,可以使用正文的功能editAsText
,然后使用insertText
Or, if you want to place text at a precise position, you could use the function editAsText
of the body, and then insertText
matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().editAsText().insertText(matchPosition, "The new stuff");
If you want more info, you should check out the complete list of function of the class body here
这篇关于如何在文档中的特定位置插入? (在文本项内?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!