Generally I find Google Docs (part of the bizarrely rebranded G suite) fulfils 99% of my needs. But sometime there are stare-at-the-screen-and-work-out-where-something-is moments. Like trying to insert the current date into a document (for a legal agreement, where I wanted to track drafts printed). You can't do it in Google Docs! There is no default mechanism for doing this. Luckily I found this post: and this code seemed to do the trick: ——————-COPY BELOW THIS LINE————————— function onOpen() { // Add a menu with some items, some separators, and a sub-menu. DocumentApp.getUi().createMenu('Utilities') .addItem('Insert Date', 'insertAtCursor') .addToUi(); } /** * Inserts the date at the current cursor location in boldface. */ function insertAtCursor() { var cursor = DocumentApp.getActiveDocument().getCursor(); if (cursor) { // Attempt to insert text at the cursor position. If insertion returns null, // then the cursor's containing element doesn't allow text insertions. var date = Utilities.formatDate(new Date(), "GMT", "dd-MM-yyyy"); // "yyyy-MM-dd'T'HH:mm:ss'Z'" var element = cursor.insertText(date); if (element) { element.setBold(false); } else { DocumentApp.getUi().alert('Cannot insert text at this cursor location.'); } } else { DocumentApp.getUi().alert('Cannot find a cursor in the document.'); } } ——————-COPY ABOVE THIS LINE————————— Now, why would you not include a Date() function as standard? Think on ... |
Blogs >