Created
February 26, 2020 18:09
-
-
Save diyclassics/da86b11f6f76a969b3e7840eaf0f537d to your computer and use it in GitHub Desktop.
Google Docs add on to update a "field" on open
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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