Question

I have a spreadsheet that is in a shared folder structure. This shared folder structure is shared among 3-9 people at any given time. Now the folder structure itself is constant on any of the people's Google Drive. But the "root" of the structure maybe placed differently depending on the person's preference. (I hope this makes sense.)

Example:

Folder that is shared: RootFolder
Folder structure of RootFolder: RootFolder>First_Level_Folder>Second_Level_Folder>Third_Level_Folder
Spreadsheet that this question pertains to: SS_
Where SS_ resides in: Second_Level_Folder
People that are collaborating: P1 P2 P3 P4 P5

Now, RootFolder and all its contents are shared among the people. They may place RootFolder (Shared) anywhere in their respective Google Drives.

Example:

P1: P1Root>P1First_Folder>RootFolder
P2: P2Root>RootFolder
P3: P3Root>P3First_Folder>P3Second_Folder>RootFolder
P4: P4Root>P4First_Folder>RootFolder

So if a script is attached to SS_ and it is suppose to "look for another spreadsheet" in the same folder as SS_, I need to know the full folder path of SS_. This folder path will be unique to the different people. (P1, P2, P3, etc.)

So my question is, can I find out the folder path of SS_ through script..? That way each time any person runs the script, the path will be the "correct one" for them and the rest of the process won't be hindered?

I tried to look for a getFolderPath(spreadsheet) type function, but had no luck.

Was it helpful?

Solution

You don't need the full folder path if you just need to search on the immediate parent folder of the current spreadsheet.

It's important to notice that a file on Google Drive may "be in" multiple folders. Therefore determining the full folder path is not something absolute even for a single user. Even folders can be inside multiple folders and, although I have not tested this, I think you can have a parent inside a child folder. Therefore if you try to get the full path you may end up in an endless loop if not careful to detect this cyclic situation.

Here's how you get the immediate parent folders of the current spreadsheeet:

function logParents() {
  var ss = SpreadsheetApp.getActive(); //current spreadsheet
  var directParents = DriveApp.getFileById(ss.getId()).getParents();
  while( directParents.hasNext() )
    Logger.log(directParents.next().getName());
}

On the code above I'm just logging the each parent name (if there's more than one), but to search within each parent you just have to call searchFiles instead of getName.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top