目前在我的Gooogle工作表文档中有4个定期更新的单元格。每次更新时,我都想给一个单独的单元加时间戳。
我可以使附加的脚本在1个单元格中工作,但是当我复制脚本以使其在另一个单元格中工作时,它将停止较早版本的脚本。
function onEdit(event)
{
var timezone = "GMT+1";
var timestamp_format = "dd-MM-yyyy hh:mm:ss";
var updateColName = "Development Team Comments";
var timeStampColName = "To update select cell";
var sheet = event.source.getSheetByName('WIP');
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
我也尝试过此代码,但似乎不适用于后续单元:
function onEdit(event)
{
var timezone = "GMT+1";
var timestamp_format = "dd-MM-yyyy hh:mm:ss"; // Timestamp Format.
var updateColName = "Development Team Comments";
var timeStampColName = "To update select cell";
var sheet = event.source.getSheetByName('WIP'); //Name of the sheet where you want to run this script.
var responseArray = ["o update select cell", "Bupa time stamp","Date Stamp Move Live"];
var questionArray = ["Development Team Comments", "BDC proofing comments", "Approved Y"];
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // 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);
}
}
我希望我应该能够做的是能够更新任何单元格,并像上面的脚本一样为它们显示当前时间。我是否需要重复此操作,还是有一种在单元格引用中插入的方法。
答案 0 :(得分:1)
看起来您的脚本对于您实际需要的内容来说非常复杂,它会检查所有标头等,而实际上您仅需要基于setValue()
语句的几个if
并将其放在{ {1}}触发即可完成!
onEdit(e)
此脚本将在C列中起作用,如果要添加更多内容,只需调整function onEdit(e) {
var sheet = e.source.getSheetByName('WIP');
var timezone = 'GMT+1';
var format = 'dd-MM-yyyy hh:mm:ss';
var cell = e.range;
var row = cell.getRow();
var col = cell.getColumn();
var stamp = Utilities.formatDate(new Date(), timezone, format);
if (col === 1) { //if edited cell is in column A
sheet.getRange(row, 4).setValue(stamp); //set timestamp in column D
}
if (col === 2) { //if edited cell is in column B
sheet.getRange(row, 5).setValue(stamp); //set timestamp in column E
}
if (col === 3) { //if edited cell is in column C
sheet.getRange(row, 6).setValue(stamp); //set timestamp in column F
}
}
语句和if
中的数字即可满足您的要求。