HomeOur TeamContact

Change Google Docs Font Without Losing Formatting

By Vo Tu Duc
Published in Google Appsheet
February 20, 2025
4 min read
Change Google Docs Font Without Losing Formatting

This guide provides a Google Apps Script to quickly change automatically the font in your Google Docs while preserving headings, lists, bulleted list, and other formatting styles. Perfect for cleaning up merged documents or standardizing fonts after rebranding. No more manual reformatting headaches.

Change Google Docs Font Without Losing Formatting: A Google Apps Script Solution

Change Google Docs Font Without Losing Formatting
Change Google Docs Font Without Losing Formatting

We’ve all faced the frustrating task of wrangling fonts in Google Docs. Whether you’re dealing with a messy collaborative project, cleaning up after importing AI-generated content, merging documents from different sources, or even rebranding your entire company’s documentation, inconsistent fonts can be a major time sink. You painstakingly format headings, craft bulleted lists, and then bam - a single pasted paragraph throws everything off.

The typical solution? Manually select everything and change the font. But that’s a recipe for disaster! It wipes out your carefully crafted headings (H1, H2, H3!), mangles your bullet points, and generally throws your document into disarray. You end up spending more time fixing the formatting than you would have if you’d just started from scratch.

The Problem: Font Chaos and Formatting Frustration

  • Collaborative Mayhem: Multiple contributors with different font preferences lead to a visual jumble.

  • AI Integration: Copying and pasting text from AI tools often introduces unwanted font styles.

  • Legacy Documents: Old templates or documents from legacy systems bring outdated fonts into your workflow.

  • Rebranding Nightmares: Updating fonts across hundreds of documents after a rebranding can feel impossible.

  • The Time Drain: Manually reformatting documents is tedious, error-prone, and a massive waste of valuable time.

The Solution: A Google Apps Script to the Rescue

There’s a better way. A Google Apps Script can automate the process of changing fonts across your entire Google Doc, but here’s the key: it preserves your existing formatting! This script selectively applies the font change, leaving headings, lists, and other styled elements untouched.

Benefits of this approach:

  • Save Time: Automate the font change process, saving hours of manual work.

  • Maintain Consistency: Ensure a professional and consistent look across all your documents.

  • Preserve Formatting: Keep your headings, lists, and other styles intact.

  • Streamline Workflows: Simplify document merging, AI integration, rebranding, and more.

The Apps Script Code:

/**
* Changes the font of the entire Google Document from Arial to Google Sans,
* while preserving existing formatting (headings, lists, etc.).
*/
function changeFontToGoogleSans() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Regular expression to exclude common formatting elements (headings, lists, etc.)
const excludeRegex = /Heading\d|LIST_NUMBER|LIST_BULLET|LIST_CHECKBOX/;
// Function to process a single element and its children recursively
function processElement(element) {
const elementType = element.getType();
// Skip processing if the element type is excluded.
if (excludeRegex.test(elementType)) {
return; // Do nothing for headings and list items
}
// Check if the element has child elements
if (element.getNumChildren) {
const numChildren = element.getNumChildren();
for (let i = 0; i < numChildren; i++) {
const child = element.getChild(i);
processElement(child); // Recursive call
}
} else if (element.getText) { // element.getText() check is important here
// This is a leaf node (text element), so apply the font change.
try {
element.setFontFamily('Google Sans'); // Apply the font
// Handle other formatting that might be lost. This is critical.
// If you find other formatting being removed, add logic here to preserve it.
// Example (if you also want to preserve bolding):
// element.setBold(element.isBold());
// element.setItalic(element.isItalic()); // Preserve italics
// element.setUnderline(element.isUnderline());//Preserve underline
//element.setForegroundColor(element.getForegroundColor());
//element.setBackgroundColor(element.getBackgroundColor());
} catch (e) {
// Log errors for debugging. This is VERY important for complex documents.
Logger.log("Error processing element: " + e);
}
}
}
// Start processing from the document body
processElement(body);
Logger.log("Font change complete.");
}

How to Use the Script:

  1. Open your Google Doc.

  2. Go to “Tools” > “Script editor”.

  3. Copy and paste the code into the script editor.

  4. Save the script (e.g., as “FontChanger”).

  5. Run the changeFontToGoogleSans function. You’ll likely need to authorize the script to access your Google Docs.

  6. Check the Google Apps Script execution log (View > Logs) for any errors.

  7. Refresh your Google Doc to see the changes.

Explanation of the Code:

  • changeFontToGoogleSans(): The main function that orchestrates the entire process.

  • excludeRegex: This regular expression defines which element types to exclude from the font change. It specifically avoids headings (Heading1, Heading2, etc.) and list items (numbered lists, bulleted lists, and checkboxes), ensuring that your formatting is preserved.

  • processElement(element): This recursive function walks through the entire document structure, processing each element. Recursion allows the script to handle nested elements correctly.

  • element.getText: This is essential. The font change (setFontFamily) is applied only to text elements, preventing errors and ensuring that the script doesn’t try to change the font of non-text elements (like tables or images).

  • try…catch: This error handling block catches any unexpected errors during the process and logs them to the Apps Script execution log. This helps you troubleshoot any issues.

  • Preserving Formatting: This is the key. If other formats are also removed while you use this script, you can add lines to preserve the original formatting.

Example Scenarios

Rebranding

Imagine you’re rebranding your company and the new font is Google Sans. You have hundreds of Google Docs that need to be updated. Instead of manually changing the font in each document, you can use this script to update all the fonts in minutes, while keeping the headings and lists intact.

Merging Documents from Different Sources

Ever slammed two Google Docs together, hoping for a seamless merge, only to be greeted by a chaotic clash of fonts? Times New Roman warring with Calibri, Arial battling Comic Sans – your carefully crafted report suddenly resembles a ransom note from the digital age. Before you resign yourself to hours of tedious reformatting, imagine this: a single click, and the entire document adopts a consistent font, transforming the font anarchy into harmonious uniformity. And the best part? Your section headings, bullet points, and painstakingly formatted lists remain untouched, perfectly preserved in their original glory. This script is the peace treaty your merged documents desperately need.

Migrating from a Legacy System/Template

Picture this: you’re tasked with bringing dusty old documents created in a prehistoric word processor into the modern age of Google Docs. The fonts are outdated, unsupported, or simply clash with your brand’s sleek new aesthetic. Manually updating hundreds of documents? A task akin to rewriting the Library of Alexandria by hand. But what if you could wave a magic wand – or rather, execute a simple script – and instantly modernize these documents? This script is your time machine, swiftly updating the font to your preferred choice, while preserving the original structure, layout, and the blood, sweat, and tears that went into creating those documents in the first place. Less error-prone and infinitely faster, it’s the ultimate legacy document rescue mission

Cleaning up Messy Shared Documents

Collaborative projects: the breeding ground for font anarchy. Multiple contributors, each with their own unique (and often clashing) font sensibilities. The result? A Google Doc that looks like a stylistic battleground, where formatting wars rage with every keystroke. Imagine a ‘format reset’ button – a single click that sweeps away the font chaos, replacing it with clean, consistent elegance. That’s exactly what this script offers. It’s like Marie Kondo for your Google Docs, sparking joy by tidying up the fonts, all while respecting the hard work and careful structure invested by your team in the document’s content. Turn that font free-for-all into a masterpiece of collaboration

Conclusion

Stop wasting time manually reformatting Google Docs! This Google Apps Script provides a simple, effective, and automated solution for changing fonts while preserving your hard-earned formatting. Give it a try and reclaim your productivity. Share your experiences and any improvements you make to the script in the comments below!


Tags

Google DocsGoogle Apps ScriptFontFormattingAutomationProductivityDocument ManagementText StylingScriptingGoogle WorkspaceGoogle DriveWeb DevelopmentCode SnippetTutorial

Share

Previous Article
How to build a Custom Sentiment Analysis System for Operations Feedback Using Google Forms AppSheet and Vertex AI
Vo Tu Duc

Vo Tu Duc

A Google Developer Expert, Google Cloud Innovator

Table Of Contents

1
Change Google Docs Font Without Losing Formatting: A Google Apps Script Solution
2
The Solution: A Google Apps Script to the Rescue
3
Example Scenarios
4
Conclusion

Related Posts

How to build a Custom Sentiment Analysis System for Operations Feedback Using Google Forms AppSheet and Vertex AI
February 14, 2025
20 min
© 2025, All Rights Reserved.
Powered By

Quick Links

About UsContact Us

Social Media