0

My script cannot be used in Google App Script V8 model, because there is a problem that causes triggers that are created automatically via script to crash.

So I need to use Legacy Mode, old version of GAS.

I'm trying to adjust the script so that a Menu is created in the spreadsheet, containing all the functions in the file.

This model works perfectly on 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();
}

However, in Legacy mode it doesn't work, so I was initially asked to make these changes: 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();
}

But, it returns the error mentioned in the question TypeError: Cannot find includes function in object onOpen,installFunctions., so I saw an indication to change the includes for indexOf, the Menu is created, but only with the OnOpen function appear, which in this case actually shouldn't appear because it should be excluded.

enter image description here

Full Script Test:

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) {
}

What do I need to modify in the script to make it work in Legacy mode?

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 1
    I wonder what's wrong with yours. it actually works on mine. Added a function named `otherFunction` and that's the only one that popped into the menu using Legacy mode. `includes` definitely works and no errors similar to yours. I did encounter an issue before making it work and it's not related to includes (just an error resulting to empty menu). Just added another function and it worked. – NightEye Jun 21 '21 at 17:26
  • Hi @NaziA I added the error alert to the question, Below these lines of script that I published, there are 5 functions, but unfortunately the Menu is not even created. – Digital Farmer Jun 21 '21 at 17:32
  • 1
    Are there special characters in your function names? can you at least provide or show them? just remove the contents of these functions in your post. – NightEye Jun 21 '21 at 17:39
  • 1
    I add in my question the name of functions @NaziA – Digital Farmer Jun 21 '21 at 17:45
  • 1
    can you try `var excludedFunctions = new Array("onOpen", "installFunctions");` instead? It seems the issue in your case is that it reads your excluded functions as object. I wonder if it works using `Array()` function. Reference, https://www.w3schools.com/js/js_arrays.asp – NightEye Jun 21 '21 at 17:48
  • . @NaziA Thank you in advance for taking the time to try and help me. About the change tip, unfortunately the same error persists. I can share the entire script if you want, there's nothing important in them, if you think it might be useful I do. – Digital Farmer Jun 21 '21 at 17:53
  • 1
    Feel free. But I'm not sure if the contents of the functions matter. but if it isn't a hassle. please do – NightEye Jun 21 '21 at 17:53
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234042/discussion-between-nazia-and-brondby-if). – NightEye Jun 21 '21 at 17:57

1 Answers1

1

After confirming in tests that it really doesn't work on your side. You can use indexOf instead.

Use:

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

Instead of:

if (typeof this[i] == "function" && !excludedFunctions.includes(i)) {
NightEye
  • 10,634
  • 2
  • 5
  • 24