function udpateQuestion() {
var doc = DocumentApp.getActiveDocument(); // Get the active document
var body = doc.getBody(); // Get the body of the document
var paragraphs = body.getParagraphs(); // Get all paragraphs in the document
// Iterate through each paragraph in the document
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i]; // Get the current paragraph
// Checks if the paragraph style is Heading 3
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
// Get the index of the next element in the document
var nextElementIndex = body.getChildIndex(paragraph) + 1;
// Get the next element using the index
var nextElement = body.getChild(nextElementIndex);
// Log the type of the next element (for debugging purposes)
Logger.log(nextElement.getType());
// Check if the next element is of type PARAGRAPH
if (nextElement.getType() === DocumentApp.ElementType.PARAGRAPH) {
// Get the text content of the next paragraph
var nextText = nextElement.asParagraph().getText();
// Set the text content of the current heading 3 paragraph to be the same as the next paragraph
paragraph.setText(nextText);
// Remove the original next element (the next paragraph)
body.removeChild(nextElement);
}
}
}
}
Here’s a breakdown of how the code works:
- It starts by getting the active document and the body of the document using
DocumentApp.getActiveDocument()
andgetBody()
. - It retrieves all the paragraphs in the document using
getParagraphs()
and stores them in theparagraphs
array. - The script then iterates through each paragraph in the
paragraphs
array. - For each paragraph, it checks if the paragraph style is set to “Heading 3” using
paragraph.getHeading()
. This is likely used to identify specific sections of the document. - If the paragraph is indeed a Heading 3, it proceeds to work with the next element in the document.
- It calculates the index of the next element in the document using
body.getChildIndex(paragraph) + 1
. - It retrieves the next element using the calculated index and logs its type using
Logger.log(nextElement.getType())
. This is for debugging purposes to see the type of the next element. - It checks if the next element is of type
PARAGRAPH
usingnextElement.getType() === DocumentApp.ElementType.PARAGRAPH
. - If the next element is indeed a paragraph, it gets the text content of the next paragraph using
nextElement.asParagraph().getText()
. - It then sets the text content of the current Heading 3 paragraph to be the same as the next paragraph using
paragraph.setText(nextText)
. - Finally, it removes the original next element (the next paragraph) from the document using
body.removeChild(nextElement)
.
In summary, this script is designed to find Heading 3 paragraphs in the document, take the content of the next paragraph, and replace the content of the Heading 3 paragraph with it, effectively merging the two paragraphs while removing the original next paragraph.
function udpateQuestion() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
// Checks if the paragraph style is Heading 1
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
var nextElementIndex = body.getChildIndex(paragraph) + 1;
var nextElement = body.getChild(nextElementIndex);
Logger.log(nextElement.getType());
if (nextElement.getType() === DocumentApp.ElementType.PARAGRAPH) {
var nextText = nextElement.asParagraph().getText();
paragraph.setText(nextText);
body.removeChild(nextElement); // Remove the original next element
}
}
}
}