Can a Google Sheets custom menu pass a variable to function?

后端 未结 3 1091
失恋的感觉
失恋的感觉 2020-12-17 01:24

I\'m creating a menu item in Google Sheets that will list a work directory and pull the email address for that user, to be later used to process employee records within Goog

3条回答
  •  臣服心动
    2020-12-17 02:03

    Matthew Wolman's solution did not work in google sheets. Everything works at create time, typeof sees function. However not sure if it goes out of scope or there's another security policy in place, but when you invokve the menu with a dynamic function it gives error:

      Execution failed: Script function not found: 'dynfunc-item1'
    

    Was able to get dynamic menus working in using globally scoped function defs.

    Main difference between Matthew Wolman's answer and what worked is this bit:

    /* generate all menuFunctions in immediatelly called closure */
    (function (globalScope) {
    for (var i = 0, l = items.length; i < l; i++)
        createMenuFunction(items[i])
    
    // you need to generate menuFunction via another function,
    // because that way you generate scope to keep param unchanged.
    // If you'll generate menuFunction right inside for loop,
    // all your menuFunctions will refer to same instance of var i,
    // which will be undefined after this closure terminates.
    function createMenuFunction(param) {
        // here we define new function inside global scope
        globalScope[MENU_ITEM_FUNCTION_PREFIX + param] = function() {
           alertNum(param);
        }
    }
    })(this) // here we passing global scope and call the closure
    

提交回复
热议问题