Skip to content

Instantly share code, notes, and snippets.

@dziban303
Forked from pbaruns/DateTime
Last active November 30, 2022 17:56
Show Gist options
  • Save dziban303/d478ec2fa0b5e099702f237f23b93fd7 to your computer and use it in GitHub Desktop.
Save dziban303/d478ec2fa0b5e099702f237f23b93fd7 to your computer and use it in GitHub Desktop.
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 GooDate', 'insertGooDate')
.addItem('Insert GooDateSig', 'insertGooDateSig')
.addItem('Insert TeKn0DaTe', 'insertTechno')
.addItem('Insert Date', 'insertAtCursor')
.addItem('Insert Time', 'insertTimeAtCursor')
.addItem('Insert Long Date', 'insertLDateAtCursor')
.addItem('Insert Date and Time', 'insertDTAtCursor')
.addItem('Insert Custom Long Date and Time', 'insertCLDTAtCursor')
.addItem('Insert METAR Date and Time', 'insertMETAR')
.addToUi();
}
/**
* Inserts the insertGooDate at the current cursor location in boldface.
*/
function insertGooDate() {
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 HH:mm 'UTC'"); // "yyyy-MM-dd HH:mm 'UTC'" -> 2019-05-17 14:00 UTC
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.');
}
moveCursorToEnd();
}
/**
* Inserts the insertGooDateSig at the current cursor location in boldface.
*/
function insertGooDateSig() {
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 HH:mm 'UTC ∿dz'"); // "yyyy-MM-dd HH:mm 'UTC ~dz'" -> 2019-05-17 14:00 UTC
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.');
}
moveCursorToEnd();
}
/**
* 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.');
}
moveCursorToEnd();
}
/**
* Inserts the TeKn0DaTe at the current cursor location in boldface.
*/
function insertTechno() {
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'T'HH:mm:ss'Z'"); // "yyyy-MM-dd'T'HH:mm:ss'Z'" -> 2020-07-20T06:27:32Z
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.');
}
moveCursorToEnd();
}
/**
* 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.');
}
moveCursorToEnd();
}
/**
* 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.');
}
moveCursorToEnd();
}
/**
* Inserts the Date and time at the current cursor location in boldface.
*/
function insertDTAtCursor() {
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 longDateTime = Utilities.formatDate(new Date(), "GMT", "MMMM dd, yyyy HH:mm"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(longDateTime);
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.');
}
moveCursorToEnd();
}
/**
* Inserts the custom long date and time at the current cursor location in boldface.
*/
function insertCLDTAtCursor() {
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 customLDateTime = Utilities.formatDate(new Date(), "GMT", "EEEE, dd MMMM, yyyy- HH:mm'hrs'"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(customLDateTime);
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.');
}
moveCursorToEnd();
}
/**
* Inserts the METAR at the current cursor location in boldface. It's terrible, don't use this.
*/
function insertMETAR() {
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", "ddHHmm'Z'"); // "yyyy-MM-dd" "ddHHmm'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.');
}
moveCursorToEnd();
}
function moveCursorToEnd() {
//move cursor to the end
var doc=DocumentApp.getActiveDocument();
var txtElement=doc.getCursor().getElement();
var txtOffset=doc.getCursor().getOffset();
var position=doc.newPosition(txtElement, txtOffset + 1);
doc.setCursor(position);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment