- You want to insert a table after the text of
#PLACEHOLDER#
in Google Document.
- From your question, I thought that
#PLACEHOLDER#
is put to a paragraph, and the paragraph includes only the text of #PLACEHOLDER#
.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this sample script?
Pattern 1:
When there is only one text of #PLACEHOLDER#
in the Google Document, how about the following modification?
Modified script:
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
// I added below script.
var ele = range.getElement();
if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
var offset = body.getChildIndex(ele.getParent());
body.insertTable(offset + 1, data);
}
Pattern 2:
When there is multiple texts of #PLACEHOLDER#
in the Google Document, how about the following modification?
Modified script:
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
// I added below script.
while (range) {
var ele = range.getElement();
if (ele.getParent().getParent().getType() === DocumentApp.ElementType.BODY_SECTION) {
var offset = body.getChildIndex(ele.getParent());
body.insertTable(offset + 1, data);
}
range = body.findText("#PLACEHOLDER#", range);
}
Note:
- These modified scripts suppose that
data
is table
or 2 dimensional array.
References:
If I misunderstood your question and this was not the result you want, I apologize. At that time, can you provide a sample Document you want to use? By this, I would like to confirm the issue.
findText()
return according to documentation? – TheMaster