Home > Enterprise >  debug a google apps script
debug a google apps script

Time:04-27

I don't uderstand why my function doesn't recognize the 'e' from the onEdit function? Do you have an idea? Thank's a lot if you can help me the error when i try to debug

function onEdit(e){
  
  var feuille = e.source;
  if(SpreadsheetApp.getActiveRange().getColumn()==7){  //the 7th column is the " /-" column
    var i = e.source.getActiveRange().getRow(); //i is the line of the change
    var k = 408 i;   // k is the line where we put the date
    var l = 2;       //l is the column where we put the date and the quantity
    var cellDate = feuille.getRange(k,l);  
    while (isEmpty(cellDate)==false){  //the column of the date is incremented 
      l=l 1;
    }
    feuille.getRange(k,l).setValue(new Date());
    const stockDate = feuille.getRange(i,8).getValue();
      feuille.getRange(k 1,8).setValue(stockDate);
    var spreadsheet = SpreadsheetApp.getActive();   
    spreadsheet.getRange("G2:G403").setValue('0');  ////we reset the " /-" column to zero

  }    
    
    
}

CodePudding user response:

The onEdit(e) function is a simple trigger that is designed to run automatically when you hand edit the spreadsheet. In that context, the event object e is properly populated.

If you blindly run your onEdit(e) function in the script editor, the event parameter e is not populated, causing the error you mention.

You can debug your onEdit(e) function by writing a wrapper function that creates an object and uses it as parameter when calling onEdit(e), like this:

function testOnEdit() {
  const e = {
    source: SpreadsheetApp.getActive(),
  };
  onEdit(e);
}

Then debug the testOnEdit() function.

See event object.

CodePudding user response:

According to the documentation for Event Objects:

Simple triggers and installable triggers let Apps Script run a function automatically if a certain event occurs. When a trigger fires, Apps Script passes the function an event object as an argument, typically called e. The event object contains information about the context that caused the trigger to fire.

Therefore, e is the event object passed on the onEdit function.

The reason you are receiving this is because the object is passed only when an edit is made aka triggering the onEdit trigger.

If you run the function from the editor, then you will get this error as no edit is being made and therefore no edit object is being passed, hence, e being empty.

Reference

  • Related