Instantly share code, notes, and snippets.

Embed
What would you like to do?
Insert Date Time
/**
* 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 Date', 'insertAtCursor')
.addItem('Insert Time', 'insertTimeAtCursor')
.addItem('Insert Long Date', 'insertLDateAtCursor')
.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", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
/**
* Inserts the time at the current cursor location in boldface.
*/
function insertTimeAtCursor() {
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 time = Utilities.formatDate(new Date(), "GMT", "HH:mm:ss"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(time);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
/**
* Inserts the long date at the current cursor location in boldface.
*/
function insertLDateAtCursor() {
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 longDate = Utilities.formatDate(new Date(), "GMT", "MMMM dd, yyyy"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(longDate);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
@pbarunsarathy

This comment has been minimized.

Copy link
Owner

pbarunsarathy commented Dec 16, 2018

Added 'time' and 'long date' formats.
Change the timezone as per your needs from 'GMT' to your time zone abbr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment