我想制作一个Google脚本,将其每分钟永久添加1.利用以前的帖子,我感觉自己很亲近 [英] I want to make a google script that adds 1 to a running total every minute, in perpetuity. Utilizing previous posts I feel I am close
本文介绍了我想制作一个Google脚本,将其每分钟永久添加1.利用以前的帖子,我感觉自己很亲近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var x = 0
Logger.log(x)
function myFunction(){
var y = x+1
Logger.log(y)
x = y
Logger.log(x)
}
// execute this only and once
function createTrigger(){
ScriptApp.newTrigger(myFunction())
.timeBased()
.everyMinutes(1)
.create();
}
以上是我的代码.在我的脑海中,它将x设置为0,将其记录下来.然后引入函数myFunction并将其加到x并将其记录.
Above is my code. In my head it sets x to 0, logs that. Then function myFunction is introduced and adds 1 to x and logs it.
然后根据提示,我得到了下面的文字,应该使此情况每分钟发生一次.没有.
Then based on a tip I got this bottom text should make this happen every minute. It doesnt.
这是执行日志:日志图片
我也尝试过这样的操作:像14个区别
I've tried it like this too: Like 14 difference
推荐答案
修改点:
- 在脚本中,每次运行都声明变量
x = 0
.这样,x
不会增长.我认为这可能是您遇到问题的原因.在这种情况下,如何使用Properties Service
? - 您需要修改
ScriptApp.newTrigger(myFunction())
的脚本.newTrigger(functionName)
的functionName
是字符串值. - In your script, the variable
x = 0
is declared every run. By this,x
is not grown. I thought that this might be the reason of your issue. In this case, how about usingProperties Service
? - Your script of
ScriptApp.newTrigger(myFunction())
is required to be modified.functionName
ofnewTrigger(functionName)
is the string value.
Modification points:
当以上几点反映到您的脚本中时,它如下所示.
When above points are reflected to your script, it becomes as follows.
function myFunction() {
var p = PropertiesService.getScriptProperties();
var x = p.getProperty("x");
if (x) {
Logger.log(Number(x)) // Value before 1 is added.
x = Number(x) + 1;
} else {
x = 1; // or x = 0;
}
Logger.log(x) // Value after 1 was added.
p.setProperty("x", x);
}
// execute this only and once
function createTrigger() {
ScriptApp.newTrigger("myFunction")
.timeBased()
.everyMinutes(1)
.create();
}
- 运行
createTrigger()
时,将为myFunction()
的功能安装时间驱动的触发器. - 运行
myFunction()
时,通过添加1
的数量来增大x
的值. - When
createTrigger()
is run, the time-driven trigger is installed for the function ofmyFunction()
. - When
myFunction()
is run, the value ofx
is grown by adding the number of1
.
这篇关于我想制作一个Google脚本,将其每分钟永久添加1.利用以前的帖子,我感觉自己很亲近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文