google-apps-script - TypeError:找不到包含对象中的函数(Google App Script)
问题描述
我的脚本无法在 Google App Script V8 模型中使用,因为存在导致通过脚本自动创建的触发器崩溃的问题。
所以我需要使用旧版 GAS 的 Legacy Mode。
我正在尝试调整脚本,以便在电子表格中创建一个菜单,其中包含文件中的所有功能。
该模型在 V8 上完美运行: https ://stackoverflow.com/a/68053222/11462274
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();
}
但是,在传统模式下它不起作用,所以我最初被要求进行这些更改: https ://stackoverflow.com/a/68064124/11462274
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 onOpen() {
installFunctions();
}
但是,它返回问题中提到的错误TypeError: Cannot find includes function in object onOpen,installFunctions.
,所以我看到了更改includes
for的指示indexOf
,创建了菜单,但只有OnOpen function
出现,在这种情况下实际上不应该出现,因为它应该被排除在外。
完整的脚本测试:
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 onOpen() {
installFunctions();
}
function ClimaParaTestes() {
}
function JogosParaClimaParaTestes() {
}
function EnviarClimasParaSquads() {
}
function ApagarHistoricoDoClima() {
}
function deleteTriggerWithName(name) {
}
我需要在脚本中修改什么以使其在 Legacy 模式下工作?
解决方案
在测试中确认它确实对您不起作用之后。你可以indexOf
改用。
利用:
if (typeof this[i] == "function" && excludedFunctions.indexOf(i) < 0) {
代替:
if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
推荐阅读
- php - 如何使用外部 URL 或链接打开自定义选项卡及其 div?
- python - Django Rest Framework - 如何将图像导入为 JPEG 并使用序列化程序将其保存为 base 64?
- javascript - 如何将嵌套的 json 数据绑定到 angularjs 下拉列表中?
- jsf - 通过调用 javaScript 函数来中断 Bean 方法?
- python - 如何在使用 testinfra 库的失败测试中返回 stderr=?
- ios - 更新 Firebase 包后,“InstanceID”不包含“SetApnsToken”的定义
- python - 重定向后来自模板的信息,Django - Python
- ios - 在 iOS 上拦截崩溃
- android - 输出形式 ArrayMap 等于 null
- shell - 如何在 Shell 脚本中运行多个 SQL 脚本文件