代码之家  ›  专栏  ›  技术社区  ›  Alan Wells

应用程序脚本获取触发器事件类型名称-如何从触发器类获取事件类型的名称

  •  0
  • Alan Wells  · 技术社区  · 5 年前

    以下代码无法找到触发器的事件类型,即使事件类型名称作为字符串是正确的。方法 getEventType() 是获取一个对象,而不是字符串。根据以下文件:

    https://developers.google.com/apps-script/reference/script/event-type?hl=en

    这个 getEventType() 方法返回EventType枚举。但是文档中没有列出任何从枚举中获取任何内容的方法,文档中列出的属性也没有返回任何内容。

    假设要查找的事件类型为 ON_FORM_SUBMIT 需要如何修改代码以检测触发器是否适用于该事件类型?

    function getEventTypeNameOfTrigger() {
      
      var oneTrigger,triggers,triggerEventType;
    
      triggers = ScriptApp.getProjectTriggers();//Get the projects triggers
      
      oneTrigger = triggers[0];//Get the first trigger - For testing
      
      triggerEventType = oneTrigger.getEventType();//Use the getEventType method to get the EventType ENUM
      
      Logger.log('triggerEventType: ' + triggerEventType);//Displays the event type name in the logs
      
      Logger.log('typeof triggerEventType: ' + typeof triggerEventType);//Displays "object"
      
      Logger.log(triggerEventType === 'ON_FORM_SUBMIT');//Evaluates to FALSE even when the event type name is ON_FORM_SUBMIT
      
      
    }
    
    0 回复  |  直到 4 年前
        1
  •  3
  •   tehhowch    5 年前

    一种可能是简单地依赖字符串表示。因为我们知道事件类型显示为 ON_FORM_SUBMIT 查看日志时,我们知道 toString() 在eventType上,将对应于 在表格上提交 :

    Logger.log(triggerEventType.toString() === 'ON_FORM_SUBMIT'); // true
    

    首选方法是比较枚举:

    switch (triggerEventType) {
      case ScriptApp.EventType.CLOCK:
        Logger.log('got a clock event');
        break;
      case ScriptApp.EventType.ON_FORM_SUBMIT:
        Logger.log('got a form submit event')
        break;
      ...
    }
    

    这是首选,因为这意味着你对谷歌如何实现枚举并不敏感。