问题描述
我正在尝试使用脚本在团队协作中建立文档的完整路径.代码如下:
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.
但是,当我扎根时,并没有返回Team Drive的实际名称,例如"Accounting"或"Marketing",而是返回了"Team Drive".我需要知道Team Drive的实际 name ,为什么我没有得到此信息?如果我在绑定到我的云端硬盘"中文档的脚本中运行此脚本,则它会在根目录中显示我的云端硬盘",这至少是有道理的,因为这是我在浏览器中看到的实际名称.在Team Drive中,根目录实际上是"Team Drives"而不是"Team Drive".
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".
推荐答案
由于团队驱动器的实现方式不同于常规" Google云端硬盘文件夹",因此不能保证内置的DriveApp
可以对所有处理他们. DriveApp
可能会在某个时候进行更新以完全支持Team Drives,但是Google仍有很多明智的事情要做;)
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 ;)
相反,请使用高级服务" Drive
,它是实现Drive REST API版本2并允许正确处理Team Drive信息的客户端应用程序.作为高级服务",您必须 启用此服务,然后再使用它.
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.
要仅使用高级服务来构建Team Drive项目的完整路径,请执行以下操作:
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;
}
Drive REST API参考中提供了有关Team Drive及其相关处理的更多信息.我链接了v2版本,因为它们可以通过Apps Script的高级服务"获得,但是v3版本应该用于使用客户端库的第三方应用程序.
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.
重要资源:
- About Team Drives
- Enabling Team Drives support
- Team Drives API Reference
- Enabling "Advanced Services" in Apps Script
- API Best Practices: Partial Resources & "fields"
这篇关于文件夹getParents无法在Google脚本中获取团队驱动器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!