Search code examples
javascriptgoogle-apps-scriptgoogle-sheetscustom-function

IEFE (immediately executed function expression) in Google Spreadsheet Script Editor


I tried to define some IEFE style Javascript functions in Google Spreadsheet's Script Editor (Tools > Script Editor):

function sayHello() {
  return "HELLO";
}

var World = (function () {
  return {
    'say': function() {
      return "WORLD";
    }
  };
}();

So in a cell, =sayHello() will work, but not =World.say(). I want to use IEFE style because I would like to namespace my various JS functions for better management and maintainability.

Is this possible? I tried other IEFE ways to define functions but still failed.

TIA.


Solution

  • This is not possible. As written in the documentation

    The name of a custom function must be declared with the syntax function myFunction(), not var myFunction = new Function().

    So, IIFE/anonymous functions and functions inside objects cannot be used as custom functions.