类型错误:无法读取属性“源"从未定义.(第 7 行,文件“代码") [英] TypeError: Cannot read property "source" from undefined. (line 7, file "Code")

查看:34
本文介绍了类型错误:无法读取属性“源"从未定义.(第 7 行,文件“代码")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google 表格中遇到问题类型错误:无法从未定义中读取属性源".(第 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);
  }
}

推荐答案

sourceevent 的一个没有被赋值的属性,所以被称为未分配.当直接从脚本编辑器运行像您这样的函数时会发生这种情况.

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.

要从脚本编辑器运行它,首先必须将具有相应属性的事件对象分配给函数参数.详情见如何在 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()

这篇关于类型错误:无法读取属性“源"从未定义.(第 7 行,文件“代码")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆