Skip to content

Instantly share code, notes, and snippets.

@BobToninho
Created June 29, 2023 09:40
Show Gist options
  • Save BobToninho/7345762d9d2357b3406886b70e21849d to your computer and use it in GitHub Desktop.
Save BobToninho/7345762d9d2357b3406886b70e21849d to your computer and use it in GitHub Desktop.
Google Docs: Replace paragraphs' fonts with a new font
const IS_DRY_RUN = false
const FONTS_TO_REPLACE = ['Arial', 'Times New Roman']
const REPLACING_FONT = 'Helvetica Neue'
function convertFonts() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();
// Define a custom paragraph style.
const style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = REPLACING_FONT;
let replacedParagraphs = 0
for (var i = 0; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
const attributes = paragraph.getAttributes()
if (FONTS_TO_REPLACE.includes(attributes[DocumentApp.Attribute.FONT_FAMILY])) {
if (IS_DRY_RUN) {
Logger.log('Found paragraph: ' + paragraph.getText())
} else {
paragraph.setAttributes(style)
}
replacedParagraphs++
}
}
if (IS_DRY_RUN) {
Logger.log(`${replacedParagraphs} paragraphs would be replaced`)
} else {
Logger.log(`Replaced ${replacedParagraphs} paragraphs!`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment