文件夹getParents无法在Google Script中获取Team Drive名称

时间:2018-03-28 19:49:07

标签: google-apps-script google-drive-api google-drive-team-drive

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

db.test.updateMany(
    { "array.name": "test1a" }, 
    { "$set": { "array.$[elem].quantity": "newvalue" } },
    { "arrayFilters": [{ "elem.name": "test1a" }] }
)

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

但是当我到达root时,而不是返回Team Drive的实际名称,例如" Accounting"或"营销"它改为" Team Drive"。我需要知道Team Drive的实际名称,为什么我没有收到此信息?如果我在绑定到“我的云端硬盘”中的文档的脚本中运行此操作,则会显示"我的云端硬盘"在根目录 - 这至少是有道理的,因为这是我在浏览器中看到的实际名称。在Team Drive中,根实际上是" Team Drives"不是" Team Drive"。

1 个答案:

答案 0 :(得分:3)

由于Team Drives的实现方式与“常规”Google云端硬盘“文件夹”不同,因此无法保证内置DriveApp能够正常处理所有处理它们的操作。有时可能会更新DriveApp以完全支持Team Drives,但Google仍有许多明智的事情要做;)

相反,请使用“高级服务”Drive,它是实现Drive REST API第2版的客户端应用程序,并允许正确处理Team Drive信息。作为“高级服务”,您必须 enable this service才能使用它。

仅使用高级服务构建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参考中提供了有关Team Drives及其相关处理的更多信息。我链接v2版本,因为它们是通过Apps Script的“高级服务”提供的,但v3版本应该用于使用客户端库的第三方应用程序。

重要资源: