In a script bound to a Google Sheet, how can I learn the ID (not the name) of the folder containing the current sheet? I want to do this:
var files = DriveApp.getFolderById('0B9momRZYw4DVTlJVeXNpX25BemM').getFiles();
but using a variable that represents the folder ID, like this:
var files = DriveApp.getFolderById(folderId).getFiles();
The problem is I cannot figure out how to obtain the parent folder's ID. I realize the current sheet may reside in several different folders (mine doesn't), but surely there is some way to get the ID of the folder?
This is quite straightforward using DriveApp getParents()
method.
Code goes like this :
function getParentFolder(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var file = DriveApp.getFileById(ss.getId());
var folders = file.getParents();
while (folders.hasNext()){
Logger.log('folder name = '+folders.next().getName());
}
}
Use folders.next().getId()
to get the folder(s) ID(s).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With