Last active
July 7, 2020 10:36
-
-
Save thepushkarp/24acca981e90b9550caec2b793750806 to your computer and use it in GitHub Desktop.
Custom Script to add current date in Google Docs
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
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