Skip to content

Instantly share code, notes, and snippets.

@diyclassics
Created February 26, 2020 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyclassics/da86b11f6f76a969b3e7840eaf0f537d to your computer and use it in GitHub Desktop.
Save diyclassics/da86b11f6f76a969b3e7840eaf0f537d to your computer and use it in GitHub Desktop.
Google Docs add on to update a "field" on open
/**
* Written by diyclassics
*
* Looks for text in a Google Doc on open in the form "last updated 1/1/2001" and updates
* with the current date; also adds a menu item for manual update.
*/
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Scripts')
.addItem('Update Last Updated Date', 'updateLastUpdated')
.addToUi();
updateLastUpdated();
}
function getCurrentDate() {
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1;
var day = date.getDate(); if(day.toString().length==1){var day = day;}
return month + '.' + day + '.' + year;
}
function updateLastUpdated() {
var currentDate = getCurrentDate();
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText("last updated .+", 'last updated ' + currentDate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment