在谷歌应用脚​​本库中创建一个名称空间组织 [英] Creating a namespace-like organization in a google apps script library

查看:90
本文介绍了在谷歌应用脚​​本库中创建一个名称空间组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用谷歌应用脚​​本构建工具集。这个问题是,据我所知,只允许一级组织。您可以创建一个名为Stopwatch的库,并调用方法Stopwatch.start()和Stopwatch.stop(),这非常酷。

I was looking into building a toolset using google apps script. The problem with this is that as far as I can tell in only allows one level of organization. You can create a Library called Stopwatch and call methods Stopwatch.start() and Stopwatch.stop() which is pretty cool.

我想到的是更多像Utils.Stopwatch()。start()和Utils.Timer.start()等等。我认为这当然可以在JavaScript中使用,但为了不断破坏Apps Script自动完成功能,它需要以特定的格式添加。下面是一个例子,它做错了(给出错误),但可能会节省一些时间。它基于这篇文章。

What I had in mind though was something more like Utils.Stopwatch().start() and Utils.Timer.start() etc. I think it's certainly possible in javascript, but in order to keep breaking Apps Script autocomplete function it needs to be added in a certain format. Below is example an example of doing it wrong (gives an error) but perhaps saves some time. It's based on this article.

/**
* A stopwatch object.
* @return {Object} The stopwatch methods
*/
function Stopwatch() 
{
  var current;

  /**
  * Stop the stopwatch.
  * @param {Time} time in miliseconds
  */
  function timeInSeconds_(time)
  {
    return time/1000;
  }

  return 
    {
      /**
      * Start the stopwatch.
      */
      start: function() 
      {
        var time = new Date().getTime();
        current = timeInSeconds_(time);
      },

      /**
      * Stop the stopwatch.
      * @return {decimal} time passed since calling 
      *    start (in seconds)
      */
      stop: function() 
      {
        var time = new Date().getTime();
        var difference = timeInSeconds_(time) - this.current;
        return difference.toFixed(3);
      }
    };
}

谢谢

Thanks

推荐答案

除非谷歌本身支持这样的功能,否则可以使用与构造函数相同级别的注释定义空函数。你甚至可以保持你原来的代码结构。这将在编辑器中启用自动完成。另外,您还可以为自己的图书馆获取自动生成的文档,例如 https://script.google.com/macros/library/versions/d/YOUR_PROJECT_KEY

Until such functionality is natively supported by Google you can define empty functions with annotations on the same level as your constructor function. You can even keep your original code structure. This would enable auto-complete in the editor. Plus you'll get auto-generated documentation for your library, e.g. https://script.google.com/macros/library/versions/d/YOUR_PROJECT_KEY

示例:

Example:

/**
* Constructor.
* @constructor
* @param {String} apiKey Doxument API key
* @param {String} apiToken Doxument API token
*/    
function DoxumentApi(apiKey, apiToken) {
  // public api
  return {
        get: function(id, params) {
          var httpResponse = execute('/docs/' + id + '.json?' + buildQuery(params));
          return parse(httpResponse);
        }
    }
}

/**
* Get document record.
* @param {String} id document id
* @param {Object=} params optional. extra get params
* @return {Object} Document object
*/    
function get(id, params) {}

这篇关于在谷歌应用脚​​本库中创建一个名称空间组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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