Search code examples
google-apps-scriptintrospection

Do Google Apps scripts permit any sort of introspection?


  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?


Solution

    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.