繁体   English   中英

以编程方式将基于时间的触发器添加到Google Apps脚本中的加载项

[英]Programmatically adding a time-based trigger to an add-on in Google Apps Script

故事

我正在发布第一个电子表格加载项。 它是教师的交流工具,我需要脚本在安装时添加基于时间的触发器。 我在过去大约一天的时间里一直在从事此工作,但我想我已经接近了,但是基于时间的触发器/功能仍然不适用于测试人员。

设置创建触发器的安装逻辑

到目前为止,这是我的逻辑:
onInstall(e) onOpen(e)onOpen(e) ,几行代码调用一个函数,该函数检查是否存在触发器(使用ScriptApp.getProjectTriggers();方法),该触发器在需要时调用所需函数一个还不存在,它调用另一个函数来创建触发器。代码基本上如下所示:

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Standard Install Logic ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function onInstall(e) {
   onOpen(e);
}

function onOpen(e) {
  // sets up the UI in case it's not in the right auth mode
  var menu = SpreadsheetApp.getUi().createAddonMenu();
  if (e && e.authMode !== ScriptApp.AuthMode.FULL) {
    menu.addItem("Set Up the Addon", "initialSetup") // Add a menu item that works in all auth modes to get to full authorization.
    .addToUi();

  } else {
    menu
    .addItem("Menu Stuff", "someFunction")
    .addToUi();

    var checkTrigger = triggerCheck(); // ensure the the trigger is installed
    if (checkTrigger != true) {
      createHoneybeeTrigger();
    }
  }
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Functions to Help Set Up the Trigger if It's Not There ////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

// function that's callable from the UI if the user isn't yet in auth mode FULL
function initialSetup () {
  triggerCheck();
  onOpen();
  SpreadsheetApp.getUi().alert('Everything looks good! You can now use the addon.')
}

// function to see if a trigger yet exists that calls the desired function
function triggerCheck () {

  var triggers = ScriptApp.getProjectTriggers(); // get current project's triggers
  var triggerFunctions = [];
  triggers.forEach(function (trigger) {
    var handlerFunction = trigger.getHandlerFunction();
    triggerFunctions.push(handlerFunction); // push the functions called by triggers into array
  });

  // check the array to see if one of the current trigger functions is myFunction
  var correctTrigger = function(functionName) { // sets up the filter
    return functionName == 'myFunction';
  }
  var triggerPresent = triggerFunctions.some(correctTrigger);

  return triggerPresent;
}

// function to create the trigger if it's not yet there
function createTrigger () {
  ScriptApp.newTrigger('myFunction')
  .forSpreadsheet(SpreadsheetApp.getActive())
  .timeBased()
  .everyHours(12)
  .create();
  return true;
}

奇怪的验证结果

我还设置了一个可以从用户界面调用的小函数,该函数使用triggerCheck ()函数(上面列出)来检查是否已安装触发器,然后就结果向他们发出警报。 虽然发生了一些奇怪的事情。 在我的测试帐户(处于完全验证模式下)中,我可以运行此菜单项,检查触发器是否存在,并查看是否存在。 但是,触发器仍不会触发。 为什么/怎么会这样?

这些东西真的很难测试,因为在为测试帐户安装该插件时,我无法清楚了解该插件的活动触发器。 有什么提示吗?


关于SO的类似但不确切的问题在这里 ,但我的问题是关于可安装的Google附加组件,因此用例是不同的。

长话短说,您必须更改附加逻辑。

为什么?

诸如onOpen之类的简单触发器,当它们被事件触发时,在这种情况下,打开在LIMITED授权模式下运行的电子表格,这意味着某些操作(如获取项目触发器)是不可能的。

提示

  • 保持onOpen和其他简单的触发器来执行“简单”的事情。 首先限制onOpen以创建附加的自定义菜单。
  • 使用打开时可安装的触发器进行加载项(脚本)/电子表格/用户设置/初始化验证
  • 查看https://developers.google.com/apps-script/support/troubleshooting,以了解有关Google Apps Script项目的基本故障排除技术。 在这种特定情况下,执行记录和在https://scripts.google.com/executions上记录的错误可以帮助找出发生错误的位置以及错误的原因。

备注

是的,加载项很难测试,但您并不孤单。 在这里,我们有很多问题可以为您的旅途提供帮助,例如如何在GAS中测试触发功能?

有关

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM