简体   繁体   中英

How to time stamp when a cell was updated in Google Sheets using scripts?

I want a cell in column B to timestamp when a cell column A is edited/updated

I have this function but unfortunately it updates every time the spreadsheet reloads

function getUpdate() {
  return new Date();
}

Is there a way I can timestamp it with the date when the cell is updated, but not when it reloads? Many thanks

This does the job. Notice I've slightly modified it to enter a timestamp in column J, if input was recorded in column 6, 7, 8, 9

Is there a range function in Javascript like in Python?

Instead of using || is there something like range(6, 10) ?

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Projects" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 6 || r.getColumn() == 7 || r.getColumn() ==  8 || r.getColumn() == 9 ) { //checks the column

      // var nextCell = r.offset(0,2);

      var nextCell = SpreadsheetApp.getActiveSheet().getRange('J' + r.getRow());


      // if( nextCell.getValue() === '' ) //is empty?
      nextCell.setValue(new Date());
    }
  }
}
function onEdit(e) {
  var s = e.range.getSheet();
  if( s.getName() == "Sheet1" && e.range.columnStart == 1 ) {           
     e.range.offset(0,2).setValue(new Date());
  }
}

Regarding your proposed solution and what are you asking there, you can use the method includes :

var range = [6,7,8,9]
range.includes(col)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM