Removing a paragraph of text

2019-01-28 07:05发布

问题:

I built an app script to copy a doc and edit it based upon the data I have entered into a Google spreadsheet.

Once the doc is copied and edited, I would like to remove certain text paragraphs.

I was using the following to see if I could find the position of the text I was searching for, but my code returned Range Element.

var body = DocumentApp.openById(copyID).getBody();
var para = body.findText('X1');

Logger.log(para);

My ideal state would be to provide apps script with two positions within the document, and then have Apps Script delete all the text within that range.

How can I generate the correct offsets and delete the text?

回答1:

I was using the following to see if I could find the position of the text I was searching for, but my code returned Range Element.

That is supposed to happen, here is some information from the documentation about range elements.

Class RangeElement
A wrapper around an Element with a possible start and end offset. These offsets allow a range of characters within a Text element to be represented in search results, document selections, and named ranges.
https://developers.google.com/apps-script/reference/document/range-element


My ideal state would be to provide apps script with two positions within the document, and then have Apps Script delete all the text within those ranges.

Use the following to retrieve the offsets and delete the text.

var startOffset = rangeElement.getStartOffset();
var endOffset = rangeElement.getEndOffsetInclusive();
rangeElement.getElement().asText().deleteText(startOffset,endOffset);

getStartOffset()
Gets the position of the start of a partial range within the range element. If the element is a Text element and isPartial() returns true, the offset is the number of characters before the start of the range (that is, the index of the first character in the range); in any other case, this method returns -1.
https://developers.google.com/apps-script/reference/document/range-element#getStartOffset()

getEndOffsetInclusive()
Gets the position of the end of a partial range within the range element. If the element is a Text element and isPartial() returns true, the offset is the number of characters before the last character in the range (that is, the index of the last character in the range); in any other case, this method returns -1.
https://developers.google.com/apps-script/reference/document/range-element#getEndOffsetInclusive()

deleteText(startOffset, endOffsetInclusive)
Deletes a range of text.
https://developers.google.com/apps-script/reference/document/text#deleteText(Integer,Integer)


How can I generate the correct offsets and delete the text?

The following script will do that.

var rangeElement = DocumentApp.openById(copyID).getBody().findText('X1');
if (rangeElement.isPartial()) {
     var startOffset = rangeElement.getStartOffset();
     var endOffset = rangeElement.getEndOffsetInclusive();
     rangeElement.getElement().asText().deleteText(startOffset,endOffset);
} else {
     rangeElement.getElement().removeFromParent();
}


回答2:

I've been trying to solve this and found the following snippet that works:

var start = '{testingstart}'; // Starting text
var end = '{testingend}'; // Ending text
var startPara = findThisText(body, start); // Find element that has start text
var endPara = findThisText(body, end);  // Find element that has end text
var stIndex = body.getChildIndex(startPara); // Find index number of the starting child element
var edIndex = body.getChildIndex(endPara);  // Find the index number of the ending child element

var loop = edIndex - stIndex;
for(var i = 0; i < loop; i++){
  // Keep removing the child element with index = stIndex until the loop ends.
  var removing = body.getChild(stIndex);
  body.removeChild(removing);
}

I also use the following helper function to find the element I need with a given text:

// Helper function to locate field based on search text
function findThisText(body, text) {
    var found = body.findText(text);
    if(found) {
      return found.getElement().getParent();
    } else {
      return null;
    }
}

What's good about it is that it removes any ElementType between the start and end targets. It's been useful for me if I also have Tables in between the paragraphs. Reference: https://developers.google.com/apps-script/reference/document/body