Created
December 11, 2020 19:29
-
-
Save cougrimes/2119358ccc7d169dd9df0acde8f9d32d to your computer and use it in GitHub Desktop.
A Google Apps script for creating custom monospace styles in documents. Pair with a macro editor to use as a keyboard shortcut!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Welcome to monospace.gs, a script that handles standardizing formatting for items | |
needing monospace formatting in documents. Note that if you are wanting to share | |
large code blocks, you likely should install the following add-on: | |
https://workspace.google.com/marketplace/app/code_blocks/100740430168 | |
This plugin is meant more for psuedocode and inline markup of code elements in | |
documentation. Use https://www.autocontrol.app/ to assign a keyboard shortcut for | |
the script's behavior. Enjoy! */ | |
// Add new menu item | |
function onOpen() { | |
DocumentApp.getUi() | |
.createMenu('Styles') | |
.addItem('Monospace', 'formatMonospace') | |
.addToUi(); | |
} | |
// Define monospace styling | |
var style = {}; | |
/* If you have preferences for other fixed width fonts, feel free to comment/uncomment; | |
these are simply your default options inside Google Docs for monospaced fonts. */ | |
// style[DocumentApp.Attribute.FONT_FAMILY] = "Consolas"; | |
// style[DocumentApp.Attribute.FONT_FAMILY] = '"Courier New"; | |
// style[DocumentApp.Attribute.FONT_FAMILY] = "Roboto Mono"; | |
style[DocumentApp.Attribute.FONT_FAMILY] = "Droid Sans Mono"; | |
// Recommend setting font size one size down from your standard body copy | |
style[DocumentApp.Attribute.FONT_SIZE] = 10; | |
// Slack style value: #F7F7F9 | |
style[DocumentApp.Attribute.BACKGROUND_COLOR] = "#F0F0F0"; | |
// Slack style value: #E01E61 | |
style[DocumentApp.Attribute.FOREGROUND_COLOR] = "#000000"; | |
// If you want Slack-esque borders around your code box. Default Slack color and thickness preset. | |
// style[DocumentApp.Attribute.BORDER_COLOR] = "#E2E1E9"; | |
// style[DocumentApp.Attribute.BORDER_WIDTH] = "1.0"; | |
style[DocumentApp.Attribute.BOLD] = false; | |
// Apply monospace formatting | |
function formatMonospace() { | |
var selection = DocumentApp.getActiveDocument().getSelection(); | |
if (selection) { | |
var elements = selection.getRangeElements(); | |
// Loop through selected elements to apply styling | |
for (var i = 0; i < elements.length; i++) { | |
var element = elements[i]; | |
// Only modify elements that can be edited as text; skip images and other non-text elements. | |
if (element.getElement().editAsText) { | |
var text = element.getElement().editAsText(); | |
// Style the selected part of the element, or the full element if it's completely selected. | |
if (element.isPartial()) { | |
text.setAttributes(element.getStartOffset(), element.getEndOffsetInclusive(), style); | |
} else { | |
text.setAttributes(style); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment