Google apps script 如何使用谷歌api脚本在特定文本之后从谷歌文档中删除所有部分?
问题是删除某些特定文本之后的所有内容段落、图像、表格或其他内容(在我的例子中是“#占位符”)。我是否需要循环全部或在获得范围位置后可以清除所有内容Google apps script 如何使用谷歌api脚本在特定文本之后从谷歌文档中删除所有部分?,google-apps-script,google-docs,google-docs-api,Google Apps Script,Google Docs,Google Docs Api,问题是删除某些特定文本之后的所有内容段落、图像、表格或其他内容(在我的例子中是“#占位符”)。我是否需要循环全部或在获得范围位置后可以清除所有内容 var body = somedoc.getBody(); var range = body.findText("#PLACEHOLDER#"); var ele = range.getElement(); if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BO
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
var ele = range.getElement();
if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
var offset = body.getChildIndex(ele.getParent());
}
我可以添加类似于body.delteText(偏移量+1)
或body.setText('',偏移量+1)
?- 您希望删除Google文档中段落
后正文中的所有子项#占位符#
- 您希望使用谷歌应用程序脚本实现这一点
- 在这种情况下,
的段落不会被删除。如果您还想删除#占位符#
的段落,请修改如下。#占位符#
- From:
for(var i=numChildren-2;i>offset;i--){
- To:
for(var i=numChildren-2;i>=offset;i--){
- From:
body.findText(“#占位符#”)
的结果,而不使用用于删除子项的for循环
修改脚本:
请按如下方式修改。在此修改中,if语句中的脚本被修改
发件人:
致:
- 在这种情况下,不会删除
的段落。如果要同时删除#占位符#
的段落,请按以下方式修改。#占位符#
- 发件人:
content[offset+1]。段落。元素[0]。endIndex
- 收件人:
content[offset]。段落。元素[0]。endIndex
- 发件人:
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));
}
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());