我有一个Google文档,其文本下面是水平线。如果用户从ui.alert
选择"否",我需要删除所有这些文本(简单使用Regex(和水平线。我不知道如何通过Google Apps脚本删除此水平线。在文档中找不到任何内容。有人有主意吗?谢谢!
var regExpFirstBriefing = "[A-Z ()]{42}\v+[A-Za-z.", ]*[\v+]{1}"; // This accounts for all the text I need removed along with an extra new line. The horizontal line is the next line.
// Ask user if this is the first briefing
var responseFirstBriefing = ui.alert('Question here...' , ui.ButtonSet.YES_NO);
if (responseFirstBriefing == ui.Button.YES) {
document.replaceText(regExpFirstBriefing, '');
}
- 您想删除Google文档中的搜索文本。
- 您想在文本下方删除" Horizontal_rule"。
- 当用户从
ui.alert
中选择" no"时,您想在上方运行。 - 您想使用Google Apps脚本实现此目的。
如果我的理解是正确的,那么此示例脚本怎么样?尽管我不确定您的实际文档,但从您的解释中,我对此进行了成像并准备了一个示例脚本。请将其视为几个答案之一。此示例脚本的流量如下。
流:
- 使用
findText()
搜索搜索文本。 - 将搜索文本的元素放在数组中。
- 此数组用于删除元素。
- 搜索" Horizontal_rule"下面的文本下方。
- 在这种情况下,当" Horizontal_rule"不相邻搜索的文本时,
offsetValue
搜索了" Horizontal_rule"。在此示例中,它最多可搜索到前面的3段。 - 当找到" Horizontal_rule"时,将元素放在数组中。
- 在这种情况下,当" Horizontal_rule"不相邻搜索的文本时,
- 删除数组中的元素。
- 从您的脚本中,清除了搜索的文本。在这种情况下,该段落未删除。
- 从您的问题中,关于" Horizontal_rule",该段落被删除。
上面的流程反映到脚本时,如下。
示例脚本:
运行脚本时,清除了使用regExpFirstBriefing
搜索的文本,并且在文本下方也已删除" Horizontal_rule"。
function myFunction() {
var document = DocumentApp.getActiveDocument(); // Added
var ui = DocumentApp.getUi(); // Added
var regExpFirstBriefing = "[A-Z ()]{42}\v+[A-Za-z.", ]*[\v+]{1}";
var responseFirstBriefing = ui.alert('Question here...' , ui.ButtonSet.YES_NO);
if (responseFirstBriefing == ui.Button.YES) {
document.replaceText(regExpFirstBriefing, '');
// I added below script.
} else if (responseFirstBriefing == ui.Button.NO) {
var offsetValue = 3; // When "HORIZONTAL_RULE" doesn't adjacent the searched text, "HORIZONTAL_RULE" is searched by "offsetValue". In this sample, it is searched up to 3 paragraph ahead.
var body = document.getBody();
var r = body.findText(regExpFirstBriefing);
var remove = [];
while (r) {
remove.push(r.getElement().asText())
var parentParagraph = body.getChildIndex(r.getElement().getParent());
var totalChildren = body.getNumChildren();
for (var offset = 1; offset <= offsetValue; offset++) {
if (parentParagraph + offset <= totalChildren) {
var nextParagraph = body.getChild(parentParagraph + offset);
if (nextParagraph.getType() === DocumentApp.ElementType.PARAGRAPH) {
var c = nextParagraph.asParagraph().getNumChildren();
for (var i = 0; i < c; i++) {
var childOfNextParagraph = nextParagraph.asParagraph().getChild(i);
if (childOfNextParagraph.getType() === DocumentApp.ElementType.HORIZONTAL_RULE) {
remove.push(childOfNextParagraph.asHorizontalRule());
break;
}
}
if (remove[remove.length - 1].getType === DocumentApp.ElementType.HORIZONTAL_RULE) {
break;
}
}
}
}
r = body.findText(regExpFirstBriefing, r);
}
for (var i = remove.length - 1; i >=0; i--) {
/////
// If you want to delete the paragraph of searched text, please delete this if statement.
if (remove[i].getType() === DocumentApp.ElementType.TEXT) {
remove[i].removeFromParent();
continue;
}
/////
remove[i].getParent().asParagraph().removeFromParent();
}
}
}
注意:
- 此脚本认为
[A-Z ()]{42}\v+[A-Za-z.", ]*[\v+]{1}
的正则适用于您的文档。 如果要删除搜索文本的段落,请删除以下脚本的语句。
if (remove[i].getType() === DocumentApp.ElementType.TEXT) { remove[i].removeFromParent(); continue; }
参考:
- findText(searchpattern,来自(
- remove fromparent((
- 类Horizontalrule
如果我误解了您的问题,这不是您想要的结果,我深表歉意。当时,为了正确理解您的情况,您可以提供要使用的示例文档吗?当然,请删除您的个人信息。我想从中确认问题。