Google Docs – Using Macros: Is It Possible?

google docsgoogle-apps-script

I want to be able to have some macros, while writing in a Google Document.

The idea is to replace the Quick Parts in Word – so I can have the same functionality in a Google Document.

Any ideas?

Best Answer

Introduction

Google lately added the Google Apps Script functionality in Google Documents and presented some basics on the Google Developers site.

One key feature is the sidebar. This is where the "Quick Parts" could reside. It needs however a bit of coding to do that. See the following example, made by Martin Hawksey: Sidebar

Update 27/07/2013, here's an example I wrote myself:

Code

// global
var app = DocumentApp.getUi();

function onOpen() {
  app.createMenu('Quick Parts')
    .addItem('Document Property', 'docProperty').addToUi();
}

function docProperty() {
  // set variables
  var doc = DocumentApp.getActiveDocument(); 
  var fileName = doc.getName(), Id = doc.getId();
  var file = DocsList.getFileById(Id), lastUpdated = file.getLastUpdated();
  var fileOwner = file.getOwner().getEmail();

  // arrays with label and result names
  var aNames = ['File name', 'File Id', 'File Owner', 'Last updated by'];
  var aResults = [fileName, Id, fileOwner, lastUpdated];

  // create Ui  
  var Ui = UiApp.createApplication().setTitle('Quick Parts').setWidth(450);
  var vPanel = Ui.createVerticalPanel().setId('vPanel').setSize(450, 100);  
  var fTable = Ui.createFlexTable()
    .setStyleAttribute('borderCollapse','collapse');

  // create labels
  for(var i=0, iLen=aNames.length; i<iLen; i++) {
    fTable.setWidget(i, 0, Ui.createLabel(aNames[i]));
    fTable.setWidget(i, 1, Ui.createLabel(aResults[i]));
  }

  // add to Ui
  vPanel.add(Ui.createLabel().setText("Document Property")
    .setStyleAttribute('font-size','175%'));
  vPanel.add(fTable);
  app.showSidebar(Ui.add(vPanel));
}

Screenshot

enter image description here