The Toybox

people for the conservation of limited amounts of indignation


Previous Entry Share Next Entry
documents in googledocs is almost entering the 21 century
children of dune - leto 1
seperis
If you use Googledocs for fic, you may already know that they've added javascript functionality so you can create your own scripts for stuff that they don't have in the menus or stuff that you want to do that takes several different menu options (or a select all). You can find the manager and editor under the Tools menu.

There are some odd restrictions with their currently available classes, but below are two simple functions you can add to the custom menu dropdown that you can add to your toolbar (which trust me, was the highlight of my googledocs experience). One controls fixing your entire document's font and size (you can add any of the attributes listed here for the style), and the other fixes a selection of text.

This is a work in progress. These do not run very quickly yet, but I timed how long it takes me to do it manually and it is faster. I assume googledocs will eventually speed this up; their excel scripts run extremely fast, but no matter how much I optimize the code here, it's still noticeable in its lag.



Notes: This formats the entire document, including headers, footers, table of contents. To only do the body, after getActiveDocument, add getBody, with the appropriate open/closed parentheses.


/**
 * Fix font size and font type for entire document.
*/
function fixDocumentFontAndSize() {
  // set style
  var style = {};
  style[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.ARIAL;
  style[DocumentApp.Attribute.FONT_SIZE] = 11;
  
  // format document
  DocumentApp.getActiveDocument().editAsText().setAttributes(style);

  // To format the body only
  //DocumentApp.getActiveDocument().getBody().editAsText().setAttributes(style);

}



Notes: this covers only what you have physically selected (or hit Select All).


/**
 * Fix font size and font type for selection
*/
function fixSelectionFontAndSize() {
   // variables
   var selection = DocumentApp.getActiveDocument().getSelection();
  
  // Check if there's anything selected
  if (!selection) {
    DocumentApp.getUi().alert('Cannot find a selection in the document.');
    return;
  }
  
  // set style
  var style = {};
  style[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.ARIAL;
  style[DocumentApp.Attribute.FONT_SIZE] = 11;
  
  // create array variable
  var elements=selection.getSelectedElements();
  
  // loop through all elements, formatting only the ones that are text
  for (var i = 0; i < elements.length; ++i) {
    var text = elements[i].getElement().editAsText().setAttributes(style);
  }
}





Slowly but surely, googledocs is adding more classes as they go, so hopefully there will be more flexibility soon. It does recognize the most recent version of javascript that I could find and test with, and I haven't found any that doesn't work.

However, the classes available can be confusing in what they do and don't allow, one, and two, they occasionally have secret methods and properties that don't show up in the API or in the autocomplete. I have no idea why, but it's kind of like an easter egg when it happens. Such as, in the first script, in the last line, editAsText does not actually appear as a potential method on the document object, but as it worked, I used it to add some (very small) speed to execution.

Any suggestions or hey, code of your own! Would love to hear about it.

Posted at Dreamwidth: http://seperis.dreamwidth.org/984817.html. | You can reply here or there. | comment count unavailable comments

  • 1
(Deleted comment)
(Deleted comment)
Multiple browsers, one for fandom, one for mundane life.


  • 1
?

Log in

No account? Create an account