如何在文档中的特定位置插入? (在文本项内?)

时间:2018-07-06 19:59:41

标签: google-apps-script

我的目标是找到文档中某些文本的位置,删除该文本,然后再放置其他内容。我以为我会先插入新文本开始,但是它并没有出现在我想像的地方。

有关代码行在这里:

matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();

theDoc.getBody().insertParagraph(matchPosition, "The new stuff");

“在这里放东西”位于文档中间,其中包含各种其他文本和格式。

我猜想这不可能达到我认为的方式。谁能指出我正确的方向?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您尝试使用的函数insertParagraph将索引作为参数,即 paragraph 的索引,而不是文本出现的位置。

您可以简单地替换它,而不是附加文本然后删除其中的内容

theDoc.getBody().replaceText("put stuff here", "The new stuff");

或者,如果要将文本放置在精确的位置,可以使用正文的功能editAsText,然后使用insertText

matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().editAsText().insertText(matchPosition, "The new stuff");

如果您需要更多信息,则应查看类主体here的完整功能列表

相关问题