You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Script中Folder.getParents无法获取团队驱动器名称问题

Fix: Get Real Team Drive Name Instead of Default "Team Drive"

I’ve run into this exact issue before—when traversing up to the root of a Team Drive, the default folder name returned is "Team Drive" instead of the actual drive name like "Accounting" or "Marketing". The problem is that the root "folder" of a Team Drive has a generic default name, but we need to pull the name directly from the Team Drive itself, not the root folder object.

Here’s how to modify your script to get the real Team Drive name:

var path = [];
// Assuming 'id' is your document object (adjust if needed)
var folder = id.getParents(); 

while (folder && folder.hasNext()) {
  var f = folder.next();
  var folderName;
  
  // Check if this folder belongs to a Team Drive and is the root folder
  var teamDrive = f.getTeamDrive();
  if (teamDrive && f.isRoot()) {
    // Use the actual Team Drive name instead of the default "Team Drive"
    folderName = teamDrive.getName();
  } else {
    // For regular folders, use their normal display name
    folderName = f.getName();
  }
  
  path.unshift(folderName);
  folder = f.getParents();
}

// Optional: Join the path array into a single string (e.g., "Accounting/Invoices/2024")
var fullDocumentPath = path.join("/");

Key Changes Explained:

  • f.getTeamDrive(): This method returns the TeamDrive object associated with the folder (returns null if the folder isn't in a Team Drive).
  • f.isRoot(): Checks if the current folder is the root of its parent drive (for Team Drives, this will be true when we reach the top-level).
  • When both conditions are true, we replace the generic "Team Drive" string with the actual name of the Team Drive.

Notes:

  • Make sure your script has the necessary permissions to access Team Drive data—when you first run the modified script, you’ll likely need to re-authorize it with the appropriate Drive API scopes.
  • This script works for both Team Drive and regular My Drive files; it’ll fall back to normal folder names when not in a Team Drive.

内容的提问来源于stack exchange,提问作者Michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:39:01