How to generate global, named javascript functions in coffeescript, for Google Apps Script

前端 未结 4 868
自闭症患者
自闭症患者 2020-12-20 15:39

I\'d like to write Javascript scripts for Google Apps Script using CoffeeScript, and I\'m having trouble generating functions in the expected form.

Google Apps Scrip

相关标签:
4条回答
  • 2020-12-20 15:52

    This should give you a global named function (yes, it's a little hacky, but far less that using backticks):

    # wrap in a self invoking function to capture global context
    do ->
      # use a class to create named function
      class @triggerableFunction
        # the constructor is invoked at instantiation, this should be the function body
        constructor: (arg1, arg2) ->
          # whatever
    
    0 讨论(0)
  • 2020-12-20 15:58

    CoffeeScript does not allow you to create anything in the global namespace implicitly; but, you can do this by directly specifying the global namespace.

    window.someFunc = (someParam) -> 
        alert(someParam)
    
    0 讨论(0)
  • 2020-12-20 15:58

    Turns out this can be done using a single line of embedded Javascript for each function.

    E.g. this CoffeeScript:

    myNonTriggerableFunction = ->
      Logger.log("Hello World!")
    
    `function myTriggerableFunction() { myNonTriggerableFunction(); }`
    

    ... will produce this JavaScript, when invoking the coffee compiler with the 'bare' option (the -b switch):

    var myNonTriggerableFunction;
    
    myNonTriggerableFunction = function() {
      return Logger.log("Hello World!");
    };
    
    function myTriggerableFunction() { myNonTriggerableFunction(); };
    

    With the example above, Google Apps Script is able to trigger myTriggerableFunction directly.

    0 讨论(0)
  • 2020-12-20 16:05

    juste use @ in script, exemple of my code :

    @isArray = (o)->
      Array.isArray(o)
    

    it will be compiled in :

    (function() {
    
      this.isArray = function(o) {
        return Array.isArray(o);
      };
    
    }).call(this);
    

    this = window in this case, so it's global function

    0 讨论(0)
提交回复
热议问题