Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsshared-librariesversion

How to retrieve google apps script library version number and note programmatically?


I want to get the Google apps script library details such as version number and note programmatically in a spreadsheet cell.


Solution

  • You can use the projects.versions.list method.

    You need to specify the script ID of the library. If you have the link of the library you can find the script id in the url or in the project settings.

    If they are not automatically added, you might have to add the scopes manually in the appsscript.json file. E.g. add this inside the brackets:

    "oauthScopes": [
          "https://www.googleapis.com/auth/script.projects",
          "https://www.googleapis.com/auth/script.projects.readonly",
          "https://www.googleapis.com/auth/script.external_request"
        ],
    

    Example:

    function getVersion(){
    
        const AccessToken = ScriptApp.getOAuthToken();
        const scriptID = "scriptID" // put the script id of the library
        const url = "https://script.googleapis.com/v1/projects/" + scriptID + "/versions";
    
      options = {
        "method" : "GET",
        "muteHttpExceptions": true,
        "headers": {
          'Authorization': 'Bearer ' +  AccessToken
        }
      };
    
      response = UrlFetchApp.fetch(url,options);
      response = JSON.parse(response);
      allVersions = response.versions; //get all versions
      highestVersion = allVersions[0].versionNumber;
      console.log(response);
      console.log(highestVersion);
    }