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

Google Apps Script修改需求:复制含文件文件夹或多文件至新文件夹

修改Google Apps脚本实现多文件/文件夹复制

方案1:支持复制多个文件(简单易上手)

表格调整

在folder_data标签的**B列(Template Doc ID)**中,可输入多个文件ID,用逗号分隔(允许带空格),例如:1abc..., 2def..., 3ghi...

脚本修改

  1. 替换原makeCopy函数为以下批量处理函数:
function makeMultipleCopies(sourceIds, destinationFolderId, baseFileName) {
  const destinationFolder = DriveApp.getFolderById(destinationFolderId);
  // 拆分ID并去除无效空格
  const ids = sourceIds.split(',').map(id => id.trim()).filter(id => id);
  
  ids.forEach((id, index) => {
    try {
      const file = DriveApp.getFileById(id);
      // 生成文件名:多文件时加序号,单文件用原文件名
      const fileName = ids.length > 1 ? `${baseFileName} ${index + 1}` : file.getName();
      file.makeCopy(fileName, destinationFolder);
      logEventToGoogleSheet(`已复制文件:${file.getName()}`);
    } catch(e) {
      logEventToGoogleSheet(`复制文件ID ${id} 失败:${e.message}`);
    }
  });
}
  1. 修改createFolders函数中的复制调用逻辑:
    找到原代码中这段:
// copy the template doc into the new directory (if specified)
if(templateDocId != '') {
  makeCopy(templateDocId, newFolderId, newFolderName + ' Template Document');
} 

替换为:

// 复制多个模板文件到新目录(如果指定)
if(templateDocId != '') {
  makeMultipleCopies(templateDocId, newFolderId, newFolderName + ' Template Document');
} 

方案2:复制整个现有文件夹(含子文件夹和文件)

表格调整

在folder_data标签的**B列(Template Doc ID)**中,输入需要复制的源文件夹ID即可,脚本会自动识别并递归复制整个结构。

脚本修改

  1. 新增递归复制文件夹的函数:
function copyFolderRecursively(sourceFolderId, destinationFolderId, newFolderName) {
  const sourceFolder = DriveApp.getFolderById(sourceFolderId);
  const destinationParent = DriveApp.getFolderById(destinationFolderId);
  
  // 创建目标文件夹,默认使用源文件夹名称
  const newFolder = destinationParent.createFolder(newFolderName || sourceFolder.getName());
  logEventToGoogleSheet(`已创建文件夹:${newFolder.getName()}`);

  // 复制当前文件夹内的所有文件
  const files = sourceFolder.getFiles();
  while (files.hasNext()) {
    const file = files.next();
    file.makeCopy(file.getName(), newFolder);
    logEventToGoogleSheet(`已复制文件:${file.getName()}`);
  }

  // 递归复制所有子文件夹
  const subFolders = sourceFolder.getFolders();
  while (subFolders.hasNext()) {
    const subFolder = subFolders.next();
    copyFolderRecursively(subFolder.getId(), newFolder.getId());
  }
  
  return newFolder.getId();
}
  1. 修改createFolders函数中的复制逻辑,兼容文件夹和多文件:
    先保留方案1中的makeMultipleCopies函数,再替换原复制代码为:
// 复制模板(支持单个文件、多个文件或整个文件夹)
if(templateDocId != '') {
  try {
    // 优先尝试按文件夹处理
    const sourceFolder = DriveApp.getFolderById(templateDocId);
    copyFolderRecursively(templateDocId, newFolderId);
  } catch(e) {
    // 非文件夹则按多文件处理
    logEventToGoogleSheet(`目标ID不是文件夹,尝试按多文件复制:${e.message}`);
    makeMultipleCopies(templateDocId, newFolderId, newFolderName + ' Template Document');
  }
} 

测试注意事项

  • 运行脚本时需授权足够的Google Drive权限
  • 先在测试目录验证功能,避免误操作
  • 查看log标签的日志信息,排查复制失败问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 18:12:37