0

The script below works perfectly for auto-populating the date. I need it to do the exact same thing but for time. I can't for the life of me figure it out. It does need to populate +1 to the right of the date but that is an easy fix!

function onEdit() {
  // writes the current date to the cell to the right on the row when a cell in a specific column is edited
  // adjust the following variables to fit your needs

  var sheetNameToWatch = "Sheet1";
  var columnNumberToWatch = /* column A */ 3; // column A = 1, B = 2, etc.
  var valueToWatch="paid"; /* isn't doing anything */

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();
  var val=sheet.getActiveCell().getValue()

  if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val!= "" /* I changed this var value so if I input anything, it will update the date */) {
    var targetCell = sheet.getRange(range.getRow(), range.getColumn()-2 /* changed this to "-1" so it will input to the left column */
                               );
    targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT-07:00", "MM/dd/yyyy"));
  }
}
  • Welcome. Utilities.formatDate - you've used that to return the date, but you can also use it to return the time too. formatDate has three arguments - 'date', 'timezone' and 'format'. To display the time, use the same 'date' and 'timezone' but change the 'format'. 'Format' is a string per the SimpleDateFormat specification, so you could use "HH:mm" to return the hour and minute (for example). – Tedinoz Jan 4 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.