首页 > 解决方案 > 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.,所以我看到了更改includesfor的指示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 模式下工作?

标签: google-apps-scriptgoogle-sheets

解决方案


在测试中确认它确实对您不起作用之后。你可以indexOf改用。

利用:

if (typeof this[i] == "function" && excludedFunctions.indexOf(i) < 0) {

代替:

if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {

推荐阅读