由于团队云端硬盘的实现方式与“常规”Google 云端硬盘“文件夹”不同,因此内置DriveApp
不保证处理它们的所有操作都能正常工作。有可能在某个时刻DriveApp
将进行更新以完全支持 Team Drives,但 Google 仍有很多明智的事情尚未完成;)
相反,请使用“高级服务”Drive
,这是一个实现 Drive REST API 版本 2 的客户端应用程序,并允许正确处理 Team Drive 信息。作为“高级服务”,您must 启用此服务在你可以使用它之前。
要仅使用高级服务构建团队云端硬盘项目的完整路径:
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;
}
有关团队驱动器以及与其相关的处理的更多信息,请参阅驱动器 REST API 参考。我链接了 v2 版本,因为它们是通过 Apps 脚本的“高级服务”提供的,但 v3 版本应该用于使用客户端库的第三方应用程序。
重要资源:
- 关于团队驱动器
- 启用团队云端硬盘支持
- 团队驱动 API 参考
- 在 Apps 脚本中启用“高级服务”
- API 最佳实践:部分资源和“字段”