Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

javascript - Removing a paragraph of 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...