How do I locate the path of the folder in which the current script file is located?

后端 未结 5 856
小鲜肉
小鲜肉 2020-12-21 09:46

How do I locate the path of the current folder? I just want to be able to get the path of the folder so that I can manipulate the files in the folder without typing the path

5条回答
  •  礼貌的吻别
    2020-12-21 10:20

    Thanks to Corey's answer and to Thomas'one, here is a "full featured" version that shows the folder tree in the logger and every parents id as well... just for fun ;-)

    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(DriveApp.getRootFolder().getId())
    Logger.log(Ids)  
    }
    
    
    function getThisScriptInDrive() {
      return DriveApp.getFileById("poiuytrezazertyujhgfdsdcvcxyydryfhchfh");
    }
    

    enter image description here

    (ID's are truncated intentionally)

    Note that this script is working nicely but it strangely stops working if the 'random string' is modified... I imagine that the search engine in drive doesn't like the change but I have no serious explanation (comments welcome) but after a few minutes it works again ;-)

提交回复
热议问题