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 owns the flat block-level content (paragraphs and
tables) plus section-property handles delimited by section breaks.
You typically obtain a Document instance from a transaction context when
using the programmatic API to modify documents. The document is organized
hierarchically:
DocumentTo navigate from a block back to its containing section, call
Paragraph.findSection or Table.findSection.
For 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();console.log(`Document has ${body.sections().length} sections`);
for (const block of body.content().blocklevels()) { const section = block.findSection(); console.log(block.type, section?.pageSetup().pageSize());}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();
// Append a paragraph and start a new section after it const para = body.content().addParagraph(); para.asTextView().setText('New section content'); para.addSectionBreak();
// 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.
Returns
Section titled “Returns”The Body of the document
Remarks
Section titled “Remarks”Returns the document’s body, which owns the flat block-level content (paragraphs and tables) plus the section-property handles that apply to ranges within them.
Example
Section titled “Example”const body = document.body();for (const block of body.content().blocklevels()) { if (block.type === 'paragraph') { console.log(block.asTextView().getPlainText()); }}commentThreads()
Section titled “commentThreads()”commentThreads():
CommentThreadCollection
Gets the document-root comment thread collection.
Returns
Section titled “Returns”A CommentThreadCollection for querying and creating comment threads
Remarks
Section titled “Remarks”Use this as the entry point for programmatic comment workflows. The API exposes comment threads from the document root only, even when the thread is anchored to text inside a paragraph, header, footer, or table cell.
The collection works with comment threads rather than raw comment records. Query results include the top-level comment, flat replies, and an inferred text anchor reconstructed from document content.
Example
Section titled “Example”const threads = draft.commentThreads().all({ status: 'open' });
for (const thread of threads) { console.log(thread.comment.text);}