JLCJ
JLCJ

Reputation: 41

how to bind the version of the apps script in a variable?

I would like to know if it is possible to bind the version of the apps script, the version we have in 'manage version' menu, in a variable. Then I will be able to display on UI.

Thanks a lot,

Upvotes: 0

Views: 117

Answers (2)

Alan Wells
Alan Wells

Reputation: 31300

You'll need to use the Apps Script REST API to get the project's version number.

Projects Versions List

function getProjectVersionNumber(scriptId,theAccessTkn) {
try{
  var allVersions,errMsg,highestVersion,options,payload,response,url;

  if (!scriptId) {
    //Logger.log('There was an error - No scriptID')
    //Error handling function
    throw new Error('There is no script ID for fnk getProjectVersionNumber');
  }

  if (!theAccessTkn) {
    theAccessTkn = ScriptApp.getOAuthToken();
  }

  url = "https://script.googleapis.com/v1/projects/" + scriptId + "/versions";

  options = {
    "method" : "GET",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  theAccessTkn
    }
  };

  response = UrlFetchApp.fetch(url,options);
  //Logger.log('response : ' + JSON.stringify(response).slice(0,500));

  response = JSON.parse(response);//The response must be parsed into JSON even though it is an object

  if (typeof response === 'object') {
    errMsg = response.error;
    if (errMsg) {
      errMsg = errMsg.message;
      return 'err' + errMsg;
    }
  }

  allVersions = response.versions;//Get versions

  highestVersion = allVersions[0].versionNumber;
  //Logger.log("highestVersion: " + highestVersion)

  return highestVersion;
}catch(e) {
  myErrorHandlingFunction_(e);
}
}

Upvotes: 2

yuri
yuri

Reputation: 3400

This is been a featured request for a long time, as now it is possible to retrieve the version history and the current version using the API, on the following reference you'll find the API calls:

https://developers.google.com/apps-script/api/reference/rest

Although it's not possible to enable this API as an advanced service, so you'll have to recreate the oauth2 authentication process using apps script.

As the documentation says:

If you want to use a Google API that isn't available as an advanced service, just connect to it like any other external API.

Upvotes: 2

Related Questions