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

通过API与Apps Script获取Box指定文件夹的子文件夹及文件详情

解决Box文件夹递归遍历及信息提取问题

核心问题修复方案

你当前的代码仅处理了根文件夹的一级内容,且在URL、路径处理和子文件夹遍历上存在问题,以下是针对性解决方法:

1. 正确获取访问URL

shared_link仅当文件/文件夹被手动创建共享链接后才存在,直接调用会报错。推荐两种可靠方式:

  • 在API请求的fields参数中加入web_link,这是Box官方提供的直接访问链接
  • 若web_link为空,用ID构造链接:
    • 文件:https://xxx.app.box.com/file/${item.id}
    • 文件夹:https://xxx.app.box.com/folder/${item.id}

2. 拼接完整文件夹路径

利用Box API返回的path_collection字段,它包含了从根目录到当前项的所有父文件夹。遍历path_collection.entries数组,将每个父文件夹的名称拼接起来,最后加上当前项的名称即可得到完整路径。

3. 递归遍历所有子文件夹

实现一个递归函数,每当遇到类型为folder的项时,就调用该函数继续遍历其下的子项,直到所有层级的文件和文件夹都被处理。


完整可运行代码

function getAllBoxItems() {
  const rootFolderId = '1234567'; // 你的根文件夹ID
  const boxDomain = 'https://xxx.app.box.com'; // 替换为你的Box域名
  const allItems = [];

  // 递归遍历文件夹的函数
  function traverseFolder(folderId, parentPath = '') {
    // 请求时指定需要的字段:name, type, web_link, path_collection
    const url = `https://api.box.com/2.0/folders/${folderId}/items?fields=name,type,web_link,path_collection&limit=1000`;
    const response = UrlFetchApp.fetch(url, {
      headers: { Authorization: 'Bearer ' + getBoxService_().getAccessToken() }
    });
    const result = JSON.parse(response.getContentText());
    const items = result.entries;

    // 处理当前页的所有项
    processItems(items, parentPath);

    // 处理分页(如果文件夹内项超过1000个)
    if (result.next_marker) {
      const paginatedUrl = `${url}&marker=${result.next_marker}`;
      const paginatedResponse = UrlFetchApp.fetch(paginatedUrl, {
        headers: { Authorization: 'Bearer ' + getBoxService_().getAccessToken() }
      });
      const paginatedResult = JSON.parse(paginatedResponse.getContentText());
      processItems(paginatedResult.entries, parentPath);
    }
  }

  // 封装项处理逻辑,避免重复代码
  function processItems(items, parentPath) {
    items.forEach(item => {
      // 拼接完整路径
      let fullPath;
      if (item.path_collection && item.path_collection.entries.length > 0) {
        const pathParts = item.path_collection.entries.map(entry => entry.name);
        fullPath = [...pathParts, item.name].join('\\');
      } else {
        fullPath = parentPath ? `${parentPath}\\${item.name}` : item.name;
      }

      // 获取访问URL
      let itemUrl = item.web_link || '';
      if (!itemUrl) {
        const urlSuffix = item.type === 'file' ? `file/${item.id}` : `folder/${item.id}`;
        itemUrl = `${boxDomain}/${urlSuffix}`;
      }

      // 存入结果数组
      allItems.push({
        name: item.name,
        type: item.type,
        url: itemUrl,
        fullPath: fullPath
      });

      // 递归遍历子文件夹
      if (item.type === 'folder') {
        traverseFolder(item.id, fullPath);
      }
    });
  }

  // 启动递归遍历根文件夹
  traverseFolder(rootFolderId);

  // 输出结果到日志,也可修改为写入Google Sheet等
  Logger.log(JSON.stringify(allItems, null, 2));
  return allItems;
}

代码说明

  • 递归遍历:traverseFolder函数负责处理单个文件夹,遇到子文件夹就递归调用自身,实现深度优先遍历
  • 路径拼接:通过path_collection.entries获取完整的父路径链,确保路径格式符合需求(用\\分隔)
  • URL处理:优先使用官方的web_link, fallback到ID构造的链接,避免报错
  • 分页处理:Box API默认返回1000项,加入分页逻辑确保不会遗漏大文件夹中的内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 21:05:10