gpt4 book ai didi

google-apps-script - 文件夹 getParents 无法在 Google 脚本中获取团队云端硬盘名称

转载 作者:行者123 更新时间:2023-12-02 20:27:41 25 4
gpt4 key购买 nike

我正在尝试使用脚本在团队驱动器中构建文档的完整路径。代码如下所示:

var path = [ ]
var folder = id.getParents()
while (folder && folder.hasNext()) {
var f = folder.next()
path.unshift(f.getName())
folder = f.getParents()
}

此脚本绑定(bind)到文档以进行测试。

但是当我到达根目录时,它没有返回团队云端硬盘的实际名称,例如“会计”或“营销”,而是返回“团队云端硬盘”。我需要知道团队云端硬盘的实际名称,为什么我没有得到此信息?如果我在绑定(bind)到“我的云端硬盘”中文档的脚本中运行此命令,它会在根目录中显示“我的云端硬盘” - 这至少是有意义的,因为这是我在浏览器中看到的实际名称。在团队云端硬盘中,根实际上是“团队云端硬盘”而不是“团队云端硬盘”。

最佳答案

由于团队云端硬盘的实现方式与“常规”Google 云端硬盘“文件夹”不同,因此无法保证内置 DriveApp 能够正常处理处理它们的所有操作。 DriveApp 可能会在某个时候进行更新以完全支持团队云端硬盘,但 Google 仍有很多明智的事情尚未完成;)

相反,请使用“高级服务”Drive,它是一个客户端应用程序,实现 Drive REST API 版本 2,并允许正确处理 Team Drive 信息。作为“高级服务”,您必须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 引用。我链接了 v2 版本,因为它们是通过 Apps 脚本的“高级服务”提供的,但 v3 版本应该用于使用客户端库的第三方应用程序。

重要资源:

关于google-apps-script - 文件夹 getParents 无法在 Google 脚本中获取团队云端硬盘名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49542984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com