Thursday, November 20, 2014

Add date and time to a Google spreadsheet document with one click

There is a keyboard shortcut to add the current date to a cell in a Google spreadsheet. But it will not enter the current time. Here's my solution. It adds a simple menu item in your spreadsheet that enables you to enter the current date and time with one click.

  1. Open the Tools menu and choose Script Editor
  2. Copy and paste the following text:
  3. function onOpen() {
      var spreadsheet = SpreadsheetApp.getActive();
      var menuItems = [
        {name: 'Add date and time', functionName: 'addDateTime'}
      ];
      spreadsheet.addMenu('AddDateTime', menuItems);
    }
    
    function addDateTime() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];
      
      var cell = ss.getActiveCell().setValue(new Date());
    } 
  4. Click the save button to save your script, then reload your document for the changes to take effect.
  5. Select the row or the cell in your spreadsheet where you want to add your data. From the Format menu, choose Number and then choose Date/Time. If you omit this step, it will only show the date without the time.
  6. Now select the cell where you want to add the current date. In the menu bar, look for a menu on the right that says AddDateTime. Click Add date and time to add the current date and time.

No comments:

Post a Comment