Many people are not aware that since the early days of the simple notepad app that comes bundled with Windows, it had the following undocumented feature: If you enter the text .LOG as the first line of the file, then every time you open the file with notepad, it will append the current date and time to the end of the document and scroll there. This is quite handy when you want a file that keeps track of the time when you added new entries.
I wanted to have the same functionality with Google Docs (with the added benefit of not needing to write .LOG at the beginning of the file). The following script (built via multiple shameless plagiarism from various sources) enables that functionality:
function onOpen() { var ui = DocumentApp.getUi(); // Or FormApp or SpreadsheetApp. ui.createMenu('Custom Menu') .addItem('Insert Date', 'insertDate') .addToUi(); setCursorToEnd() insertDate(); setCursorToEnd() } function setCursorToEnd() { var doc = DocumentApp.getActiveDocument(); var paragraph = doc.getBody().appendParagraph(''); var position = doc.newPosition(paragraph, 0); doc.setCursor(position); } function formatDate() { var date = new Date(); var datestr = date.getFullYear() + '-'+ ('0' + (date.getMonth()+1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2); var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? '0'+minutes : minutes; var strTime = hours + ':' + minutes + ' ' + ampm; return datestr + " " + strTime; } function insertDate() { var activeDoc = DocumentApp.getActiveDocument(); var cursor = activeDoc.getCursor(); if (cursor) { var date = formatDate(); var element = cursor.insertText(date); } else { DocumentApp.getUi().alert('Cannot find a cursor in the document.'); } }
To enable this for a document, In Google Docs : Tools -> Script Editor, enter and save the above script and then accept the permissions.