How to Automatically Add a Timestamp in Google Sheets using Google Apps Script

Tracking exactly when data is entered into a spreadsheet is vital for inventory management, employee sign-in sheets, and task trackers. While you can manually type the date or use the Ctrl + ; keyboard shortcut, relying on human input guarantees eventual mistakes.

You cannot use a standard formula like =NOW() because it is volatile—it recalculates and changes every time the sheet is refreshed. To create a permanent, automatic timestamp the moment a specific cell is edited, you must use a small piece of custom code via Google Apps Script.

Step 1: Open the Apps Script Editor

Google Apps Script is a cloud-based JavaScript platform integrated directly into your Google Workspace.

  1. Open your Google Sheet.
  2. Click on Extensions in the top menu bar.
  3. Select Apps Script from the dropdown menu. A new browser tab will open showing a blank coding environment.

Step 2: Add the Custom Timestamp Code

In the Apps Script editor, you will see a default block of code that says function myFunction() {}. Delete everything in the window and replace it with the following script:

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var targetColumn = 1; // Watch Column A
  var timestampColumn = 2; // Write timestamp in Column B

  if (e.range.columnStart === targetColumn) {
    var cell = sheet.getRange(e.range.rowStart, timestampColumn);
    if (cell.getValue() === '') { 
      cell.setValue(new Date());
    }
  }
}

How the Code Works

This script uses a simple onEdit(e) trigger. Every single time any cell in the spreadsheet is modified, Google runs this code in the background.

  • targetColumn = 1; tells the script to only pay attention if the edit happens in Column A (the 1st column).
  • timestampColumn = 2; tells the script to write the date into Column B (the 2nd column) of the same row.
  • if (cell.getValue() === '') ensures that the script only adds a timestamp if the destination cell is currently empty, preventing it from overwriting the original time if you edit Column A again later.

Step 3: Save and Test

  1. Click the Save icon (the floppy disk) in the top toolbar of the Apps Script editor.
  2. Close the Apps Script browser tab and return to your Google Sheet.
  3. Type any value into an empty cell in Column A and press Enter.
  4. Almost instantly, the current date and time will magically appear in the adjacent cell in Column B.

Because the timestamp is generated by JavaScript and pasted as a static value, it will never accidentally refresh or change like a standard formula would, giving you a perfect, permanent audit trail.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.