Drive - get id of the current folder

369 views
Skip to first unread message

Grégory Bruneau

unread,
Apr 16, 2021, 8:59:04 AM4/16/21
to Google Apps Script Community
Hello there ! 

I'm trying to develop a complementary module for gdrive.
The idea is to create a bunch of folders and documents at the current position of the user.
Create folder and file is not an issue at all... but retrieve the current location of the user seems to be complicated...

I tried to find a method to retrieve the current folder id of the user's location but without success... do you know if this is doable ? 

Many thanks for your help ! 

Greg

Clark Lind

unread,
Apr 19, 2021, 8:32:07 AM4/19/21
to Google Apps Script Community
It depends on how you are interacting with Drive. If you are building a Workspace Addon, then there are triggers you could use since your App would be reacting to what the user is doing giving you an 'event' you could work with. 
Otherwise, there is no real event to trigger anything, so you will have to ask the user to input the top folder where they want to do the work. If no folder given, it defaults to "root" (top most 'My Drive' folder). I'll look a little further, but I don't think there is a way to see what the current active folder is.

Grégory Bruneau

unread,
Apr 19, 2021, 9:16:41 AM4/19/21
to Google Apps Script Community
Hey, thanks for your time and answer ! 
Yes it's for a workspace addon, so it is using the sidebar on the right and correctly display items depending on what's in use (like drive, spreadsheet, slides, etc).

Do you have any documentation about the trigger you're mentioning? 

best,

Clark Lind

unread,
Apr 19, 2021, 9:54:23 AM4/19/21
to google-apps-sc...@googlegroups.com
Yes, it is a contextual trigger. Take a look at the Cats Addon. You will handle the event by including it in your manifest:
example:
    "drive": {
      "onItemsSelectedTrigger": {
        "runFunction": "getFolderID"
      }
    }

Then in your code: 

function getFolderID(e) {   // "e" is the event object
  console.log(e);
  var items = e.drive.selectedItems; //this may not be needed**

//write some code to parse the items to find the folderID 

//**may be as easy as " const folderID = e.parameters.id  "

//pass the fileID to your main function and do the work :)
  return yourMainFunction(folderID);
}
Good luck!


--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/Dt6kg-_ZuVY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ba449fbc-2f48-4fca-96be-222a4d5d8a25n%40googlegroups.com.

Grégory Bruneau

unread,
Apr 19, 2021, 10:55:06 AM4/19/21
to Google Apps Script Community
many thanks ! 
I'll investigate and let you know :) 
best,

Greg

Reply all
Reply to author
Forward
0 new messages