如何获取当前活动/触发的触发器ID,

问题描述 投票:0回答:1

我已经构建了一个 Sheets 插件,它根据用户设置的触发器执行任务 所以问题是用户为多个工作表设置触发器,例如同步

触发器已执行,但仅针对触发器列表中的第一个触发器,而不是全部

问题是所有设置触发器仅触发一个触发器

一个触发器的日志&所有触发器都有相同的日志

Active Trigger: -5962537404932988746 for function: fetchSynctriggerWithDetails
Active Trigger: -9139074510508922527 for function: fetchSynctriggerWithDetails
Active Trigger: -1495623190131824397 for function: fetchSynctriggerWithDetails
Active Trigger: -1224866675796805716 for function: fetchSynctriggerWithDetails
Active Trigger: 1983234386806141271 for function: fetchSynctriggerWithDetails
Active Trigger: -8082155554848723508 for function: fetchSynctriggerWithDetails

Response: {"success":true,"triggerDetails":{"trigger_id":"-5962537404932988746","sheet_name":"SKU","databaseid":"1","trigger_time":"1hour"}}

所以问题是它有所有触发器,但它总是运行

响应:{"success":true,"triggerDetails":{"trigger_id":"-5962537404932988746","sheet_name":"SKU","databaseid":"1","trigger_time":"1hour"}}

因为找不到 var triggerId = currentTrigger.getUniqueId(); 对于每个触发器 & 每次返回 -5962537404932988746 作为当前触发器 ID

这是错误的

感谢任何帮助


function createSyncTrigger() {

/// all the setup for setting up trigger 

// I am also saving trigger in PropertiesService

PropertiesService.getScriptProperties().setProperty(triggerId, JSON.stringify(triggerDetails));

}


获取触发器详细信息的代码片段


function getTriggerDetailsSync() {
  var triggers = ScriptApp.getProjectTriggers();
   // Log all active triggers for debugging
  triggers.forEach(trigger => {
    Logger.log('Active Trigger: ' + trigger.getUniqueId() + ' for function: ' + trigger.getHandlerFunction());
  });

   //var currentTrigger = triggers.find(trigger => trigger.getHandlerFunction() === 'fetchSynctriggerWithDetails');

 // Get the current trigger that invoked this function
  var currentTrigger = ScriptApp.getProjectTriggers().find(trigger => 
    trigger.getHandlerFunction() === 'fetchSynctriggerWithDetails'
  );

  if (!currentTrigger) {
    Logger.log('No trigger found for fetchSynctriggerWithDetails');
    return null;
  }

  var triggerId = currentTrigger.getUniqueId();

}
javascript google-sheets google-apps-script google-cloud-platform triggers
1个回答
0
投票

我以某种方式使用事件修复了它

// Func Called in the Trigger  
// passing events to getTriggerDetailsimport func

function fetchTableDataWithDetails(e) {
  var triggerDetails = getTriggerDetailsimport(e);
  
  if (!triggerDetails) {
    Logger.log('No trigger details found.');
    return;
  }

}


//USING EVENT TO GET CURRENT TRIGGER 

function getTriggerDetailsimport(e) {

   if (!e || !e.triggerUid) {
    Logger.log('Error: No event object or triggerUid found.');
    return null;
   }

  var triggerId = e.triggerUid; // Get the unique ID of the active trigger
  Logger.log('Active Trigger ID: ' + triggerId);

}


&它适用于触发器的每个事件,我得到正确的触发器ID

//一些触发事件的LOGS 活动触发器 ID:1983234386806141271 活动触发器 ID:-8082155554848723508 活动触发器 ID:-5962537404932988746

终于成功了

© www.soinside.com 2019 - 2024. All rights reserved.