有没有办法在 Google Apps 脚本中每隔几秒调用一次自定义函数?
Is there a way to call a custom function every couple of seconds in Google Apps Script?
我正在尝试在 google 应用程序脚本和工作表中制作一个愚蠢但简单的小程序,它会随机挑选一个爸爸的笑话,每隔几秒钟给你看一次。我尝试使用 setInterval()
,但我发现它不包含在 google 应用程序脚本中。有什么建议吗?
代码:
function LOL() {
let messageList = ["Where do dads keep their jokes? In a dad-abase!","When does a joke become a dad joke? When it becomes a-parent!","Two men walk into a bar. You'd think the second one would've noticed!","Does your face hurt? 'Cause it's killing me!"]
function randInt() {
let listLength = messageList.length
let random = Math.floor(Math.random() * listLength);
return random
}
function showMessage() {
let int = randInt()
console.log(int)
return messageList[int]
}
return showMessage()
}
它会每分钟从我的列表中选择一条随机消息放入任何具有 =LOL()
的单元格。
您可以手动管理时间驱动触发器,例如运行 一个特定的函数 everyHour()。参见 here
这里我找到了 that will update a custom function by using a TextFinder and combined it with Time driven trigger的解决方案,每分钟自动刷新一次。
试试这个:
代码:
function LOL(){
let messageList = ["Where do dads keep their jokes? In a dad-abase!","When does a joke become a dad joke? When it becomes a-parent!","Two men walk into a bar. You'd think the second one would've noticed!","Does your face hurt? 'Cause it's killing me!"]
let listLength = messageList.length
let random = Math.floor(Math.random() * listLength);
return messageList[random];
}
function refresher() {
const sheet = SpreadsheetApp.getActiveSheet();
const formula = "=LOL";
sheet.createTextFinder("^\" + formula).matchFormulaText(true).useRegularExpression(true).replaceAllWith("Loading");
sheet.createTextFinder("Loading").matchFormulaText(true).useRegularExpression(true).replaceAllWith(formula);
}
可安装触发器设置:
输出:
您可以使用 Serge's Answer 中的原生 Utilities.sleep,但如果您需要在脚本中执行任何其他操作,我建议您使用触发器或异步函数。
这是一个简单的实施示例:
function myFunction() {
var delayInMilliseconds = 5000; //1 second
while (true){
Utilities.sleep(delayInMilliseconds)
showMessage()
}
}
我正在尝试在 google 应用程序脚本和工作表中制作一个愚蠢但简单的小程序,它会随机挑选一个爸爸的笑话,每隔几秒钟给你看一次。我尝试使用 setInterval()
,但我发现它不包含在 google 应用程序脚本中。有什么建议吗?
代码:
function LOL() {
let messageList = ["Where do dads keep their jokes? In a dad-abase!","When does a joke become a dad joke? When it becomes a-parent!","Two men walk into a bar. You'd think the second one would've noticed!","Does your face hurt? 'Cause it's killing me!"]
function randInt() {
let listLength = messageList.length
let random = Math.floor(Math.random() * listLength);
return random
}
function showMessage() {
let int = randInt()
console.log(int)
return messageList[int]
}
return showMessage()
}
它会每分钟从我的列表中选择一条随机消息放入任何具有 =LOL()
的单元格。
您可以手动管理时间驱动触发器,例如运行 一个特定的函数 everyHour()。参见 here
这里我找到了
试试这个:
代码:
function LOL(){
let messageList = ["Where do dads keep their jokes? In a dad-abase!","When does a joke become a dad joke? When it becomes a-parent!","Two men walk into a bar. You'd think the second one would've noticed!","Does your face hurt? 'Cause it's killing me!"]
let listLength = messageList.length
let random = Math.floor(Math.random() * listLength);
return messageList[random];
}
function refresher() {
const sheet = SpreadsheetApp.getActiveSheet();
const formula = "=LOL";
sheet.createTextFinder("^\" + formula).matchFormulaText(true).useRegularExpression(true).replaceAllWith("Loading");
sheet.createTextFinder("Loading").matchFormulaText(true).useRegularExpression(true).replaceAllWith(formula);
}
可安装触发器设置:
输出:
您可以使用 Serge's Answer 中的原生 Utilities.sleep,但如果您需要在脚本中执行任何其他操作,我建议您使用触发器或异步函数。
这是一个简单的实施示例:
function myFunction() {
var delayInMilliseconds = 5000; //1 second
while (true){
Utilities.sleep(delayInMilliseconds)
showMessage()
}
}