Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Last active August 29, 2015 14:27
Show Gist options
  • Save bennettscience/c663c4653e209ecfd28a to your computer and use it in GitHub Desktop.
Save bennettscience/c663c4653e209ecfd28a to your computer and use it in GitHub Desktop.
// Add a custom menu item to the document when opened.
function onOpen() {
var ui = DocumentApp.getUi()
.createMenu('Set Fonts')
.addItem('Run','setFont')
.addToUi();
}
function setFont(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// Get the doc content
var elements = body.getParagraphs();
// Loop through the paragraphs
for(var i = 0; i < elements.length; i++) {
// Font and style arrays
var fonts = ['Open Sans','Josefin Slab','Arvo','Lato','Vollkorn','Abril Fatface','Ubuntu','PT Sans','PT Serif','Old Standard TT','Droid Sans'];
// Each word is grown to 48px font size
// Bold is set with a random boolean value generated for each word
var style = {};
style[DocumentApp.Attribute.FONT_SIZE] = '48';
style[DocumentApp.Attribute.BOLD] = !!Math.floor(Math.random() * 2);
// Choose a random font from the font array
var font = fonts[Math.floor(Math.random()*fonts.length)];
// Apply the font family and styles to all paragraphs in the document.
elements[i].editAsText().setFontFamily(font).setAttributes(style);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment