BlockLevel
Union type representing the block-level elements that can appear in a document.
Remarks
Section titled “Remarks”BlockLevel is a discriminated union of Paragraph and Table, the two
types of block-level elements that can exist within a document’s content structure. Block-level
elements are the primary building blocks of document content, appearing within:
- Section content (
Section.content) - Table cells (
TableCell) - Footnote content (
Footnote.content) - Endnote content (
Endnote.content) - Headers and footers (
HeaderFooter)
Use the type discriminator property to determine which specific block-level element you’re
working with:
Block-level elements are obtained from BlockLevelContainer.blocklevels, which returns
an array of BlockLevel elements that you can iterate through and process based on their type.
Examples
Section titled “Examples”Iterate through and process block-level elements:
const content = section.content();const blockLevels = content.blocklevels();
for (const blockLevel of blockLevels) { if (blockLevel.type === 'paragraph') { // It's a paragraph const textView = blockLevel.asTextView(); const text = textView.getPlainText(); console.log('Paragraph:', text); } else if (blockLevel.type === 'table') { // It's a table const rows = blockLevel.rows(); console.log(`Table with ${rows.length} rows`); }}Count paragraphs and tables in a section:
const blockLevels = section.content().blocklevels();
let paragraphCount = 0;let tableCount = 0;
for (const bl of blockLevels) { if (bl.type === 'paragraph') { paragraphCount++; } else if (bl.type === 'table') { tableCount++; }}
console.log(`Section has ${paragraphCount} paragraphs and ${tableCount} tables`);Process only paragraphs with specific text:
const blockLevels = section.content().blocklevels();
for (const bl of blockLevels) { if (bl.type === 'paragraph') { const text = bl.asTextView().getPlainText(); if (text.includes('TODO')) { // Highlight TODOs in red bl.replaceText(/TODO/g, { text: 'TODO', formatting: { color: '#ff0000', bold: true } }); } }}Find the first table in a section:
const blockLevels = section.content().blocklevels();const firstTable = blockLevels.find(bl => bl.type === 'table');
if (firstTable && firstTable.type === 'table') { const rows = firstTable.rows(); console.log(`First table has ${rows.length} rows`);} else { console.log('No tables found');}Extract all text from both paragraphs and tables:
const blockLevels = section.content().blocklevels();
for (const bl of blockLevels) { if (bl.type === 'paragraph') { const text = bl.asTextView().getPlainText(); allText.push(text); } else if (bl.type === 'table') { // Extract text from all cells in the table for (const row of bl.rows()) { for (const cell of row.cells()) { const cellBlocks = cell.blocklevels(); for (const cellBl of cellBlocks) { if (cellBl.type === 'paragraph') { const text = cellBl.asTextView().getPlainText(); allText.push(text); } } } } }}
console.log('All text:', allText.join('\n'));Replace text across all block-level elements: