【问题标题】:Click play button in Google Apps Script with Greasemonkey?使用 Greasemonkey 在 Google Apps 脚本中单击播放按钮?
【发布时间】:2016-10-10 20:58:09
【问题描述】:

我正在编写一个 Greasemonkey 脚本,以每五 5 分钟点击一次 Google Apps 脚本中脚本的播放按钮,以避免 Google 设置的执行时间限制。

时间结束后,我能够识别脚本,但无法使用 JavaScript 单击“运行”按钮。我一直在用谷歌浏览器检查这个按钮并尝试了一些东西,但我无法让它工作。

谁能帮帮我?

我想点击 Google 表格工具栏中的任何按钮都会完全相同..

谢谢!

【问题讨论】:

标签: javascript google-apps-script google-sheets greasemonkey


【解决方案1】:

您以完全错误的方式处理此问题。您应该包括在脚本内部使用触发器重新启动执行的可能性。我会告诉你我是怎么做到的。请记住,我的脚本非常大,它执行一个循环,我必须让它记住它在循环中停止的位置,以便它可以继续处理相同的数据。

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Purpose: Check if there is enough time left for another data output run
// Input:   Start time of script execution
// Output:  Boolean value if time is up
function isTimeUp(start, need) {  
  var cutoff = 500000 // in miliseconds (5 minutes)
  var now = new Date();
  return cutoff - (now.getTime() - start.getTime()) < need; 
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这里的start 只是您在启动脚本时创建的new Date()need 只是我的脚本执行 1 个循环所需的平均时间。如果没有足够的时间循环,我们将用另一个函数切断脚本。

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Purpose: Store current properties and create a trigger to start the script after 1 min
// Input:   propertyCarrier object (current script execution properties)
// Output:  Created trigger ID
function autoTrigger(passProperties, sysKeys) {  
  var sysProperties = new systemProperties();

  if (typeof sysKeys === 'undefined' || sysKeys === null) {
    sysKeys = new systemKeys();
  }

  var triggerID = ScriptApp.newTrigger('stateRebuild')
                           .timeBased()
                           .after(60000)
                           .create()
                           .getUniqueId();

  Logger.log('~~~ RESTART TRIGGER CREATED ~~~');

//--------------------------------------------------------------------------
// In order to properly retrieve the time later, it is stored in milliseconds
  passProperties.timeframe.start = passProperties.timeframe.start.getTime();
  passProperties.timeframe.end = passProperties.timeframe.end.getTime();
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
// Properties are stored in User Properties using JSON
  PropertiesService.getUserProperties()
                   .setProperty(sysKeys.startup.rebuildCache, JSON.stringify(passProperties));
//--------------------------------------------------------------------------

  Logger.log('~~~ CURRENT PROPERTIES STORED ~~~');
  return triggerID;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

如果您不需要记住停止的位置,则可以更简单(根据您当前的实现判断,您并不关心是否从头开始)。

您创建的触发器应该针对主函数,或者如果您需要像我一样传递数据,则需要有一个单独的启动函数来从用户属性中获取数据并将其传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2014-06-29
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多