Get GAS library info

There are many references to shared libraries on this site. You can either take a copy of them or use them in place as you wish. I'm always hitting a few problems with a) versions b) keys

Versions

In Google Apps Script, in theory, if you use development mode, it will use the latest code in your library. If you turn it off, it will use whatever version of the library you choose. If there are other libraries linked to that library, then the same rules should apply. This makes it very hard to orchestrate shared libraries with lots of shared dependencies, when people can choose which ones they want to use (various version may work together, other may not). 

I usually keep everything in development mode so it always picks up the latest versions until I'm ready to release, but when someone other than me is using one of my libraries, they only get the selected library version. All this gets fairly complicated when you have many libraries.

Library info

Here's a way of finding out which versions of libraries are actually being used. 

You'll find this button appearing on some of my pages. It will return JSON describing the libraries and dependency info of all my public libraries.

https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec

In each of your libraries, add this function, substituting your dependent libraries if any, along with the library key, name and version. You can update that as you create new versions or additional dependencies. Since each of your dependent libraries will have this function too (as will the dependents of their dependents etc..), you'll return a single object with info about all dependencies of all libraries as well as the versions currently being referenced.  

function getLibraryInfo () {
  return {
    info: {
      name:'cDriverSheet',
      version:'2.00',
      key:'Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j',
    },
    dependencies:[
      cDriverMemory.getLibraryInfo(),
      cFlatten.getLibraryInfo(),
      cNamedLock.getLibraryInfo()
    ]
  }; 
}

Use it like this to get the profile of a list of libraries.

function log () {
  Logger.log (JSON.stringify(getLibraryData()));
}

function getLibraryData () {

  return getAllLibraries ( [
      cDbAbstraction,
      cDriverDataStore
    ]);
}

function getAllLibraries (libraries) {

  var result = [];
  libraries.forEach ( function (l) {
    result.push ( l.getLibraryInfo() );
  });
  
  return result;
  
}

You'll get this

[
    {
        "info": {
            "name": "cDbAbstraction",
            "version": "2.00",
            "key": "MHfCjPQlweartW45xYs6hFai_d-phDA33"
        },
        "dependencies": [
            {
                "info": {
                    "name": "cCacheHandler",
                    "version": "2.00",
                    "key": "M3reA5eBxtwxSqCEgPywb9ai_d-phDA33"
                },
                "dependencies": []
            },
            {
                "info": {
                    "name": "cUseful",
                    "version": "2.00",
                    "key": "Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j"
                },
                "dependencies": []
            },
            {
                "info": {
                    "name": "cNamedLock",
                    "version": "2.00",
                    "key": "Mpv7vUR0126U53sfSMXsAPai_d-phDA33"
                },
                "dependencies": [
                    {
                        "info": {
                            "name": "cCacheHandler",
                            "version": "2.00",
                            "key": "M3reA5eBxtwxSqCEgPywb9ai_d-phDA33"
                        },
                        "dependencies": []
                    }
                ]
            },
            {
                "info": {
                    "name": "cFlatten",
                    "version": "2.00",
                    "key": "MqxKdBrlw18FDd-X5zQLd7yz3TLx7pV4j"
                },
                "dependencies": []
            },
            {
                "info": {
                    "name": "cUAMeasure",
                    "version": "2.00",
                    "key": "MIHfxr-fc_7bXa1l0Dkk0oqi_d-phDA33"
                },
                "dependencies": []
            }
        ]
    },
    {
        "info": {
            "name": "cDriverDataStore",
            "version": "2.00",
            "key": "MPZF_EC6nOZFAjMRqCxEaUyz3TLx7pV4j"
        },
        "dependencies": [
            {
                "info": {
                    "name": "cNamedLock",
                    "version": "2.00",
                    "key": "Mpv7vUR0126U53sfSMXsAPai_d-phDA33"
                },
                "dependencies": [
                    {
                        "info": {
                            "name": "cCacheHandler",
                            "version": "2.00",
                            "key": "M3reA5eBxtwxSqCEgPywb9ai_d-phDA33"
                        },
                        "dependencies": []
                    }
                ]
            },
            {
                "info": {
                    "name": "cFlatten",
                    "version": "2.00",
                    "key": "MqxKdBrlw18FDd-X5zQLd7yz3TLx7pV4j"
                },
                "dependencies": []
            }
        ]
    }
]

Click on this https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec to get all keys and dependencies from the majority of my public libraries. It will return JSON, or JSONP if you include a callback parameter. Here's the code if you want to create your own. 

you can also provide an optional parameter ?list=lib1,lib2,lib3 to get dependencies for specific libraries.

The web app url is https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec

/**
 * used to return all known keys and dependencies for public library
 * parameter is callback to get jsonp
 * and optional list = list",... to get a list of libraries
 **/
function test() {
  Logger.log(getLibraryData());
  Logger.log(getLibraryData({parameter:{list:"cDogger,cFlatten"}}));
}
function doGet(e) {

  e = e || {parameter:{}};
  // do whatever this webapp is for
  var result = getLibraryData(e); 
  // prepare the result
  var s = JSON.stringify(result);
  // publish result
  return ContentService
    .createTextOutput(e.parameter.callback ? e.parameter.callback + "(" + s + ")" : s )
    .setMimeType(e.parameter.callback ? ContentService.MimeType.JAVASCRIPT : ContentService.MimeType.JSON); 
}


function getLibraryData (e) {
 
  return e && e.parameter && e.parameter.list ?
      getAllLibraries (e.parameter.list.split(",").map(function(d) {
        return d;
      }))
    : getAllLibraries ( [
      "cDbAbstraction",
      "cDriverOrchestrate",
      "cDriverParse",
      "cDriverFusion",
      "cDriverDrive",
      "cDriverImportio",
      "cDriverDataStore",
      "cDriverSheet",
      "cDriverScriptDB",
      "cDriverMemory",
      "cDriverProperties",
      "cDriverMongoLab",
      "cParseCom",
      "cFlatten",
      "cUAMeasure",
      "cNamedLock",
      "cCacheHandler",
      "cDataHandler",
      "cDogger",
      "yourDoggerServer",
      "doggerDemo"
    ]);
}

function getAllLibraries (libraries) {

  var result = [];
  libraries.forEach ( function (l) {
    result.push ( eval(l+".getLibraryInfo();"));
  });
  
  return result;
  
}

Try this link to get the dependencies of a couple of libraries

https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec?list=cDogger,cDriverMemory

For more snippets like this see Google Apps Scripts snippets

For help and more information join our forumfollow the blogfollow me on twitter

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments