hunter21188
hunter21188

Reputation: 415

Remove Horizontal Line in Google Doc with Google Apps Script

I have a Google Doc with text followed by a Horizontal Line below. If the user selects "NO" from a ui.alert, I need to remove all this text (simple using regex) and the horizontal line. I have no clue how to remove this Horizontal Line via Google Apps Script. Can't find anything about it in the documentation. Anyone have any ideas? Thanks!

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, '');
  }

Upvotes: 1

Views: 1701

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to remove the searched text in Google Document.
  • You want to delete "HORIZONTAL_RULE" below the text.
  • You want to run above when the user selects "NO" from a ui.alert.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this sample script? Although I'm not sure about your actual Document, from your explanation, I imaged about it and prepare a sample script. Please think of this as just one of several answers. The flow of this sample script is as follows.

Flow:

  1. Search text is searched using findText().
  2. Put the element of searched text in an array.
    • This array is used for deleting element.
  3. Search "HORIZONTAL_RULE" below the searched text.
    • In this case, 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.
    • When "HORIZONTAL_RULE" is found, the element is put to the array.
  4. Delete elements in the array.
    • From your script, the searched text is cleared. In this case, the paragraph is not deleted.
    • From your question, about "HORIZONTAL_RULE", the paragraph is deleted.

When above flow is reflected to the script, it becomes as follows.

Sample script:

When you run the script, the texts searched with regExpFirstBriefing are cleared and "HORIZONTAL_RULE" below the text is also removed.

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();
    }
  }
}

Note:

  • This script supposes that the regex of [A-Z \(\)]{42}\\v+[A-Za-z\.\", ]*[\\v+]{1} works for your Document.
  • If you want to delete the paragraph of searched text, please delete this if statement of as follows from above script.

    if (remove[i].getType() === DocumentApp.ElementType.TEXT) {
      remove[i].removeFromParent();
      continue;
    }
    

References:

If I misunderstood your question and this was not the result you want, I apologize. At that time, in order to correctly understand your situation, can you provide a sample Document you want to use? Of course, please remove your personal information. I would like to confirm the issue from it.

Upvotes: 1

Related Questions