Maintaining bullet point formatting when extracting text with getText() in Google Apps Script

I’m working on a script that copies content from one Google Doc to another. The source document has text organized with bullet points, but when I use the getText() method, all the formatting disappears.

var sourceDoc = DocumentApp.openById("[document_id]");
var extractedContent = sourceDoc.getBody().getText();
documentBody.replaceText("{{PlaceholderText}}", extractedContent);

The bullet point structure gets lost completely in the target document. What’s the best approach to keep the original formatting intact? Should I be using a different method instead of getText(), or is there another way to handle this without having to manually process each element?

getText() flattens everything to plain text - that’s your problem. Hit this same issue building a template system for my team’s reports. I used copyToBlob() and insertPageBreak() methods, but that’s probably overkill for what you need. Easier approach: loop through document elements with getNumChildren() and getChild(index), then check each element’s type using getType(). When you find a list item, grab getListId() and getNestingLevel() to rebuild the bullet structure in your target doc. Key thing is keeping the list properties intact during transfer instead of converting to plain text first.

Indeed, the getText() method is not ideal for preserving formatting. I encountered similar issues while creating a document migration script. Instead of relying on getText(), it’s more effective to work with the document’s structure directly. Use getBody().getChild() to access each element individually, ensuring you maintain formatting. For bullet points, determine if a paragraph belongs to a list by checking its attributes with getAttributes(). This approach requires more code, but it allows you to retain all formatting, including indentation and bullet styles.

agreed! getText() totally strips formatting. u shud try appendText() or insertText() to take each paragraph one by one. looping through them helps keep bullet points and everything. good luck!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.