I'm trying to retrospectively append a paragraph at a certain point to a document in Google Apps Script, not at the end of the document. But everything I've found will only allow me to insert a paragraph at the specified point, which adds it before the point where I want.
So my document has the word "Summary" at a certain point within it. I'm using findText() to locate this position. I can then insert text (which appends text to the element), but it's part of the same paragraph, or insert a paragraph (which puts the paragraph before "Summary"). Neither are what I want. I basically want to add some lines of text after the Summary, but I also want to apply different formatting to these new lines, like setting them to bold with normal text.
The code I'm using to find the position is below, but what do I do next to then append a paragraph after the word "Summary" (which is not at the end of the document).
var tmpBody = tmpDoc.getBody();
var matchPosition = tmpBody.findText("Summary");
I believe your goal is as follows.
In this case, how about the following sample script?
function myFunction() {
const searchText = "Summary";
const addParagraphs = ["Added paragraph 1", "Added paragraph 2", "Added paragraph 3"];
const style = { [DocumentApp.Attribute.BOLD]: true };
const paragraphs = addParagraphs.join("\n");
const body = DocumentApp.getActiveDocument().getBody();
let s;
while (s = body.findText(searchText, s)) {
const e = s.getElement();
if (e.asText().getText().trim() == searchText) {
body.insertParagraph(body.getChildIndex(e.getParent()) + 1, paragraphs).setAttributes(style);
}
}
}
The above script supposes multiple paragraphs of "Summary". But, if the paragraph of "Summary" includes only one in the document and you want to insert the paragraphs to the 1st "Summary", I think that the following script can be also used.
function myFunction() {
const searchText = "Summary";
const addParagraphs = ["Added paragraph 1", "Added paragraph 2", "Added paragraph 3"];
const style = { [DocumentApp.Attribute.BOLD]: true };
const paragraphs = addParagraphs.join("\n");
const body = DocumentApp.getActiveDocument().getBody();
const s = body.findText(searchText);
if (!s) return;
const e = s.getElement();
if (e.asText().getText().trim() == searchText) {
body.insertParagraph(body.getChildIndex(e.getParent()) + 1, paragraphs).setAttributes(style);
}
}
or
function myFunction() {
const searchText = "Summary";
const addParagraphs = ["Added paragraph 1", "Added paragraph 2", "Added paragraph 3"];
const style = { [DocumentApp.Attribute.BOLD]: true };
const paragraphs = addParagraphs.join("\n");
const body = DocumentApp.getActiveDocument().getBody();
const s = body.getParagraphs().find(p => p.getText().trim() == searchText);
if (!s) return;
body.insertParagraph(body.getChildIndex(s) + 1, paragraphs).setAttributes(style);
}
When this script is run, the following result is obtained.
const addParagraphs = ["Added paragraph 1", "Added paragraph 2", "Added paragraph 3"];
to const addParagraphs = ["Added paragraph 1"];
..setAttributes(style);
like body.insertParagraph(body.getChildIndex(e.getParent()) + 1, paragraphs);
.Your following 2nd question,
I think that's just what I'm looking for, but one question, what if I don't want all the text in bold. So for example, you've added "Added Paragraph 1", but what if I wanted it to look like "Added Paragraph 1". Is there a way to do that?
Although, unfortunately, I'm not sure which you used from my 3 proposed scripts, when the 3rd script is modified, please modify it as follows.
function myFunction() {
const searchText = "Summary";
const addParagraphs = ["Added paragraph 1", "Added paragraph 2", "Added paragraph 3"];
const style = { [DocumentApp.Attribute.BOLD]: true };
const paragraphs = addParagraphs.join("\n");
const body = DocumentApp.getActiveDocument().getBody();
const s = body.getParagraphs().find(p => p.getText().trim() == searchText);
if (!s) return;
body.insertParagraph(body.getChildIndex(s) + 1, paragraphs).editAsText().setAttributes(0, addParagraphs[0].length, style);
}
By this modification, only the 1st paragraph of the inserted 3 paragraphs is set as bold type.