Last active
November 1, 2016 17:25
-
-
Save adamthebig/6a2fcd5e5b419009cce970a85506f229 to your computer and use it in GitHub Desktop.
Insert today's date into a Google doc, using a custom macro (Utilities/Insert Today's Date)
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
/** | |
* The onOpen function runs automatically when the Google Docs document is | |
* opened. Use it to add custom menus to Google Docs that allow the user to run | |
* custom scripts. For more information, please consult the following two | |
* resources. | |
* | |
* Extending Google Docs developer guide: | |
* https://developers.google.com/apps-script/guides/docs | |
* | |
* Document service reference documentation: | |
* https://developers.google.com/apps-script/reference/document/ | |
*/ | |
function onOpen() { | |
// Add a menu with some items, some separators, and a sub-menu. | |
DocumentApp.getUi().createMenu('Utilities') | |
.addItem('Insert Today\'s Date', 'insertDate') | |
.addToUi(); | |
} | |
/** | |
* Inserts the date at the current cursor location. | |
*/ | |
function insertDate() { | |
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. | |
// Available date formatting options: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
var date = Utilities.formatDate(new Date(), "EST", "EEEE, MMM d, YYYY"); // Tue, Nov 1, 2016 | |
var element = cursor.insertText(date); | |
if ( ! element) { | |
DocumentApp.getUi().alert('Cannot insert date at this cursor location. Please move cursor and try again.'); | |
} | |
} else { | |
DocumentApp.getUi().alert('Cannot find a cursor in the current document.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment