I want to append table to google doc after specific text. Here is my attempt.
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
body.appendTable(data);
Here data is some multidimensional array. This works fine. But table gets appended at the end of body. I want to append table after '#PLACEHOLDER#'.
var body = somedoc.getBody();
var range = body.findText("#PLACEHOLDER#");
range.appendTable(data);
This doesn't work. How to get position of range (text: #PLACEHOLDER#) and append table after it?
To concatenate two strings (i.e., to join them together), use the concatenation operator ( + ). Both the concatenation operator and the addition operator have the same symbol + but Apps Script will figure out which operation to perform based on the values being operated on.
The onSelectionChange(e) trigger runs automatically when a user changes the selection in a spreadsheet. To activate this trigger, you must refresh the spreadsheet once the trigger is added and every time the spreadsheet is opened.
#PLACEHOLDER#
in Google Document.
#PLACEHOLDER#
is put to a paragraph, and the paragraph includes only the text of #PLACEHOLDER#
.If my understanding is correct, how about this sample script?
When there is only one text of #PLACEHOLDER#
in the Google Document, how about the following modification?
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);
}
When there is multiple texts of #PLACEHOLDER#
in the Google Document, how about the following modification?
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);
}
data
is table
or 2 dimensional array.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.
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