我正在尝试自动填充一个单元格的时间(工作表“ leads”中的单元格B2)
但是似乎无法成功运行脚本。给我错误"TypeError: Cannot read property "source" from undefined. (line 6, file "Code"
function onEdit(event) {
var timezone = "GMT-2";
var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.
var updateColName = "Time";
var sheet = event.source.getSheetByName('Leads'); //Name of the sheet where you want to run this script.
var responseArray = ["Time"];
var questionArray = ["Time"];
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRow();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); //get values and place them inside array
var dateCol = headers[0].indexOf(timeStampColName); //get index position inside the array
for (var i = 0; i < questionArray.length; i++) {
if (headers[0].indexOf(responseArray[1]) > -1 && index > 1 && editColumn == (headers[0].indexOf(questionArray[i]) + 1)) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, (headers[0].indexOf(responseArray[i]) + 1));
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
}
我已经尝试了所有解决方案,但似乎没有一个成功的方法。
我希望单元格B2自动添加有人开始向单元格A1中输入数据的时间
答案 0 :(得分:0)
说实话,对于您要实现的代码,该代码非常复杂。
尝试使用此:
function onEdit(e) {
var TIME_ZONE = "GMT-2"; //enter desired timezone
var date = Utilities.formatDate(new Date(), TIME_ZONE, 'MM-dd-yyyy'); //enter desired date format
//if column A has been edited, set date in same row in column B
if (e.range.getColumn() == 1) {
var row = e.range.getRow();
e.source.getSheetByName('Leads').getRange(row, 2).setValue(date);
}
}
简单地,此代码将检查用户是否已编辑A列,如果是,则它将日期值放入B列的同一行。