我正在嘗試在谷歌應用程序腳本和工作表中製作一個愚蠢但簡單的小程序,它每隔幾秒鐘就會隨機挑選一個爸爸的笑話向您展示。我嘗試使用 setInterval(),但我發現它不包含在谷歌應用程序腳本中。有什麼建議?
代碼:
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() 的單元格中。
在這裡,我找到了Tanaike的解決方案,該解決方案將使用TextFinder更新自定義函數,並將其與時間驅動觸發器相結合,以每分鐘自動刷新一次。
嘗試這個:
代碼:
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);
}
可安裝的觸發器設置:
輸出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句