javascript - Removing a paragraph of text - Stack Overflow

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

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?

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?

Share Improve this question edited Aug 10, 2015 at 23:40 user4639281 asked Jun 1, 2015 at 16:24 Jordan ThibodeauJordan Thibodeau 652 silver badges7 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

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./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./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./apps-script/reference/document/range-element#getEndOffsetInclusive()

deleteText(startOffset, endOffsetInclusive)
Deletes a range of text.
https://developers.google./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();
}

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./apps-script/reference/document/body

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316765a4622256.html

相关推荐

  • javascript - Removing a paragraph of text - Stack Overflow

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

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信