如何使用 google api 脚本从 google 文档中删除特定文本后的所有部分?
How to erase all parts from google doc after specific text using google api scripts?
问题是删除所有内容段落、图像、表格或某些特定文本(在我的例子中为“#PLACEHOLDER#”)之后的任何内容。是需要遍历所有还是在得到range的位置后就清除所有内容?
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(offset + 1)
或 body.setText('',offset + 1)
的内容吗?
- 您想删除 Google 文档中
#PLACEHOLDER#
段落之后正文中的所有子项。
- 您想使用 Google Apps 脚本实现此目的。
如果我的理解是正确的,这个修改怎么样?请将此视为几个答案之一。
模式 1:
在此模式中,使用文档服务。
修改后的脚本:
请修改如下。本次修改修改了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--) {
模式二:
在此模式中,使用了 Google 文档 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
参考文献:
- getNumChildren()
- removeChild()
- Advanced Google services
- Method: documents.get
- Method: documents.batchUpdate
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
问题是删除所有内容段落、图像、表格或某些特定文本(在我的例子中为“#PLACEHOLDER#”)之后的任何内容。是需要遍历所有还是在得到range的位置后就清除所有内容?
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(offset + 1)
或 body.setText('',offset + 1)
的内容吗?
- 您想删除 Google 文档中
#PLACEHOLDER#
段落之后正文中的所有子项。 - 您想使用 Google Apps 脚本实现此目的。
如果我的理解是正确的,这个修改怎么样?请将此视为几个答案之一。
模式 1:
在此模式中,使用文档服务。
修改后的脚本:
请修改如下。本次修改修改了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--) {
- 发件人:
模式二:
在此模式中,使用了 Google 文档 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
- 发件人:
参考文献:
- getNumChildren()
- removeChild()
- Advanced Google services
- Method: documents.get
- Method: documents.batchUpdate
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。