Skip to content

Instantly share code, notes, and snippets.

@Eccenux
Created November 21, 2016 11:20
Show Gist options
  • Save Eccenux/712ae3d7913e971e46035546b2ccc85b to your computer and use it in GitHub Desktop.
Save Eccenux/712ae3d7913e971e46035546b2ccc85b to your computer and use it in GitHub Desktop.
InsertDate Spreadsheet extenstion
/**
* The onOpen function runs automatically when the Google Docs document is
* opened.
*
* More on `onOpen` and other trigger functions: https://developers.google.com/apps-script/guides/triggers/
* More on `SpreadsheetApp` class: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
*/
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
SpreadsheetApp.getUi().createMenu('Utilities')
.addItem('Insert Date', 'insertInCell')
.addToUi();
}
/**
* Inserts date into active cell.
*/
function insertInCell() {
var cell = SpreadsheetApp.getActive().getActiveSheet().getActiveCell();
if (cell) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
cell.setValue(date);
} else {
SpreadsheetApp.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