Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

google apps script - TypeError: Cannot read property "source" from undefined. (line 7, file "Code")

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);
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

  • To change the function name, as onEdit is one of the reserved function names.
  • To remove the function argument.
  • To replace event.source by SpreadsheetApp.getActiveSpreadsheet()

References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...