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

如何通过Google Script用ID将公开Google文件/文件夹导入个人GDrive账号

Google Drive公开文件/文件夹导入脚本权限报错解决方案

报错核心原因

  1. 公开文件/文件夹默认权限为仅查看,无编辑权限时无法调用文件写入、文件夹修改类接口
  2. 原代码逻辑存在冗余错误:source_folder.removeFile(file2)操作完全没有必要,复制生成的新文件本身不属于源公开文件夹,该操作会触发源文件夹的权限校验,直接导致权限报错
  3. 脚本未申请足够的Google Drive读写OAuth权限

解决步骤

  • 确认待导入的公开资源权限设置:如果资源仅对特定用户开放,请先向所有者申请查看/编辑权限,或先将资源添加到你的云端硬盘快捷方式
  • 调整脚本OAuth权限范围:
    1. 打开Apps Script编辑器,点击左侧「项目设置」
    2. 勾选「在appsscript.json清单中显示“appsscript.json”编辑器」
    3. 回到编辑页打开appsscript.json,替换为以下配置,时区可根据你所在区域调整:
    {
      "timeZone": "Asia/Shanghai",
      "dependencies": {},
      "exceptionLogging": "STACKDRIVER",
      "runtimeVersion": "V8",
      "oauthScopes": ["https://www.googleapis.com/auth/drive"]
    }
    
  • 替换原错误代码,根据你的需求选择对应版本即可

修正后代码

版本1:单个公开文件复制

function copySinglePublicFile() {
  // 替换为实际要复制的公开文件ID
  const publicFileId = "替换为目标文件ID";
  // 替换为你的目标文件夹ID
  const targetFolderId = "mydriveid";
  try {
    const sourceFile = DriveApp.getFileById(publicFileId);
    const targetFolder = DriveApp.getFolderById(targetFolderId);
    const copyFileName = `BACKUP ${Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd')} ${sourceFile.getName()}`;
    // 直接复制到目标文件夹,无需额外移动或操作源文件夹
    sourceFile.makeCopy(copyFileName, targetFolder);
    console.log("文件复制成功");
  } catch (err) {
    console.error("操作失败:" + err.message);
  }
}

版本2:整个公开文件夹内容批量复制(支持子文件夹递归)

function copyPublicFolderContents() {
  // 替换为待导入的公开文件夹ID
  const sourceFolderId = "publicfolderid";
  // 替换为你的目标文件夹ID
  const targetFolderId = "mydriveid";
  const sourceFolder = DriveApp.getFolderById(sourceFolderId);
  const targetFolder = DriveApp.getFolderById(targetFolderId);

  // 复制当前层级所有文件
  const files = sourceFolder.getFiles();
  while (files.hasNext()) {
    const file = files.next();
    const copyFileName = `BACKUP ${Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd')} ${file.getName()}`;
    file.makeCopy(copyFileName, targetFolder);
  }

  // 递归复制所有子文件夹
  const subFolders = sourceFolder.getFolders();
  while (subFolders.hasNext()) {
    const subFolder = subFolders.next();
    const newSubFolder = targetFolder.createFolder(subFolder.getName());
    copySubFolder(subFolder, newSubFolder);
  }
  console.log("文件夹所有内容复制完成");
}

// 子文件夹递归复制辅助函数
function copySubFolder(sourceSubFolder, targetSubFolder) {
  const files = sourceSubFolder.getFiles();
  while (files.hasNext()) {
    const file = files.next();
    file.makeCopy(file.getName(), targetSubFolder);
  }
  const subFolders = sourceSubFolder.getFolders();
  while (subFolders.hasNext()) {
    const subFolder = subFolders.next();
    const newSubFolder = targetSubFolder.createFolder(subFolder.getName());
    copySubFolder(subFolder, newSubFolder);
  }
}

注意事项

  • 单次复制文件数量过多、体积过大时可能触发Google Drive API配额限制,可分批次操作或等待24小时配额自动恢复
  • 首次运行脚本时需在弹出的授权窗口中允许脚本访问你的Google Drive资源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 15:30:02