用Greasemonkey点击Google Apps脚本中的播放按钮?
问题描述:
我编写了一个Greasemonkey脚本,每5分钟在Google Apps脚本中单击一个脚本的播放按钮,以避免Google设置的执行时间限制。用Greasemonkey点击Google Apps脚本中的播放按钮?
当时间结束但我无法使用JavaScript单击“运行”按钮时,我能够识别脚本。我一直在使用谷歌浏览器检查按钮,并尝试了一些东西,但我无法使其工作。
任何人都可以帮我吗?
我猜一下谷歌表的工具栏上的任何按钮将是完全一样的..
谢谢!
答
你正以完全错误的方式接近这一点。您应该包含在脚本内部使用触发器重新启动执行的可能性。我会告诉你我是如何做到的。请记住,我的脚本非常大,它执行一个循环,我不得不记住它停止的循环中的位置,因此它可以继续使用相同的数据。
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
此致可以更简单,如果你不需要记住您停止(由目前的实施来看你不关心你是否从头开始或没有)。
您创建的触发器应该瞄准主函数,或者如果您需要像我一样传递数据,则需要具有单独的启动器函数以从用户属性获取数据并将其传递。
替代greasemonkey使用时间驱动的触发器。请参阅https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers –
或捕获超时错误并再次调用脚本的html。 –
我低调这个问题,因为它显示零研究工作。你只是做了一个控制台的截图。你的代码在哪里不能像你期望的那样工作?另请阅读:http://stackoverflow.com/help/how-to-ask –