Update revision date with Google App Script

I was bitching about what Google Documents is missing for technical documents. Which, in all honestly, is not much, especially, in the light of the revision and collaboration tools it does have -- and had on its first day.

Since I like to have revision dates and numbers in my documents how hard could it be to script their automatic update. Not hard at all. The following script will add an "Update Revision" menu item to a "Scripts" menu and when invoked it will replace all occurances of "Revision YYYY-MM-DD HH:MM" in headers, footers, and the body with the current timestamp.

function onOpen() {
      .addItem('Update Revision', 'updateRevisionTimestamp')

function updateRevisionTimestamp() {
  var doc = DocumentApp.getActiveDocument();
  var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm");
  var revisionPattern = "Revision [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}";
  var revisionReplacement = "Revision " + date;
  var body = doc.getBody();
  if ( body ) {
    body.replaceText(revisionPattern, revisionReplacement);
  var footer = doc.getFooter();
  if ( footer ) {
    footer.replaceText(revisionPattern, revisionReplacement);
  var header = doc.getHeader();
  if ( header ) {
    header.replaceText(revisionPattern, revisionReplacement);
  return doc;  

1 comment:

Andrew Gilmartin said...

I next want to write a script that adds purple numbers.