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

解决Google Apps Script获取共享Drive文件夹列表时的gobj未定义错误

问题解决:ReferenceError: gobj is not defined 及脚本优化

1. 直接报错原因修复

报错行let folder = DriveApp.getFolderById(gobj.globals.testfolderid);中的gobj是原脚本作者自定义的全局对象,你未定义该对象,因此直接替换为你需要保存文件的目标文件夹ID即可:

let folder = DriveApp.getFolderById("你的目标文件夹ID");

如果不需要将结果保存到Drive,也可以直接删除这两行代码。

2. 其他必改问题

(1)启用Drive API

确保已在Google Apps Script编辑器中启用Drive API:

  • 打开脚本编辑器 → 左侧菜单栏选择「服务」→ 点击「添加服务」→ 选择「Drive API」并添加

(2)修复Drive.Files.list调用错误

Drive.Files.list返回的已是JavaScript对象,无需JSON.parse(r),直接使用即可:

let r = Drive.Files.list({ 
  corpora: 'drive', 
  includeItemsFromAllDrives: true, 
  supportsTeamDrive: true, 
  supportsAllDrives: true, 
  driveId: "你的共享Drive ID", // 替换为你的共享Drive ID
  pageToken: token,
  q: "mimeType = 'application/vnd.google-apps.folder'" 
});
tr.push(r); // 直接push返回对象,无需JSON.parse
token = r.nextPageToken;

(3)修正条件判断的赋值错误

原代码中if (item.mimeType = "application/vnd.google-apps.folder")是赋值操作,需改为全等判断:

if (item.mimeType === "application/vnd.google-apps.folder")

(4)补充缺失的getPathAllDrivesFromId函数

原脚本调用了未定义的getPathAllDrivesFromId函数,需添加以下代码来获取文件夹完整路径:

function getPathAllDrivesFromId(fileId) {
  let path = [];
  let file = Drive.Files.get(fileId, { supportsAllDrives: true });
  path.push(file.title);
  while (file.parents && file.parents.length > 0) {
    file = Drive.Files.get(file.parents[0].id, { supportsAllDrives: true });
    path.unshift(file.title);
  }
  return path.join("/");
}

(5)替换对话框标题占位符

原代码中JSON.parse(Drive.Drives.get("driveid")).name里的"driveid"需替换为你的共享Drive ID,且无需JSON.parse:

`Folders in: ${Drive.Drives.get("你的共享Drive ID").name}`

3. 完整修正后的脚本

function getFoldersInASharedFolder() {
  const SHARED_DRIVE_ID = "你的共享Drive ID"; // 替换为你的共享Drive ID
  const TARGET_FOLDER_ID = "你的目标文件夹ID"; // 替换为保存文件的文件夹ID(可选)
  
  let tr = [];
  let token = '';
  
  do {
    let r = Drive.Files.list({ 
      corpora: 'drive', 
      includeItemsFromAllDrives: true, 
      supportsTeamDrive: true, 
      supportsAllDrives: true, 
      driveId: SHARED_DRIVE_ID, 
      pageToken: token,
      q: "mimeType = 'application/vnd.google-apps.folder'" 
    });
    tr.push(r);
    token = r.nextPageToken;
  } while (token != null);

  // 可选:将结果保存到指定Drive文件夹
  if (TARGET_FOLDER_ID) {
    let folder = DriveApp.getFolderById(TARGET_FOLDER_ID);
    folder.createFile(
      `SharedDriveList ${Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss")}`, 
      JSON.stringify(tr), 
      MimeType.PLAIN_TEXT
    );
  }

  let html = '<style>td,th{border:1px solid black;font-size: 16px;padding:8px;}</style><table><tr><th>Title</th><th>Id</th><th>Path</th></tr>';
  tr.forEach((o) => {
    o.items.forEach(item => {
      if (item.mimeType === "application/vnd.google-apps.folder") {
        html += `<tr><td>${item.title}</td><td>${item.id}</td><td>${getPathAllDrivesFromId(item.id)}</td></tr>`;
      }
    });
  });
  html += '</table><input type="button" value="exit" onclick="google.script.host.close()" />';
  
  SpreadsheetApp.getUi().showModelessDialog(
    HtmlService.createHtmlOutput(html).setHeight(500).setWidth(1200), 
    `Folders in: ${Drive.Drives.get(SHARED_DRIVE_ID).name}`
  );
}

function getPathAllDrivesFromId(fileId) {
  let path = [];
  let file = Drive.Files.get(fileId, { supportsAllDrives: true });
  path.push(file.title);
  while (file.parents && file.parents.length > 0) {
    file = Drive.Files.get(file.parents[0].id, { supportsAllDrives: true });
    path.unshift(file.title);
  }
  return path.join("/");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 23:55:31