将事件对象从触发函数传递到另一个函数 [英] Passing event objects from trigger function to another function

查看:89
本文介绍了将事件对象从触发函数传递到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定将此 onEdit(e)触发函数分解为多个函数,但是当我这样做时,事件对象(e)部分丢失。经过一段时间的摸索,我终于让它再次工作,但我认为它不是最有效的解决方案。

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.

任何建议,或者这足够好吗?基本上,我只是添加了 var e = e; ,它又可以正常工作。

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...");
      
    }
  }
}  


推荐答案

以免在评论中增加讨论:您可以从脚本中安全地删除 var e = e 分配,因为它不影响脚本更新版本解决的问题:

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 事件对象,其构造为对触发触发器的响应。由于在您的情况下,触发器是 onEdit(e)触发器,因此事件对象是 undefined ,直到在目标电子表格(请注意,脚本触发的编辑不计算在内);

  2. 即使您使用参数调用函数(如 doSomething(e)),以防您不通过 arguments 对象访问参数,或者在函数声明中显式定义该参数的情况函数doSomething(e),事件对象将不会保留;

  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;

此外,您可能会错过最后一个 subCatDV()调用+ <$ c $的 e 绑定c>如果语句可以优化(顺便说一句,请不要使用平等比较,请使用身份比较,它将为您节省调试时间):

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);
}

有用的链接


  1. 事件对象引用;

参数对象参考;

这篇关于将事件对象从触发函数传递到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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