- 您想删除 Google 文档中
#PLACEHOLDER# 段落之后正文中的所有子项。
- 您希望使用 Google Apps 脚本实现此目的。
如果我的理解是正确的,那么这个修改呢?请认为这只是几个答案之一。
模式一:
在这个模式中,使用了文档服务。
修改脚本:
请进行如下修改。本次修改,修改了if语句中的脚本。
从:
var offset = body.getChildIndex(ele.getParent());
到:
var offset = body.getChildIndex(ele.getParent());
// Retrieve total number of children in the body.
var numChildren = body.getNumChildren();
// If the text of the last paragraph is cleared.
body.getChild(numChildren - 1).asParagraph().editAsText().setText("");
// The children from offset to the child before the last paragraph are removed.
for (var i = numChildren - 2; i > offset; i--) {
body.removeChild(body.getChild(i));
}
- 在这种情况下,
#PLACEHOLDER# 的段落不会被删除。如果还想去掉#PLACEHOLDER#这一段,请修改如下。
- 发件人:
for (var i = numChildren - 2; i > offset; i--) {
- 收件人:
for (var i = numChildren - 2; i >= offset; i--) {
模式 2:
在此模式中,使用了 Google Docs API。当您使用此脚本时,please enable Docs API at Advanced Google services。在这种情况下,使用来自body.findText("#PLACEHOLDER#") 的结果,不使用用于删除子项的 for 循环。
修改脚本:
请进行如下修改。本次修改,修改了if语句中的脚本。
从:
var offset = body.getChildIndex(ele.getParent());
到:
var offset = body.getChildIndex(ele.getParent());
// Retrieve document.
var content = Docs.Documents.get(somedoc.getId()).body.content;
// Create request body.
var startIndex = content[offset + 1].paragraph.elements[0].endIndex;
var endIndex = content[content.length - 1].paragraph.elements[0].endIndex - 1;
var resource = {requests: [{deleteContentRange: {range: {startIndex: startIndex, endIndex: endIndex}}}]};
// Update document.
Docs.Documents.batchUpdate(resource, somedoc.getId());
- 在这种情况下,
#PLACEHOLDER# 的段落不会被删除。如果还想去掉#PLACEHOLDER#这一段,请修改如下。
- 发件人:
content[offset + 1].paragraph.elements[0].endIndex
- 收件人:
content[offset].paragraph.elements[0].endIndex
参考资料:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。