在Google Apps脚本库中创建类似命名空间的组织

时间:2012-06-05 00:34:44

标签: google-apps-script namespaces jsdoc

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

我的想法虽然更像是Utils.Stopwatch()。start()和Utils.Timer.start()等。我认为它肯定可能在javascript中,但为了不断破坏Apps Script自动完成功能它需要以某种格式添加。下面是一个例子,它做错了(给出错误),但也许节省了一些时间。它基于this文章。

/**
* 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);
      }
    };
}

由于

4 个答案:

答案 0 :(得分:3)

您可以在此处提交功能请求:

http://code.google.com/p/google-apps-script-issues/issues/list

答案 1 :(得分:3)

在Google本机支持此类功能之前,您可以使用与构造函数相同级别的注释定义空函数。您甚至可以保留原始代码结构。这将在编辑器中启用自动完成。另外,您将获得自动生成的库文档,例如: https://script.google.com/macros/library/versions/d/YOUR_PROJECT_KEY

示例:

/**
* 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) {}

答案 2 :(得分:2)

它还没有用,但团队确实知道它。在此之前,您需要在站点上记录您的库。我猜你也可以把这些方法放在描述中。对于新服务来说,这真的是一个很好的开始,但我和你在一起大约5分钟,并且已经想要更多了。 ;)

答案 3 :(得分:0)

使用OO怎么样?

它看起来非常有条理,而且很容易记录。

/**
 * A stopwatch Class.
 *
 * @constructor
 */
function Stopwatch() {
  this.current =  new Date().getTime();
}

/**
 * Starts the stopwatch
 */
Stopwatch.prototype.start = function(){
  this.current = new Date().getTime();
};

/**
 * Stops the stopwatch
 *
 * @return {number} Number of seconds since Stopwatch was started
 */
Stopwatch.prototype.stop = function(){
  return ((new Date().getTime()) - this.current) / 1000;
};

实施例。如果您的lib导入为Utils

var s = new Utils.Stopwatch();
s.start();

// (...) 

Logger.log(s.stop());

PS:这是未经测试的代码。

作为一个副作用,您可以拥有多个秒表,每个秒表都维护它自己的局部变量current

<强>更新

虽然这是在JSDoc之后正确记录的,但它目前没有使用Google Apps脚本自动填充方法。

相关问题