google-apps-script - 从 V8 Runtime 转换为 Rhino Runtime (Legacy) 我需要进行哪些脚本调整?
问题描述
V8 Runtime 在通过脚本创建触发器时存在问题,因此我需要转换为旧的脚本模板,但是在尝试转换为 Legacy 时,它不会保存两个问题:
原始脚本第 1 部分:
function installFunctions() {
const excludedFunctions = ["onOpen", "installFunctions"];
const menu = SpreadsheetApp.getUi().createMenu('Funções do GAS');
for (let i in this) {
if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
menu.addItem(i, i);
}
}
menu.addToUi();
}
function onOpen() {
installFunctions();
}
Missing ; after the for-loop launcher.
未保存,因为它在这行代码中包含错误:
for (let i in this) {
原始脚本第 2 部分:
function deleteTriggerWithName(name) {
const trs = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());
const ts = ScriptApp.getProjectTriggers();
ScriptApp.deleteTrigger(ts[trs.indexOf(name)]);
}
未保存,因为它Syntax Error
在这行代码中包含 a:
const trs = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());
解决方案
Modifications
- Arrow functions are not supported in Rhino, so changed the
map
function into a normalfor
loop that adds items to an array. - It seems that while
const
is supported, usinglet
in afor
loop initialization is not. So replaced everything withvar
to be safe
Script
function installFunctions() {
var excludedFunctions = ["onOpen", "installFunctions"];
var menu = SpreadsheetApp.getUi().createMenu('Funções do GAS');
for (var i in this) {
if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
menu.addItem(i, i);
}
}
menu.addToUi();
}
function deleteTriggerWithName(name) {
var trs = []
for (var trigger in ScriptApp.getProjectTriggers()){
trs.push(trigger.getHandlerFunction())
}
var ts = ScriptApp.getProjectTriggers();
ScriptApp.deleteTrigger(ts[trs.indexOf(name)]);
}
This saved successfully in the script editor.
Reference
EDIT:
function deleteTriggerWithName(name) {
var trs = []
var projectTriggers = ScriptApp.getProjectTriggers()
for (var i=0; i != projectTriggers.length; i++) {
trs.push(projectTriggers[i].getHandlerFunction())
}
ScriptApp.deleteTrigger(projectTriggers[trs.indexOf(name)]);
}
推荐阅读
- swift - 在将值分配给 Swift 语言的属性之前,我们如何验证和调整值?
- javascript - ERROR TypeError: undefined is not an object (evalating 'this.props.navigation.navigate') when import my component in child
- flutter - m1 Mac 上的颤振插件显示错误“未找到模块 'foo'”
- swift - 在奇怪的行为中比较角度和向上
- python - 是否可以将 POST 请求中的视频读入 python-ffmpeg 或 OpenCV?
- python - Python pydantic 范围验证
- node.js - Dockerfile 应该执行“npm install”和“npm run build”还是应该只复制这些文件?
- r - 如何标记给定 ggplot 对象中数据点的 y 值?
- java - Vaadin - 导航时更新样式
- html - 使用 HTML 表单在新选项卡中生成输入结果