-1
  1. Can a Google Apps script learn its own name? or its id?

  2. Can it get the id of its containing folder?

  3. Can it learn where it resides in the folder hierarchy?

  4. Can script "A" get/set the properties of script "B"?

  5. Can script "A" edit any aspects of the libraries used by script "B"?

  6. Can script "A" publish script "B"?

  7. Can script "A" alter the text of script "B"?

Update 2014/10/03 16:00 EDT

  1. Can script "A" manage versions of script "B"?

  2. Can script "A" do any of 5, 6, 7 or 8 to itself?

Martin Bramwell
  • 2,003
  • 2
  • 19
  • 35
  • I just noticed I have added a few further details, but otherwise duplicated this question http://stackoverflow.com/questions/13642281/can-a-running-google-apps-script-determine-its-own-resource-id?rq=1. – Martin Bramwell Oct 03 '14 at 18:34
  • 1
    maybe a bit too much questions in a single post ? don't you think ? ;-) – Serge insas Oct 03 '14 at 19:55

2 Answers2

1

A few references to answer some of your (numerous) questions : (if I think of other answer elements, I'll update)


to get a script own url : ScriptApp.getService().getUrl()


to read the contents of a Google Apps Script from another Script : Is it possible to read the contents of a Google Apps Script from another Script

ScriptApp.getResource("Code").getDataAsString();


to locate the path of the folder in which the current script file is located :

function getScriptFolderTree() {
  var thisScript = getThisScriptInDrive();
  var names = []
  var Ids = []
  var folder = thisScript.getParents()[0];
  while (folder.getName() != "Root"){
      names.unshift(folder.getName());
      Ids.unshift(folder.getId());
    var parents = folder.getParents();
       var folder = parents[0];
  }
Logger.log('Root/'+names.join().replace(/,/g,'/'))  
Ids.unshift(DocsList.getRootFolder().getId())
Logger.log(Ids)  
}


function getThisScriptInDrive() {
  return DocsList.find("poiuytrezazertyujhgfdsdcvcxyydryfhchfh")[0];
}

Borrowed from this post (Corey G original answer + my development example)


(to be completed...)

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
1
  1. Can a Google Apps script learn its own name? or its id?

Not as such. Common practice is to create a global constant string in the script containing its own ID. With the ID, you can get the script file name with

var SCRIPT_ID = 'abcdefghijklmnopqrstuvwxyz0123456789'; // Constant
...
var scriptFileName = DriveApp.getFileById(SCRIPT_ID).getName();

Conversely, if you have the file name of the script you can get its ID by using DriveApp.getFilesByName(name) then calling getId() on the file objects. However, since more than one file can can have the same name you will need to handle the possibility of duplicates.

Note that ScriptApp.getService().getUrl() will return null unless the script is deployed as a web app, in which case it will return the URL for the web app interface, not the script file itself.

  1. Can it get the id of its containing folder?

Yes. However, it is important to remember that files in Drive may be placed in multiple folders and thus have multiple parents.

function getScriptParent() {
  var thisScript = DriveApp.getFileById(myId);
  var parents = thisScript.getParents();
  while (parents.hasNext()) {
    var folder = parents.next();
    Logger.log(folder.getId());
  }
}
  1. Can it learn where it resides in the folder hierarchy?

It's possible to trace a file's path back to the root by finding the parents of each ancestor folder. However, again, each file and folder in Drive can have multiple parents, so finding paths isn't straightforward. If you only one a single path that is valid, you can just take the first parent of each file/folder and step backward. If you want all the valid paths, then you will probably want to implement a BFS or DFS to find and record them all.

  1. Can script "A" get/set the properties of script "B"?

There is no way to access the ScriptProperties of a script from another script. If you want to send data between scripts, the best option is to use an external data store of some sort, such as a SQL database accessed via JDBC.

  1. Can script "A" edit any aspects of the libraries used by script "B"?

If you are asking whether script A can change which libraries are included by script B, then no, that is not possible. Script B cannot programmatically change which libraries it uses either -- those selections have to be made in the script editor interface.

  1. Can script "A" publish script "B"?

No.

  1. Can script "A" alter the text of script "B"?

No.

  1. Can script "A" manage versions of script "B"?

No.

  1. Can script "A" do any of 5, 6, 7 or 8 to itself?

Mostly no -- only web app depolyment can be programmatically controlled. A script can manage its own deployment as a web app via the ScriptApp.Service methods. See the ScriptApp.Service.enable(restriction) method for more information.

Ryan Roth
  • 1,394
  • 9
  • 15
  • A really great detailed answer. Thank you. I asked the question after exhausting every avenue to find a way to control scripts from scripts. So you and Serge have confirmed my understanding. Thank you for going to the effort of spelling it all out. – Martin Bramwell Oct 21 '14 at 10:48