Google Docs API text replacement breaks bullet formatting and removes list indentation

I’m having trouble with the Google Docs API when I try to update text inside bullet lists. Every time I use the replace text function, something weird happens to the formatting. The bullet points get messed up and the indentation disappears from the final item in my list.

I’m using the Google_Service_Docs_ReplaceAllTextRequest class to handle the text replacement. Here’s what my PHP code looks like:

$originalText = "Task Complete";
$updateRequests[] = new Google_Service_Docs_Request(array(
    'replaceAllText' => array(
        'containsText' => array(
            'text' => $originalText,
            'matchCase' => 'true'
        ),
        'replaceText' => '[FINISHED] '.$originalText
    )
));

The strange thing is that when my bullet list has just one item, that single item loses its bullet and spacing completely. But if there are several items in the list, only the bottom one gets broken while the others stay normal. Has anyone else run into this formatting issue when working with bulleted lists through the API?

yeah, this is an annoying bug in the docs api. the bullet formatting gets stripped when your replacement text changes the character count by a lot. try using insertText at specific positions instead of replaceAllText - it keeps list properties intact better. also, double-check you’re not including paragraph markers in your search text by mistake.

Hit the same issue with document automation. The API treats text nodes and formatting nodes differently - when you replace text that crosses paragraph boundaries or lists, it thinks you’re inserting new text instead of keeping the existing structure. Fixed it by switching from one big replaceAllText call to batchUpdate with smaller operations. I’d map out the exact text ranges using the document structure first, then run targeted insertText and deleteContentRange operations one by one. This keeps paragraph properties and list formatting intact. Watch out for newlines or special characters in your replacement text - wrap them properly or the API reads them as paragraph breaks. Also check if your original text sits right at paragraph boundaries since that’s where most formatting breaks happen.

This happens because Google Docs API treats list formatting and text content as completely separate things. When you use replaceAllText, the API loses track of the paragraph’s list properties - especially on the last list item, which has weird boundary conditions. I hit this exact bug on a project last year. What helped was checking the paragraph’s list properties before changing any text. It gets worse when your replacement text is a different length than the original since that shifts the whole document structure around. Here’s what worked for me: First, grab the document structure with documents.get and figure out which paragraphs are actually lists. Then do targeted replacements instead of replaceAllText on everything. Use range-based replacements that keep the list formatting intact. The trick is keeping those paragraph boundaries and list nesting levels stable while you’re doing the replacements.