I'm trying to build up the full path to a document in a team drive using a script. The code looks like this:
var path = [ ]
var folder = id.getParents()
while (folder && folder.hasNext()) {
var f = folder.next()
path.unshift(f.getName())
folder = f.getParents()
}
This script is bound to a document for testing.
But when I get to the root, instead of returning the actual name of the Team Drive, such as "Accounting" or "Marketing" it instead returns "Team Drive". I need to know the actual name of the Team Drive, why am I not getting this info? If I run this in a script bound to a document in My Drive, it instead says "My Drive" at the root - this at least makes sense, because that's the actual name I see in the browser. In Team Drive, the root is actually "Team Drives" not "Team Drive".
Because Team Drives are implemented differently than "regular" Google Drive "folders", the built-in DriveApp
is not guaranteed to work properly for all actions that deal with them. It is possible that at some point DriveApp
will be updated to fully support Team Drives, but there are a lot of sensible things that Google still has yet to do ;)
Instead, use the "advanced service" Drive
, which is a client application that implements version 2 of the Drive REST API, and allows properly handling Team Drive information. As an "advanced service", you must enable this service before you can use it.
To build the full path of a Team Drive item using only the advanced service:
function getTeamDrivePath(fileId) {
// Declare we know how to handle Team Drive items, and that they be included in responses.
var params = {
supportsTeamDrives: true,
includeTeamDriveItems: true
};
// Return only the fields we want, instead of the whole `File` resource.
params.fields = "id,title,parents/id"
// In a Team Drive, a file can have only one parent folder (e.g. "normal" filesystems).
// (parent.isRoot is never true for Team Drive folders so it is not used.)
var path = [], file;
do {
file = Drive.Files.get(fileId, params);
path.unshift(file.title);
fileId = file.parents.length ? file.parents[0].id : null;
} while (fileId);
// Since we also added the file, the last element of the path array is the filename.
path.pop();
// A Team Drive is subject to different permissions than files, and thus its name must be
// obtained via the Team Drives resource. Since `file` points to the Team Drive, use it:
// Requesting incorrect fields will result in an API error, so request the proper ones:
params.fields = "name"
var td = Drive.Teamdrives.get(file.id, params);
path[0] = td.name;
return path;
}
More reading about Team Drives and handling associated with them is available on the Drive REST API reference. I link the v2 versions since they are what is available via Apps Script's "Advanced Service", but the v3 version should be used for 3rd party applications using the client libraries.
Important resources:
来源:https://stackoverflow.com/questions/49542984/folder-getparents-fails-to-get-team-drive-name-in-google-script