Section
Section:
{}Represents a section within a document body.
Remarks
Section titled “Remarks”Section is a major organizational unit within a document. Each section can have its own:
- Page setup (size, margins, orientation) via
pageSetup - Headers and footers via
headersAndFooters - Content (paragraphs and tables) via
content
Most documents have a single section with consistent page layout and headers/footers throughout. Multi-section documents are used when you need different page layouts (e.g., landscape vs portrait) or different headers/footers (e.g., different headers per chapter) in different parts of the document.
Sections are obtained from Body.sections and can be added with Body.addSection.
Examples
Section titled “Examples”Access and work with section components:
const sections = document.body().sections();const section = sections[0];
// Access page setupconst pageSetup = section.pageSetup();const size = pageSetup.pageSize();console.log(`Page size: ${size.width} x ${size.height}`);
// Access contentconst content = section.content();const blockLevels = content.blocklevels();console.log(`Section has ${blockLevels.length} elements`);
// Access headers/footersconst hf = section.headersAndFooters();const headers = hf.headers();Add content to a section:
const section = document.body().sections()[0];const content = section.content();
// Add a paragraphconst paragraph = content.addParagraph();paragraph.asTextView().setText('This is a new paragraph.');
// Add a tableconst table = content.addTable();const row = table.addRow();const cell = row.addCell();Replace text within a section:
const section = document.body().sections()[0];
// Replace text in this section only (content + headers/footers)const count = section.replaceText(/TODO/g, { text: 'DONE', formatting: { highlight: '#ffff00' }});
console.log(`Replaced ${count} occurrences in this section`);Properties
Section titled “Properties”replaceText
Section titled “replaceText”replaceText:
ReplaceTextSignature
Replaces all occurrences of a pattern within this section.
Remarks
Section titled “Remarks”Convenience method that searches and replaces text across all content in this
section, including the main content, headers, and footers. This is scoped to
just this section, unlike Body.replaceText or Document.replaceText
which operate across all sections.
See ReplaceTextSignature for detailed parameter and usage information.
Examples
Section titled “Examples”// Replace only in this sectionconst count = section.replaceText(/old/g, 'new');console.log(`Replaced ${count} occurrences in section`);section.replaceText(/DRAFT/gi, { text: 'FINAL', formatting: { color: '#00aa00', bold: true }});Methods
Section titled “Methods”pageSetup()
Section titled “pageSetup()”pageSetup():
PageSetup
Gets the page setup configuration for this section.
Returns
Section titled “Returns”The PageSetup for this section
Remarks
Section titled “Remarks”Returns the page setup object that controls the physical page properties for this section, including page size (width/height) and margins. Each section can have different page setup settings.
Example
Section titled “Example”const pageSetup = section.pageSetup();const size = pageSetup.pageSize();const margins = pageSetup.pageMargins();
console.log(`Page: ${size.width}x${size.height}`);console.log(`Margins: T:${margins.top} B:${margins.bottom}`);headersAndFooters()
Section titled “headersAndFooters()”headersAndFooters():
HeadersAndFooters
Gets the headers and footers configuration for this section.
Returns
Section titled “Returns”The HeadersAndFooters container for this section
Remarks
Section titled “Remarks”Returns an object that provides access to headers and footers for this section. Headers and footers can have different content for the first page, even pages, and default (odd) pages.
Example
Section titled “Example”const hf = section.headersAndFooters();const headers = hf.headers();const footers = hf.footers();
// Access default headerconst defaultHeader = headers.default();if (defaultHeader) { const para = defaultHeader.addParagraph(); para.asTextView().setText('Document Title');}content()
Section titled “content()”content():
BlockLevelContainer
Gets the main content container for this section.
Returns
Section titled “Returns”The BlockLevelContainer containing the section’s content
Remarks
Section titled “Remarks”Returns a container that holds all block-level elements (paragraphs and tables) for this section. This is where the main document content resides, separate from headers and footers.
Example
Section titled “Example”const content = section.content();const blockLevels = content.blocklevels();
// Iterate through paragraphs and tablesfor (const bl of blockLevels) { if (bl.type === 'paragraph') { const text = bl.asTextView().getPlainText(); console.log('Paragraph:', text); } else if (bl.type === 'table') { console.log('Table with', bl.rows().length, 'rows'); }}