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