如何在 coffeescript 中为 Google Apps Script 生成全局命名的 javascript 函数 [英] How to generate global, named javascript functions in coffeescript, for Google Apps Script

查看:12
本文介绍了如何在 coffeescript 中为 Google Apps Script 生成全局命名的 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 CoffeeScript 为 Google Apps 脚本 编写 Javascript 脚本,并且我无法以预期的形式生成函数.

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 脚本要求脚本包含顶级命名函数.(我可能使用了错误的术语,所以我会用例子来说明我的意思......)

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...)

例如,这个函数很高兴被 Google Apps Script 识别:

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() {
  // ...
};

我发现使用 CoffeeScript,我能得到的最接近的是上面的 nonTriggerableFunction 表单.生成像上面的 triggerableFunction 这样的命名函数的最佳方法是什么?

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?

我已经在使用 'bare' 选项(-b 开关)来编译没有顶级函数安全包装器.

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

我在网上找到的一个结合了 CoffeeScript 和 Google App Script 的项目是 Gmail GTDBot,它似乎使用反引号的组合来做到这一点,并要求用户从结果代码中手动删除一些行.(请参阅 脚本的结尾,以及README 的安装"部分.我希望有一个更简单、更清洁的解决方案.

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 不允许你在全局命名空间中隐式创建任何东西;但是,您可以通过直接指定全局命名空间来做到这一点.

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 Script 生成全局命名的 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆