Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a namespace-like organization in a google apps script library

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.

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

like image 399
M. Oranje Avatar asked Jun 05 '12 00:06

M. Oranje


People also ask

What is SS in Google script?

var ss = SpreadsheetApp. This line retrieves the current spreadsheet that's active. Since you're only running this script when the spreadsheet you want to run the calculation on is active, it'll always get the correct spreadsheet. The sheet gets saved as an “object” variable called “ss”.

Is Google Apps Script an IDE?

In December 2020, we announced an updated and improved experience for the Apps Script integrated development environment (IDE). Since then, it has been possible to access the legacy IDE. By the end of Q3 2022, we will begin turning down the legacy experience.

Is Google Apps Script asynchronous?

google. script. run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions. To interact with dialogs or sidebars in Google Docs, Sheets, or Forms from client-side code, use google.


1 Answers

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) {}
like image 161
Doxument Team Avatar answered Oct 29 '22 06:10

Doxument Team