2

I decided to break this onEdit(e) trigger function up into multiple functions, but when I did, the event objects (e) portion got "lost." After messing with it for a while, I finally got it to work again, but I don't think it's the most efficient solution.

Any suggestions, or is this good enough? Basically, I just added var e = e; and that made it work again.

function onEdit(e){
  Logger.log(e);
  
  if(e.range.getSheet().getName() == 'Estimate'){   
    var e = e;
    Logger.log("Starting subCatDV...");
    subCatDV(e);
    Logger.log("Finished subCatDV!");

    
    Logger.log("Starting itemDV...");
    itemDV(e);
    Logger.log("Finished itemDV!");


    Logger.log("Starting subItemDV...");
    subItemDV(e);
    Logger.log("Finished subItemDV!");
    
  }    
  
  if(e.range.getSheet().getName() == 'Items'){
    subCatDV();
  }
  
  return;
    
}  

Here is the function that didn't seem to be receiving the event objects

function subItemDV(e){
  // Populate sub-item data validations
  
  var estss = SpreadsheetApp.getActive().getSheetByName('Estimate');
  var itemss = SpreadsheetApp.getActive().getSheetByName('Items');
  var subItemDVss = SpreadsheetApp.getActive().getSheetByName('subItemDataValidations');
  var activeCell = estss.getActiveCell();
  
  Logger.log("I'm in subItemDV...");
  Logger.log(e);
  Logger.log(activeCell);
  
  Logger.log("Checking sheet name...");
  
  if(activeCell.getColumn() == 3 && activeCell.getRow() > 1){
    if(e.range.getSheet().getName() == 'Items') return;
    
    Logger.log("Not in 'Items' sheet!  Moving on...");
    
    activeCell.offset(0, 1).clearContent().clearDataValidations();
    
    var subItem = subItemDVss.getRange(activeCell.getRow(),activeCell.getColumn(),itemss.getLastColumn()).getValues();
    var subItemIndex = subItem[0].indexOf(activeCell.getValue()) + 2;
    
    
    Logger.log("Checking subItemIndex...");
    
    if(subItemIndex != 0){
      
      var subItemValidationRange = subItemDVss.getRange(activeCell.getRow(),4,1,subItemDVss.getLastColumn());
      var subItemValidationRule = SpreadsheetApp.newDataValidation().requireValueInRange(subItemValidationRange).build();
      
      activeCell.offset(0, 1).setDataValidation(subItemValidationRule);

      Logger.log("Finished checking subItemIndex...");
      
    }
  }
}  
TC76
  • 860
  • 1
  • 8
  • 17
  • Please, clarify during what call and under what circumstances the `event object` gets lost? It is constructed [only in response](https://developers.google.com/apps-script/guides/triggers/events) to triggers (in your case - "onEdit") and is persisted throughout the callback chain if you do not reassign or modify it. Btw, since your two `if` statements check the same thing, you should use one `if..else` statement instead – Oleg Valter is with Ukraine Jun 11 '19 at 00:55
  • Hello @OlegValter! Thank you for your response! I edited the OP to reflect the affected function. – TC76 Jun 11 '19 at 01:22
  • Hi Trey! Thanks for clarifying! Could you please tell what was the issue you experienced? Judging from your current code, there is nothing that can cause the `event object` to become `undefined` (btw, is this what you mean by "lost"?) – Oleg Valter is with Ukraine Jun 11 '19 at 01:29
  • Yes, I suppose that is what I meant. I'm just fumbling through GAS, trying to teach myself. So I don't always use appropriate lingo. Before I added `var e = e;` and added `(e)` to each function, it just wasn't passing the values and stopping mid-function. Hence all of the `Logger` sprinkled throughout. Trying to pinpoint where it was stopping – TC76 Jun 11 '19 at 01:34

1 Answers1

1

So as not to inflate discussion in comments: you can safely remove the var e = e assignment from the script, as it does not affect the problems that your updated version of the script solved:

  1. e is an event object that is constructed as a response to trigger being fired. Since in your case the trigger is an onEdit(e) trigger, event object is undefined until an edit is made in the target Spreadsheet (please, note that script-triggered edits do not count);
  2. Even if you called the function with a parameter (like doSomething(e)), in case you either do not access the parameter via the arguments object, or explicitly define it in a function declaration function doSomething(e), the event object won't be persisted;

Also, you might've missed the e binding in the last subCatDV() call + the if statement can be optimized (btw, don't use the equality comparison, use the identity comparison instead, it will save you debugging time in the future):

var name = e.range.getSheet().getName();
if(name === 'Estimate') {
  doSomething(e);
}else if(name === 'Items') { //identity comparison ensures type match;
  doSomethingElse(e);
}

Useful links

  1. event object reference;

  2. arguments object reference;