function resetFontStyles() {
// Open the active Google Docs document
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Iterate through all the elements in the body
resetElementStyles(body);
Logger.log("Font styles reset to default.");
}
function resetElementStyles(element) {
const elementType = element.getType();
// Check the type of element
switch (elementType) {
case DocumentApp.ElementType.PARAGRAPH:
case DocumentApp.ElementType.TEXT:
// Reset the styles for text elements
element.editAsText().setFontSize(null).setForegroundColor(null).setBold(false).setItalic(false).setUnderline(false).setBackgroundColor(null);
break;
case DocumentApp.ElementType.LIST_ITEM:
// Reset the styles for list items
element.asListItem().editAsText().setFontSize(null).setForegroundColor(null).setBold(false).setItalic(false).setUnderline(false).setBackgroundColor(null);
break;
case DocumentApp.ElementType.TABLE:
// Reset the styles for table elements
const table = element.asTable();
for (let r = 0; r < table.getNumRows(); r++) {
const row = table.getRow(r);
for (let c = 0; c < row.getNumCells(); c++) {
resetElementStyles(row.getCell(c));
}
}
break;
case DocumentApp.ElementType.TABLE_CELL:
// Reset the styles for table cell content
const cell = element.asTableCell();
const cellElements = cell.getNumChildren();
for (let i = 0; i < cellElements; i++) {
resetElementStyles(cell.getChild(i));
}
break;
default:
// Handle other element types, if necessary
if (element.getNumChildren) {
for (let i = 0; i < element.getNumChildren(); i++) {
resetElementStyles(element.getChild(i));
}
}
}
}
How It Works:
resetFontStyles
: This is the main function that you can run. It resets all text elements’ font sizes, colors, and styles to default in the active Google Doc.resetElementStyles
: A recursive helper function that handles the nested structure of a Google Docs document, including paragraphs, text, tables, and table cells.- Defaults:
- Font size:
null
(resets to default size based on the document’s theme). - Font color:
null
(resets to black). - Style attributes (bold, italic, underline, background color): reset to default (off or none).
- Font size:
How to Use:
- Open a Google Docs document.
- Go to Extensions > Apps Script.
- Paste the script above into the editor and save the project.
- Close the Apps Script editor and reload your Google Doc.
- Go to Extensions > Apps Script > Reset Font Styles to run the script.
This script will reset all font styles in the document to default values. Let me know if you’d like any additional features!