0

How can I get the current script's id or folder id? I'd like to be able to copy a script into a folder and have it run on folders/files specific to that folder without having to find the folder id manually.

I've tried things like 'this.getParents()' but haven't gotten anywhere. Thanks in advance for your help!

nate11000
  • 247
  • 1
  • 4
  • 8

2 Answers2

0

Here is a script I wrote a while ago based on an answer from CoreyG that does precisely what you are looking for and just a bit more...

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];
}
Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Hmm. Helpful, but not exactly what I was hoping for. You'd have to go through every copy and change up the 'junk' string. Plus there seems to be a delay before it starts working. I suppose it's workable, but sort of hack-y and with a several minute delay. – nate11000 Mar 02 '14 at 23:31
  • If you have to manually hardcode the script string every time you copy the script, might as well hardcode the folder id. – Zig Mandel Mar 03 '14 at 14:14
  • Personally I don't think it's pointless :-) it returns the folder tree even if the file is moved in another place... but I admit it is not ideal in this specific use case.On the other hand, and as you posted it, there is no other way to get that Id. – Serge insas Mar 03 '14 at 16:03
0

Theres currently no way. Why dont you just make it so on first run asks the user to select the folder to run it at (filepicker) and save it on a script property?

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36