I’m working on a script to copy text from one Google Doc to another. The source doc has bulleted lists, but when I use getText(), it strips all formatting. Here’s what I’ve got so far:
This copies the text, but it comes out as plain text without bullets. Is there a way to maintain the original formatting? I’d rather not manually recreate the bullet points if possible. Any suggestions on how to keep the list structure intact when moving text between documents?
I’ve encountered this problem before, and there’s a more efficient solution than copying the entire body. You can use the getListItems() method to specifically target and maintain bullet points. Here’s an approach that worked for me:
This method preserves the bullet points and their nesting levels. It’s more precise than copying the entire body, especially if you only need to transfer specific list elements. Remember to handle non-list content separately if needed.
I ran into a similar issue when working on a document automation project. Instead of using getText(), which indeed strips formatting, try using the copyTo() method. This approach preserves most formatting, including bullet points.
This copies the entire body content, maintaining structure and formatting. If you need more granular control, you can iterate through the source document’s children and copy specific elements. Do note that complex formatting, such as tables, might not transfer perfectly and may require additional handling. However, for basic formatting and bullet points, this method should serve your needs well.