Created
December 2, 2019 13:48
-
-
Save picxenk/69beded605fc786638643536343db32e to your computer and use it in GitHub Desktop.
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
// ref : https://stackoverflow.com/questions/13585272/insert-date-time-in-google-document | |
function onOpen() { | |
var ui = DocumentApp.getUi(); | |
ui.createMenu('Custom Menu') | |
.addItem('Insert Date', 'insertDate') | |
.addToUi(); | |
} | |
function insertDate() { | |
var cursor = DocumentApp.getActiveDocument().getCursor(); | |
if (cursor) { | |
var d = new Date(); | |
var dd = d.getDate(); | |
dd = pad(dd, 2) | |
var mm = d.getMonth() + 1; //Months are zero based | |
mm = pad(mm, 2) | |
var yyyy = d.getFullYear(); | |
var h = d.getHours(); | |
var m = d.getMinutes(); | |
var date = yyyy + "-" + mm + "-" + dd + " " + h + ":" + m; | |
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.'); | |
} | |
} | |
function pad (str, max) { | |
str = str.toString(); | |
return str.length < max ? pad("0" + str, max) : str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment