Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Script expects a script to contain top-level, named functions. (I may be using the wrong terminology, so I'll illustrate what I mean with examples...)

For example, this function is happily recognised by Google Apps Script:

function triggerableFunction() {
   // ...
}

... while this function is not (it will parse, but won't you won't be able to trigger it):

var nonTriggerableFunction;

nonTriggerableFunction = function() {
  // ...
};

I've found that with CoffeeScript, the closest I'm able to get is the nonTriggerableFunction form above. What's the best approach to generating a named function like triggerableFunction above?

I'm already using the 'bare' option (the -b switch), to compile without the top-level function safety wrapper.

The one project I've found on the web which combines CoffeeScript and Google App Script is Gmail GTD Bot, which appears to do this using a combination of back-ticks, and by asking the user to manually remove some lines from the resulting code. (See the end of the script, and the 'Installation' section of the README). I'm hoping for a simpler and cleaner solution.

like image 284
mattbh Avatar asked Jan 30 '12 04:01

mattbh


People also ask

How do you make a function in CoffeeScript?

Functions in CoffeeScript To define a function here, we have to use a thin arrow (->). Behind the scenes, the CoffeeScript compiler converts the arrow in to the function definition in JavaScript as shown below. (function() {}); It is not mandatory to use the return keyword in CoffeeScript.

What is-> in CoffeeScript?

In CoffeeScript there are two different types of arrows for defining functions: thin arrows -> and fat arrows =>. The JavaScript function keyword was replaced with the thin arrow. The fat arrow serves as the function keyword and also binds the function to the current context.

How do I use CoffeeScript in HTML?

You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.


2 Answers

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)
like image 81
Andrew Kovalenko Avatar answered Oct 19 '22 10:10

Andrew Kovalenko


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.

like image 32
mattbh Avatar answered Oct 19 '22 11:10

mattbh