Tom
Tom

Reputation: 11

Google Apps Script to delete text/lines between 2 keywords

I would like to use google apps script to copy full content of another document to current document and then perform some clean up.

The workflow I can think of is:

  1. clear current document

    DocumentApp.getActiveDocument().getBody().editAsText().setText('');
  2. copy content from an other document (not sure what is the syntax)

  3. find and delete <keyword> ... </keyword>. It can be single line or pan across multiple lines and having table or diagram in between. e.g.

Before

This is a <keyword>big black</keyword> cat.

<keyword>
Title
... table here
... diagram here
</keyword>

End of doc.

After

This is a cat.

End of doc.

I need help in creating Google Apps Script for item #2 & #3 in above.

Upvotes: 0

Views: 3902

Answers (2)

Sara Rodriguez
Sara Rodriguez

Reputation: 11

I have a great solution here for #3.

In google docs, the text is:

Quero que todo o texto a seguir suma:

start
TEXTO QUE QUERO QUE SUMA
end

Será que deu certo??

In Google Apps Script:

function BoldBetweenTags() {
var testeid = '1fltAxuhgfUOOdRvvIyXfLz13gxPDvtugUMyAGOK0jcE';
var body = DocumentApp.openById(testeid).getBody();
var text = body.editAsText();
var para = body.getParagraphs();
  
var startTag = 'start';
var endTag = 'end'

var del = "no";  
  
  for(var i in para){  
  Logger.log('Parágrafo: '+i)
    var from = para[i].findText(startTag);
    var to =  para[i].findText(endTag);
    if (from != null){var del = "yes";  para[i].editAsText().removeFromParent();}
    if (from == null && to == null && del == "yes"){para[i].editAsText().removeFromParent();}
    if (to != null){var del = "no";  para[i].editAsText().removeFromParent();}
}}

For #2 you need to

var folder =  DriveApp.createFolder("name of the folder");
var rep = DriveApp.getFileById('your ID');
var repi = rep.makeCopy("name of your new document", folder);

This works for me

Upvotes: 1

Tom
Tom

Reputation: 11

For item #3, here is my working code:

function deleteSection(keyword) {

  var start = ' *\<' + keyword + '\> *';
  var end   = ' *\<\/' + keyword + '\> *';

  var bodyElement = DocumentApp.getActiveDocument().getBody();
  var found = 0;

  // have to do reverse else the result is incorrect
  var totalchildren = bodyElement.getNumChildren();
  for (var i=totalchildren-1; i >= 0; i--) {
    if (found == 0) {
      if (bodyElement.getChild(i).asText().findText(end) !== null) {
        bodyElement.getChild(i).asText().replaceText(".*"+end,"");
        if (bodyElement.getChild(i).asText().findText("^ *$") !== null) {
          bodyElement.getChild(i).removeFromParent();
        }
        found = 1;
      }
    } else {
      if (bodyElement.getChild(i).asText().findText(start) !== null) {
        bodyElement.getChild(i).asText().replaceText(start+".*","");
        if (bodyElement.getChild(i).asText().findText("^ *$") !== null) {
          bodyElement.getChild(i).removeFromParent();
        }
        found = 0;
      } else {
        bodyElement.getChild(i).removeFromParent();
      }
    }  
  }
}

Upvotes: 0

Related Questions