0

I need some help adding a feature to this code. So far if C 2 is modified it adds a date stamp on D 2 but I would like the date stamp to clear when I clear the content on C 2 and I have not been able to successfully achieve this.

Any help would be highly appreciated.

Thanks.

function onEdit() {
    var s = SpreadsheetApp.getActiveSheet();
    if( s.getName() == "Add Payroll" ) {
        //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if( r.getColumn() == 3 ) {
            //use getRow for row and getColumn
            for column
                var nextCell = r.offset(0, 1);
                //offset (row,column)
                if( nextCell.getValue() === "" )
                    //is empty?
                    nextCell.setValue(new Date());
                    //will only put date, format "123/Date and time" if time needed
        }
    }
}
Nima Soroush
  • 12,242
  • 4
  • 52
  • 53

1 Answers1

0

Can you see if this code works for you ?

function onEdit(e) {
if (e.source.getActiveSheet()
    .getName() !== "Add Payroll" || e.range.columnStart !== 3 || e.range.rowStart === 1) return;
e.range.offset(0, 1)
    .setValue(e.value ? new Date() : null)

}

Note: I also took out the editing of row 1 because I thought you may have headers in there..

JPV
  • 26,499
  • 4
  • 33
  • 48
  • Thank you for the help! I added the code but it is giving me an error "TypeError: Cannot read property "source" from undefined. (line 2, file "Code")Dismiss" – Jhonathan Morales Nov 20 '14 at 22:43
  • Did you try to run the code from the script editor ? In that case the error would be normal, since then there's no event taking place in the spreadsheet. Try using the script by simply editing the spreadsheet in the range you provided and see if the timestamps appear. – JPV Nov 21 '14 at 11:21