Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // is called by google docs when a document is open
- // adds a menu with a menu item that applies a style to the currently selected text
- function onOpen() {
- DocumentApp.getUi()
- .createMenu('Extras')
- .addItem('Apply code style', 'applyCodeStyle')
- .addToUi();
- }
- // definition of a style to be applied
- var style = {
- bold: false,
- backgroundColor: "#DDDDDD",
- fontFamily: DocumentApp.FontFamily.CONSOLAS
- };
- // helper function that strips the selected element and passes it to a handler
- function withElement(processPartial, processFull) {
- var selection = DocumentApp.getActiveDocument().getSelection();
- if (selection) {
- var elements = selection.getSelectedElements();
- for (var i = 0; i < elements.length; i++) {
- var element = elements[i];
- if (element.getElement().editAsText) {
- var text = element.getElement();
- if (element.isPartial()) {
- var from = element.getStartOffset();
- var to = element.getEndOffsetInclusive();
- return processPartial(element, text, from, to);
- } else {
- return processFull(element, text);
- }
- }
- }
- }
- }
- // called in response to the click on a menu item
- function applyCodeStyle() {
- return withElement(
- applyPartialStyle.bind(this, style),
- applyFullStyle.bind(this, style)
- );
- }
- // applies the style to a selected text range
- function applyPartialStyle(style, element, text, from, to) {
- text.setFontFamily(from, to, style.fontFamily);
- text.setBackgroundColor(from, to, style.backgroundColor);
- text.setBold(from, to, style.bold);
- }
- // applies the style if the entire element is selected
- function applyFullStyle(style, element, text) {
- text.setFontFamily(style.fontFamily);
- text.setBackgroundColor(style.backgroundColor);
- text.setBold(style.bold);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement