Skip to content

Instantly share code, notes, and snippets.

@defHLT
Last active February 22, 2021 10:59
Show Gist options
  • Save defHLT/11498254 to your computer and use it in GitHub Desktop.
Save defHLT/11498254 to your computer and use it in GitHub Desktop.
// 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;
}
@chrisbennell
Copy link

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