由于团队云端硬盘的实现方式与“常规”Google 云端硬盘“文件夹”不同,因此无法保证内置的DriveApp
所有操作都能正常工作。可能会在某个时候DriveApp
更新以完全支持 Team Drives,但 Google 仍有许多明智的事情要做;)
相反,请使用“高级服务” Drive
,这是一个客户端应用程序,它实现了 Drive REST API 的第 2 版,并允许正确处理 Team Drive 信息。作为“高级服务”,您必须先 启用该服务,然后才能使用它。
要仅使用高级服务构建团队云端硬盘项目的完整路径:
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;
}
Drive REST API 参考中提供了有关团队驱动器及其相关处理的更多信息。我链接了 v2 版本,因为它们可以通过 Apps Script 的“高级服务”获得,但 v3 版本应该用于使用客户端库的 3rd 方应用程序。
重要资源: