0

I'm following the solution found in this question but when I click one of the generated buttons, I get this error: Script function not found: test1.

const flags = {};
flags["test1"] = "data";
flags["test2"] = "data";

function onOpen(){
  var ui = SpreadsheetApp.getUi();

  Object.keys(flags).forEach(function (flag) {
    this[flag] = function () {
      addToFlagInteract(flag);
    }
  });

  var flagMenu = ui.createMenu('Flag');
  var flagSubMenu = ui.createMenu('Add to Flag');

  Object.keys(flags).forEach(function (flag) {
    flagSubMenu.addItem(flag,flag);
  });

  flagMenu.addSubMenu(flagSubMenu).addToUi();
}

function addToFlagInteract(flag){
  //Do something
}
nikko.guy
  • 41
  • 8
  • 1
    Does [this](https://stackoverflow.com/a/64384141/) answer your question and explain Tanaike's answer? – TheMaster Nov 08 '21 at 07:49
  • 1
    @TheMaster From your comment, I noticed that I forgot [the thread](https://stackoverflow.com/q/64383424/7108653). I think that this question is the same as it. So, I deleted my answer. Because I think that [this thread](https://stackoverflow.com/q/64383424/7108653) can explain the OP's issue well. – Tanaike Nov 08 '21 at 08:11
  • 1
    @Tanaike No need to delete. It may be helpful even if we close this as duplicate. In your answer though, a minor issue would be `onOpen` would run twice, when onOpen is triggered, right? Also, I think OP missed the part where you added `onOpen()` at the end. – TheMaster Nov 08 '21 at 10:22
  • @TheMaster Yes. I think so. When I had searched the duplicated question when I saw this question, unfortunately, I couldn't find it. About `run twice`, for example, when in order to run `onOpen` as the global, `const sample = (_ => onOpen())()` is put to the script editor, when the Spreadsheet is opened, `sample` and `onOpen` are run in order. So, I proposed to put `onOpen()` as the global. And, thank you for editing. I reopened it. – Tanaike Nov 08 '21 at 12:33
  • @Tanaike But `onOpen` is run twice right? **When spreadsheet is opened**, first `sample` is run -> During `sample` run, `onOpen()` is run once -> Globals are loaded and now, `onOpen()` is run again as a trigger. PS: Feel free to revert my edits as you see fit. – TheMaster Nov 08 '21 at 13:55
  • 1
    @TheMaster I think that you are correct. In the case that a line of `onOpen();` and a line of `const sample = (_ => onOpen())()` are put , both situations are the same. When Spreadsheet is opened, the function put as the global is run, and then, `function onOpen(){}` is run. By this flow, I think that `onOpen` is run 2 times. When `onOpen` is installed as the installable trigger, `onOpen` is run 4 times. When OP don't want to run 2 times when Spreadsheet is opened, for example, `function onOpen(e) {if (e) return; do something }` and `onOpen();` is used, `onOpen` is run only one time. – Tanaike Nov 08 '21 at 23:33

1 Answers1

1
  • I think that in your script when the function is run with the custom menu, the functions of test1 and test2 are not installed.

  • By executing onOpen() as the global, when the function of test1 is run from the custom menu, onOpen() is run. By this, the function test1 is installed and that function can be run.

Modified script:

//Same globals 
function onOpen(){
  //Same script in original post
}

function addToFlagInteract(flag){
  //Do something
}

onOpen(); // <--- Added
TheMaster
  • 45,448
  • 6
  • 62
  • 85
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I don't really understand what you mean by "installed". Additionally, onOpen is a global function and I can confirm that the functions test1 and test2 are also global functions by checking the debugger. – nikko.guy Nov 08 '21 at 06:50
  • @DrDaNkk Thank you for replying. I apologize for the inconvenience. I think that in your `onOpen`, the functions of `test1` and `test2` are created and put to `this` of Google Apps Script. But in your script, when `test1` is run from custom menu, the function `test1` is not put. By this, such error occurs. So I proposed above modification. By the way, I cannot understand `Additionally, onOpen is a global function and I can confirm that the functions test1 and test2 are also global functions by checking the debugger.`, can I ask you about the detail of it? – Tanaike Nov 08 '21 at 07:47