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
Solution 1:
I've been trying to solve this and found the following snippet that works:
varstart='{testingstart}'; // Starting textvarend='{testingend}'; // Ending textvarstartPara= findThisText(body, start); // Find element that has start textvarendPara= findThisText(body, end); // Find element that has end textvarstIndex= body.getChildIndex(startPara); // Find index number of the starting child elementvaredIndex= body.getChildIndex(endPara); // Find the index number of the ending child elementvarloop= edIndex - stIndex;
for(vari=0; i < loop; i++){
// Keep removing the child element with index = stIndex until the loop ends.varremoving= 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 textfunctionfindThisText(body, text) {
var found = body.findText(text);
if(found) {
return found.getElement().getParent();
} else {
returnnull;
}
}
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
Post a Comment for "Removing A Paragraph Of Text"