0

I found the following code in another post here to add a date after an edit but it places it in an offset column, I would like the date in Column 8 if column 3 or 4 has been edited. What do I change?

function onEdit(e) {
  if ([3,4].indexOf(e.range.columnStart) != -1) {
    e.range.offset(0, 1).setValue(new Date());
  }
}
0
  • then try e.range.offset(0, 7)

    function onEdit(e) {
     if ([3,4].indexOf(e.range.columnStart) != -1) {
     e.range.offset(0, 7).setValue(new Date());
     }
    }

  • to have dates only in one column try like this:

    function onEdit(e) {
    var s = SpreadsheetApp.getActiveSheet();
    if( s.getName() == "Sheet1" ) { 
    var r = s.getActiveCell();
    if( r.getColumn() == 3 ) { 
      var nextCell = r.offset(0, 7);
      var newDate = Utilities.formatDate(new Date(), 
      "GMT+8", "MM/dd/yyyy");
      nextCell.setValue(newDate);
    }
    if( r.getColumn() == 4 ) { 
      var nextCell = r.offset(0, 6);
      var newDate1 = Utilities.formatDate(new Date(), 
      "GMT+8", "MM/dd/yyyy");
      nextCell.setValue(newDate1);
    }}}

    • "Sheet1" = sheet name
    • r.getColumn() == 3 = column C / 3rd column
    • r.offset(0, 7) = offset timestamp 7 columns to the right on the same row eg column H
    • "GMT+8" = timezone
    • "MM/dd/yyyy hh:mm:ss" = date and time format
  • alternative (untested):

    function onEdit(e) { 
     if ([3].indexOf(e.range.columnStart) != -1) { 
     e.range.offset(0, 5).setValue(new Date()); } 
     if ([4].indexOf(e.range.columnStart) != -1) { 
     e.range.offset(0, 4).setValue(new Date()); } }

  • I have tried that, by using offset it offsets a change from the original column number so depending which column changes it puts the date in different columns, I want a single specific column for a change in either 3 or 4 – Fred S Jun 25 at 18:57
  • @FredS answer updated – user0 Jun 25 at 19:03
  • For some reason that didnt work for me, maybe due to the sheet name, but I did follow your example and came up with the following that does seem to work. – Fred S Jun 25 at 20:06
  • 1
    function onEdit(e) { if ([3].indexOf(e.range.columnStart) != -1) { e.range.offset(0, 5).setValue(new Date()); } if ([4].indexOf(e.range.columnStart) != -1) { e.range.offset(0, 4).setValue(new Date()); } } – Fred S Jun 25 at 20:17
  • @FredS glad to hear that. webapps.stackexchange.com/help/someone-answers – user0 Jun 25 at 21:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.