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?

8
  • 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. Commented Jun 21, 2021 at 17:26
  • 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. Commented Jun 21, 2021 at 17:39
  • 1
    I add in my question the name of functions @NaziA Commented Jun 21, 2021 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, w3schools.com/js/js_arrays.asp Commented Jun 21, 2021 at 17:48
  • 1
    Feel free. But I'm not sure if the contents of the functions matter. but if it isn't a hassle. please do Commented Jun 21, 2021 at 17:53

1 Answer 1

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)) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.