如何在coffeescript中为Google Apps脚本生成全局的,命名的javascript函数 [英] How to generate global, named javascript functions in coffeescript, for Google Apps Script
问题描述
我想使用CoffeeScript编写 Google Apps脚本的Javascript脚本,而且我无法生成
Google Apps Script希望脚本包含顶级的命名函数。 (我可能使用错误的术语,所以我将用例子说明我的意思)
例如,这个函数被Google Apps脚本:
function triggerableFunction(){
// ...
}
...这个函数不是(它会解析,但不会你不能触发它) :
var nonTriggerableFunction;
nonTriggerableFunction = function(){
// ...
};
我发现使用CoffeeScript,最近我能得到的是 nonTriggerableFunction
表单。什么是最好的方法来生成 triggerableFunction
上面的命名函数?
我已经使用'bare'选项(
-b
开关),编译而不使用顶级函数安全包装。
我在网络上找到的结合CoffeeScript和Google App Script的一个项目是 Gmail GTD Bot ,似乎使用反向蜱的组合,并要求用户手动删除一些行从结果代码。 (请参阅脚本的结尾以及安装部分的 README )。
CoffeeScript不允许你隐式地在全局命名空间中创建任何东西;但是,您可以通过直接指定全局命名空间来实现。
window.someFunc =(someParam)
alert(someParam)
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.
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)
这篇关于如何在coffeescript中为Google Apps脚本生成全局的,命名的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!