Javascript 从特定列编辑时的时间戳
我正在尝试使用谷歌表单来创建时间戳。我已经有了一个脚本,可以在每次编辑工作表时执行此操作。但是,我希望在更新特定列标题时应用时间戳 我在G列中有一个名为“STATUS”的列。我只希望只有在编辑该列时,它才会触发脚本在L列中记录一个时间戳,名为“LAST UPDATED” 我当前使用的脚本是:Javascript 从特定列编辑时的时间戳,javascript,Javascript,我正在尝试使用谷歌表单来创建时间戳。我已经有了一个脚本,可以在每次编辑工作表时执行此操作。但是,我希望在更新特定列标题时应用时间戳 我在G列中有一个名为“STATUS”的列。我只希望只有在编辑该列时,它才会触发脚本在L列中记录一个时间戳,名为“LAST UPDATED” 我当前使用的脚本是: function onEdit(event) { var timezone = "PST"; var timestamp_format = "MM/dd/yyyy hh:mm:ss a zzz";
function onEdit(event)
{
var timezone = "PST";
var timestamp_format = "MM/dd/yyyy hh:mm:ss a zzz";
var sheet = event.source.getActiveSheet();
// note: actRng = the cell being updated
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf('LAST UPDATED');
if (dateCol > -1 && index > 1) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
我也在这个论坛上找到了脚本,但是没有找到任何关于我上面问题的内容
任何帮助都将不胜感激
谢谢下面是我目前正在使用的脚本。我对JavaScript的了解非常有限,但它应该满足您的要求。但是,我仍然很难让IF返回null值正常工作。因此,请随时提供反馈 然后我将列的格式设置为包含日期/时间,但您可以轻松地适当设置脚本的格式
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0, -1);
if(s.getActiveCell() == null) { //is empty?
return nextCell.setValue(null) ;
}
else {
nextCell.setValue(new Date());
}
}
}