文件夹 getParents 无法在 Google 脚本中获取团队驱动器名称
Folder getParents fails to get Team Drive name in Google Script
我正在尝试使用脚本建立团队驱动器中文档的完整路径。代码如下所示:
var path = [ ]
var folder = id.getParents()
while (folder && folder.hasNext()) {
var f = folder.next()
path.unshift(f.getName())
folder = f.getParents()
}
此脚本绑定到文档进行测试。
但是当我到达根目录时,它没有返回团队驱动器的实际名称,例如 "Accounting" 或 "Marketing",而是 returns "Team Drive"。我需要知道 Team Drive 的实际 name,为什么我没有收到此信息?如果我在绑定到“我的云端硬盘”中文档的脚本中 运行 this,它会在根目录中显示 "My Drive" - 这至少是有道理的,因为这是我在浏览器中看到的实际名称。在 Team Drive 中,root 实际上是 "Team Drives" 而不是 "Team Drive".
由于团队云端硬盘的实施方式不同于 "regular" Google 云端硬盘 "folders",因此无法保证 built-in DriveApp
对付他们。 DriveApp
可能会在某个时候更新以完全支持 Team Drives,但还有很多明智的事情 Google 还没有做 ;)
相反,请使用 "advanced service" Drive
,这是一个实现 Drive REST API 版本 2 的客户端应用程序,并允许正确处理 Team Drive 信息。作为 "advanced service",您 必须 enable this 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;
}
Drive REST API 参考资料中提供了有关团队云端硬盘及其相关处理的更多信息。我 link v2 版本,因为它们可以通过 Apps Script "Advanced Service" 获得,但 v3 版本应该用于使用客户端库的第 3 方应用程序。
重要资源:
我正在尝试使用脚本建立团队驱动器中文档的完整路径。代码如下所示:
var path = [ ]
var folder = id.getParents()
while (folder && folder.hasNext()) {
var f = folder.next()
path.unshift(f.getName())
folder = f.getParents()
}
此脚本绑定到文档进行测试。
但是当我到达根目录时,它没有返回团队驱动器的实际名称,例如 "Accounting" 或 "Marketing",而是 returns "Team Drive"。我需要知道 Team Drive 的实际 name,为什么我没有收到此信息?如果我在绑定到“我的云端硬盘”中文档的脚本中 运行 this,它会在根目录中显示 "My Drive" - 这至少是有道理的,因为这是我在浏览器中看到的实际名称。在 Team Drive 中,root 实际上是 "Team Drives" 而不是 "Team Drive".
由于团队云端硬盘的实施方式不同于 "regular" Google 云端硬盘 "folders",因此无法保证 built-in DriveApp
对付他们。 DriveApp
可能会在某个时候更新以完全支持 Team Drives,但还有很多明智的事情 Google 还没有做 ;)
相反,请使用 "advanced service" Drive
,这是一个实现 Drive REST API 版本 2 的客户端应用程序,并允许正确处理 Team Drive 信息。作为 "advanced service",您 必须 enable this 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;
}
Drive REST API 参考资料中提供了有关团队云端硬盘及其相关处理的更多信息。我 link v2 版本,因为它们可以通过 Apps Script "Advanced Service" 获得,但 v3 版本应该用于使用客户端库的第 3 方应用程序。
重要资源: