Last active
February 22, 2021 10:59
-
-
Save defHLT/11498254 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Insert current date in Google Spreadsheet | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
// Or FormApp or SpreadsheetApp. | |
ui.createMenu('Custom Menu') | |
.addItem('Insert Date', 'insertDate') | |
.addToUi(); | |
} | |
function insertDate() { | |
var cell = SpreadsheetApp.getActive().getActiveCell(); | |
if (cell) { | |
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 date = dd + "-" + mm + "-" + yyyy; | |
var element = cell.setValue(date); | |
} else { | |
SpreadsheetApp.getUi().alert('Cannot get active cell.'); | |
} | |
} | |
function pad (str, max) { | |
str = str.toString(); | |
return str.length < max ? pad("0" + str, max) : str; | |
} |
I forked this and created a simpler version that uses the built in date formatting:
https://gist.github.com/chrisbennell/65f9fb82f677c3a5c436c9535b268dbd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
complete novice to scripting... when I run this script I get an error for the pad function:
TypeError: Cannot call method "toString" of undefined. (line 28, file "Code")D