Document
Document:
{}Represents the root of a document structure in the programmatic API.
Remarks
Section titled “Remarks”Document is the top-level entry point for programmatically accessing and modifying
document content. It provides access to the document Body, which contains all
sections and their content.
You typically obtain a Document instance from a transaction context when using the
programmatic API to modify documents. The document is organized hierarchically:
DocumentFor document-wide operations, you can use the convenience replaceText method
to search and replace across all content without manually traversing the hierarchy.
Examples
Section titled “Examples”Access document structure and iterate through content:
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 blockLevels = content.blocklevels(); console.log(`Section has ${blockLevels.length} paragraphs/tables`);}Replace text across entire document:
// Replace all occurrences of "TODO" with "DONE" throughout the documentconst count = document.replaceText(/TODO/g, 'DONE');console.log(`Replaced ${count} occurrences across entire document`);Replace text with formatting across document:
// Highlight all instances of "IMPORTANT" in the entire documentdocument.replaceText(/IMPORTANT/gi, { text: 'IMPORTANT', formatting: { highlight: '#ffff00', bold: true }});Typical usage in a transaction:
await docAuthDoc.transaction(async (context) => { const { draft } = context;
const body = draft.body(); const sections = body.sections();
// Add a new section const newSection = body.addSection(); const para = newSection.content().addParagraph(); para.asTextView().setText('New section content');
// Or use document-wide replace draft.replaceText(/old/g, 'new');
return { commit: true };});Properties
Section titled “Properties”replaceText
Section titled “replaceText”replaceText:
ReplaceTextSignature
Replaces all occurrences of a pattern throughout the entire document.
Remarks
Section titled “Remarks”Convenience method that searches and replaces text across all document content,
including all sections, paragraphs, tables, headers, and footers. This is equivalent
to manually calling Body.replaceText on the document body, but provides a
simpler API for document-wide operations.
See ReplaceTextSignature for detailed parameter and usage information.
Examples
Section titled “Examples”Simple document-wide replacement:
const count = document.replaceText('old text', 'new text');console.log(`Replaced ${count} occurrences`);Replace with formatting:
document.replaceText(/ERROR/g, { text: 'ERROR', formatting: { color: '#ff0000', bold: true }});Dynamic replacement with callback:
// Convert all numbers to currency formatdocument.replaceText(/\$?(\d+(\.\d{2})?)/g, (match) => { const amount = parseFloat(match.replace('$', '')); return { text: `$${amount.toFixed(2)}`, formatting: { color: '#00aa00' } };});Methods
Section titled “Methods”body()
Section titled “body()”body():
Body
Gets the document body containing all sections.
Returns
Section titled “Returns”The Body of the document
Remarks
Section titled “Remarks”Returns the document’s body, which contains all sections. Each section has its own content (paragraphs and tables) and headers/footers. Use this as the entry point for navigating and modifying document structure.
Example
Section titled “Example”const body = document.body();const sections = body.sections();
// Work with sectionsfor (const section of sections) { const content = section.content(); // ... work with content}