Body
Body:
{}Represents the document body containing all sections.
Remarks
Section titled “Remarks”Body is the container for all Section elements in a document. Each section can
have its own page setup (size, margins, orientation) and headers/footers. Most documents
have a single section, but documents can be divided into multiple sections for different
page layouts or header/footer configurations.
The body is accessed via Document.body and provides methods to:
- Get all sections (
sections) - Add new sections (
addSection) - Remove sections (
removeSection) - Replace text across all sections (
replaceText)
Examples
Section titled “Examples”Access and iterate through sections:
const body = document.body();const sections = body.sections();
console.log(`Document has ${sections.length} sections`);
for (const section of sections) { const content = section.content(); const paragraphs = content.blocklevels() .filter(bl => bl.type === 'paragraph'); console.log(`Section has ${paragraphs.length} paragraphs`);}Add a new section to the document:
const body = document.body();
// Add section at the endconst newSection = body.addSection();
// Configure page setupconst pageSetup = newSection.pageSetup();pageSetup.setPageSize({ width: 12240000, height: 15840000 }); // Letter size
// Add contentconst para = newSection.content().addParagraph();para.asTextView().setText('This is a new section');Replace text across all sections:
const body = document.body();
// Replace across entire document bodyconst count = body.replaceText(/TODO/g, { text: 'DONE', formatting: { highlight: '#00ff00' }});
console.log(`Replaced ${count} occurrences across all sections`);Remove a section:
const body = document.body();const sections = body.sections();
if (sections.length > 1) { // Remove the last section const removed = body.removeSection(sections.length - 1); if (removed) { console.log('Removed last section'); }}Properties
Section titled “Properties”replaceText
Section titled “replaceText”replaceText:
ReplaceTextSignature
Replaces all occurrences of a pattern across all sections.
Remarks
Section titled “Remarks”Convenience method that searches and replaces text across all sections in the body, including all content, headers, and footers. This provides a simpler API than manually iterating through sections.
See ReplaceTextSignature for detailed parameter and usage information.
Examples
Section titled “Examples”const count = body.replaceText(/old/g, 'new');console.log(`Replaced ${count} occurrences`);body.replaceText(/ERROR/gi, { text: 'ERROR', formatting: { color: '#ff0000', bold: true }});Methods
Section titled “Methods”sections()
Section titled “sections()”sections():
Section[]
Gets all sections in the document body.
Returns
Section titled “Returns”Section[]
An array of Section objects
Remarks
Section titled “Remarks”Returns all sections in the document in order. Most documents have a single section, but documents can contain multiple sections with different page setups or headers/footers.
Example
Section titled “Example”const sections = body.sections();console.log(`Document has ${sections.length} sections`);
sections.forEach((section, index) => { console.log(`Section ${index + 1}`);});addSection()
Section titled “addSection()”addSection(
index?):Section
Adds a new section to the document body.
Parameters
Section titled “Parameters”index?
Section titled “index?”Optional zero-based position to insert the section. If omitted, adds at the end.
Returns
Section titled “Returns”The newly created Section
Remarks
Section titled “Remarks”Creates and inserts a new section at the specified position. If no index is provided, the section is added at the end. The new section is initialized with default page setup and empty content.
Use this when you need different page layouts, orientations, or headers/footers for different parts of your document.
Examples
Section titled “Examples”Add section at the end:
const newSection = body.addSection();const para = newSection.content().addParagraph();para.asTextView().setText('New section content');Insert section at specific position:
// Insert as the second section (index 1)const section = body.addSection(1);removeSection()
Section titled “removeSection()”Removes a section from the document body.
Parameters
Section titled “Parameters”Zero-based index of the section to remove
Returns
Section titled “Returns”The removed Section, or undefined if the index is invalid
Remarks
Section titled “Remarks”Removes the section at the specified index. If the index is out of bounds,
returns undefined. Be cautious when removing sections as this removes all
content within that section.
Examples
Section titled “Examples”const sections = body.sections();if (sections.length > 1) { // Remove the last section const removed = body.removeSection(sections.length - 1); if (removed) { console.log('Successfully removed section'); }}Remove specific section with confirmation:
const indexToRemove = 2;const sections = body.sections();
if (indexToRemove < sections.length) { const removed = body.removeSection(indexToRemove); console.log(`Removed section ${indexToRemove + 1}`);}