I want to place multiple paragraphs from a different document at a placeholder in my document.
The placeholder in the final document is "{plc}".
I've tried this:
var docNew = DocumentApp.openById(docNewID);
var docNewBody = docNew.getBody();
var docTempBody = DocumentApp.openById(docTempID).getBody();
var docTempPars = docTempBody.getParagraphs();
var range = docNewBody.findText("{plc}").getElement().getParent().getChildIndex();
docTempPars.forEach(function(p2){
docNewBody.insertParagraph(range, p2);
var range = range + 1;
})
But nothing at all is added. Can someone help out please?
I believe your goal as follows.
{plc} in docNewBody with the all paragraphs of docTempBody using Google Apps Script.{plc} in the document.getChildIndex(child) has the argument of child.
var range = docNewBody.findText("{plc}").getElement().getParent().getChildIndex();.{plc} is not removed.copy().docTempBody.{plc}.{plc}.When your script is modified, it becomes as follows.
function myFunction() {
var docNew = DocumentApp.openById(docNewID);
var docNewBody = docNew.getBody();
// 1. Retrieve all paragraphs from `docTempBody`.
var docTempBody = DocumentApp.openById(docTempID).getBody();
var docTempPars = docTempBody.getParagraphs();
console.log(docTempPars.length)
// 2. Retrieve the child index of `{plc}`.
var element = docNewBody.findText("{plc}").getElement().getParent();
var range = docNewBody.getChildIndex(element);
// 3. Remove `{plc}`.
element.removeFromParent();
// 4. Insert the paragraphs.
docTempPars.forEach(function(p2){
docNewBody.insertParagraph(range++, p2.copy());
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With