Skip to content

Instantly share code, notes, and snippets.

@thepushkarp
Last active July 7, 2020 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepushkarp/24acca981e90b9550caec2b793750806 to your computer and use it in GitHub Desktop.
Save thepushkarp/24acca981e90b9550caec2b793750806 to your computer and use it in GitHub Desktop.
Custom Script to add current date in Google Docs
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Custom Scripts')
.addItem('Insert Date', 'insertDate')
.addToUi();
}
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.
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth();
var yyyy = d.getFullYear();
var date = monthNames[mm] + " " + dd + ", " + yyyy;
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.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment