简体   繁体   English

这个时间戳脚本有什么问题?

[英]What's wrong with this time stamp script?

I have a time stamp script giving the error: Cannot read property "source" from undefined.我有一个时间戳脚本给出错误:无法从未定义中读取属性“源”。 (line 3, file "Code") (第 3 行,文件“代码”)

I have looked into seeing if the problem was on line 1 function onEdit(e) and based off of sources I was reading, I can't run this from the script editor.我已经查看了问题是否在第 1 行function onEdit(e)上,并且根据我正在阅读的来源,我无法从脚本编辑器中运行它。 Is line 1 indeed the culprit to this error?第 1 行确实是这个错误的罪魁祸首吗?

function onEdit(e)
{
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() == "Sheet1") //Name of sheet
  {
    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf("Date & Time") + 1; // Name of Column you want to timestamp
    var orderCol = headers[0].indexOf("Input Field") + 1; // Name of Column you want to drop data
    if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol)
    {
      sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "PST","hh:mm:ss aaa"));
    }
  }
}

I have a sample sheet here.我这里有一个样本表。 https://docs.google.com/spreadsheets/d/1NcblMFTVPbKYPmfvh7YnfEHe3dl2QQp6HaC-dqtdiik/edit#gid=0 https://docs.google.com/spreadsheets/d/1NcblMFTVPbKYPmfvh7YnfEHe3dl2QQp6HaC-dqtdiik/edit#gid=0

There are two headers (Column A) Date & Time and (Column B) Input Field.有两个标题(A 列)日期和时间和(B 列)输入字段。 When Column B is editted, Column A should populate with a time stamp.编辑 B 列时,A 列应填充时间戳。

function onEdit(e) {
  var sh=e.range.getSheet();
  if (sh.getName()=="Sheet1"){
    var headers = sh.getRange(1,1,1,sh.getLastColumn()).getValues();
    var dateCol=headers[0].indexOf("Date & Time") + 1;
    var orderCol=headers[0].indexOf("Input Field") + 1
    if (dateCol>0 && e.range.rowStart>1 && e.range.columnStart==orderCol) {
      sh.getRange(e.range.rowStart, dateCol).setValue(Utilities.formatDate(new Date(), "PST","hh:mm:ss aaa"));
    }
  }
}

Nothing wrong with it.它没有错。 This takes use of the event object a little more.这需要更多地使用事件 object。 Remember, you can't run these functions from the script editor.请记住,您不能从脚本编辑器运行这些函数。 They require the event object and without it they will fail because "e" is undefined.他们需要事件 object 并且没有它他们将失败,因为“e”未定义。 But the event object provides a lot of useful data.但是事件 object 提供了很多有用的数据。

Try adding Logger.log(JSON.stringify(e));尝试添加Logger.log(JSON.stringify(e)); to the function like I did in your spreadsheet and you can see all of the data provided by the event object.就像我在您的电子表格中所做的那样,转到 function,您可以看到事件 object 提供的所有数据。

Thank you to everyone pointing out the big picture of how this script works.感谢大家指出这个脚本是如何工作的。 As it turns out, the problem was a set of cells causing the issue.事实证明,问题是导致问题的一组单元格。 I do not know how to convey the proper answer other than all I did was delete the cells on the 'column to add data' that was giving the issue, re-entered the data and the event triggered perfectly fine.我不知道如何传达正确的答案,除了我所做的只是删除给出问题的“添加数据列”上的单元格,重新输入数据并且事件触发得很好。 Not sure what was going on with these cells but it appears they were the culprit.不确定这些细胞发生了什么,但似乎它们是罪魁祸首。 I left the sample sheet un-editted to see for yourself.我留下未编辑的样本表,以便您自己查看。

If you try to type in column B, rows 19 and 23, nothing will happen.如果您尝试在 B 列第 19 行和第 23 行输入,则不会发生任何事情。 If you multi-select 19 and 23 Column B, delete, and enter data, it will work.如果多选 19 和 23 列 B,删除并输入数据,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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