Google Docs API text replacement breaks bullet formatting and removes indentation

I’m having trouble with the Google Docs API when I try to update text inside bullet lists. Every time I use the text replacement function, it messes up the formatting of my bulleted items.

The main problem is that after replacing text, the bullet points and proper indentation get removed from the last item in the list. When there’s just one bullet point, it loses all formatting. With multiple bullets, only the final one gets messed up.

I’m using the replaceAllText method from the Google Docs API. Here’s my code example:

$messageContent = "Sample Text";
$updateRequests[] = new Google_Service_Docs_Request(array(
    'replaceAllText' => array(
        'containsText' => array(
            'text' => $messageContent,
            'matchCase' => 'true'
        ),
        'replaceText' => '[Completed] '.$messageContent
    )
));

Has anyone else run into this issue? The text gets replaced correctly but the list structure breaks every time. I need to keep the bullet formatting intact after the replacement.

yeah, i’ve hit this bug too. google docs api gets weird with formatted text replacements. use insertText with a specific index instead of replaceAllText - it keeps bullet structure way better. you’ll have to find the text position first, but it’s worth it to save your formatting.

Had the same problem a few months ago. The replaceAllText method messes with paragraph styling when it hits formatted text - it treats everything as plain text and strips the formatting that keeps bullets working.

Here’s what fixed it for me: ditch replaceAllText and work with the document structure instead. Find the paragraphs with your target text using the document content structure, then use deleteContentRange followed by insertText while preserving the styling. This keeps your original formatting intact, including bullets and indentation.

Also check if your replacement text is getting the right paragraph style. Sometimes the new text just doesn’t inherit the bullet list properties from the original paragraph.

This happens because replaceAllText works at the text level and doesn’t preserve document structure. When you replace text in bulleted paragraphs, the API loses the paragraph’s list properties. I worked around this using batchUpdate with insertText requests instead. First, find the specific paragraph with your text using the document’s content structure. Then delete the old text and insert new text while explicitly setting the paragraph style to keep list formatting. Another approach that worked for me was updating text in smaller chunks instead of replacing entire paragraphs. This stops the API from losing track of the list structure. You might also need to check if your document has custom list styles that aren’t being preserved during replacements.