TypeError:无法读取属性"source"来自未定义.(第7行,文件“代码") [英] TypeError: Cannot read property "source" from undefined. (line 7, file "Code")
问题描述
我在Google表格中遇到问题TypeError:无法从未定义中读取属性"source".(第7行,文件代码")当运行以下代码时,请帮助
I am having an issue in Google Sheets TypeError: Cannot read property "source" from undefined. (line 7, file "Code") When Running the following Code Please Help
function onEdit(event)
{
var timezone = "GMT-5";
var timestamp_format = "MM-dd-yyyy-hh-mm-ss";
var updateColName = "Ticket#";
var timeStampColName = "TimeComplete";
var sheet = event.source.getSheetByName('InAndOut');
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);
}
}
推荐答案
source
是 event
的一个属性,尚未分配对象,因此被称为未分配.当直接从脚本编辑器运行像您这样的函数时,就会发生这种情况.
source
is a property of event
that has not being assigned an object, so it was referred as unassigned. This occurs when a function like yours is ran directly from the script editor.
要在脚本编辑器中运行它,首先必须将一个具有相应属性的事件对象分配给function参数.有关详细信息,请参见如何在GAS中测试触发功能?
To run it from the script editor, first you have to assign an event object with the corresponding properties to the function argument. For details see How can I test a trigger function in GAS?
如果要通过脚本编辑器运行它,建议:
If you will be running it from the script editor, it's recommended:
- 要更改功能名称,因为onEdit是保留的功能名称之一.
- 删除函数参数.
- 将
event.source
替换为SpreadsheetApp.getActiveSpreadsheet()
这篇关于TypeError:无法读取属性"source"来自未定义.(第7行,文件“代码")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!