简体   繁体   中英

Google Apps Scripts Not Working As Intended

I have the below script I am trying to use to insert a timestamp when a value is input on a workbook I have. I have a few sheets in this workbook and it seems to only be working on one sheet. I'm not anywhere near a pro with Apps Scripts, so if someone can point me in the right direction I would appreciate it.

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Assignments" ) { 
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { 
var nextCell = r.offset(0, 3);
if( nextCell.getValue() === '' ) 
nextCell.setValue(new Date());
}
}
}

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "IMEI Swap" ) { 
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { 
var nextCell = r.offset(0, 3);
if( nextCell.getValue() === '' ) 
nextCell.setValue(new Date());
}
}
}

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Cancelations" ) { 
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { 
var nextCell = r.offset(0, 3);
if( nextCell.getValue() === '' ) 
nextCell.setValue(new Date());
}
}
}

Try it this way

function onEdit(e) {
  const sh = e.range.getSheet();
  const shts = ["Assignments","IMEI Swap","Cancellations"];
  if (~shts.indexOf(sh.getName()) && e.range.columnStart == 6 && e.range.offset(0, 3) === '') {
    e.range.offset(0, 3).setValue(new Date());
  }
}

Bitwise Not

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