javascriptgoogle-sheetsdatestamp

Date stamp for google-spreadsheet


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
        }
    }
}

Solution

  • 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..