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

如何通过编程复制Google表格且不携带绑定的Apps Script项目

复制Google表格但排除绑定的Apps Script项目

下面是几种可行的编程实现方法,按推荐程度排序:

方法1:创建空白表格并复制所有工作表(推荐)

这种方式完全绕过文件级复制,新建的空白表格本身没有绑定脚本,再将原表格的所有工作表复制过去,能最大程度保留原表格的格式和数据,且不会带入任何绑定脚本。

示例代码:

function copySheetWithoutScript() {
  const sourceSpreadsheetId = "替换为你的原表格ID";
  const sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
  const sourceSheets = sourceSpreadsheet.getSheets();
  
  // 创建全新的空白Google表格
  const newSpreadsheet = SpreadsheetApp.create("你的新表格名称");
  
  // 复制原表格的所有工作表到新表格
  sourceSheets.forEach(sheet => {
    sheet.copyTo(newSpreadsheet).setName(sheet.getName());
  });
  
  // 删除新表格默认的空白Sheet(可选,根据需求调整)
  const defaultSheet = newSpreadsheet.getSheetByName('Sheet1');
  if (defaultSheet) newSpreadsheet.deleteSheet(defaultSheet);
  
  Logger.log("新表格已生成,ID:" + newSpreadsheet.getId());
}

方法2:复制文件后删除绑定的脚本项目

如果需要保留原文件的Drive属性(比如共享设置的继承),可以先复制整个文件,再通过Apps Script API删除新文件的绑定脚本项目。

前置要求:

  1. 在Google Cloud Console中启用Apps Script API
  2. 给脚本添加OAuth权限:https://www.googleapis.com/auth/script.projects

示例代码:

function copyFileAndRemoveScript() {
  const sourceFileId = "替换为你的原表格ID";
  const sourceFile = DriveApp.getFileById(sourceFileId);
  
  // 复制原文件
  const copiedFile = sourceFile.makeCopy("不带脚本的副本");
  const copiedFileId = copiedFile.getId();
  
  // 获取新文件绑定的脚本项目
  const scriptProjects = ScriptApp.getProjectsByDocumentId(copiedFileId);
  if (scriptProjects.length > 0) {
    const scriptProjectId = scriptProjects[0].getId();
    // 调用API删除脚本项目
    const deleteUrl = `https://script.googleapis.com/v1/projects/${scriptProjectId}`;
    UrlFetchApp.fetch(deleteUrl, {
      method: 'DELETE',
      headers: { 'Authorization': `Bearer ${ScriptApp.getOAuthToken()}` }
    });
  }
  
  Logger.log("不带脚本的副本已创建,ID:" + copiedFileId);
}

方法3:导出为Excel再导入(仅适用于简单表格)

将原表格导出为Excel格式,再导入生成新的Google表格。这种方法会丢失部分Google表格特有的功能(如条件格式、数据验证、保护规则等),仅适合结构简单的表格。

前置要求:

在Google Cloud Console中启用Drive API

示例代码:

function exportImportWithoutScript() {
  const sourceFileId = "替换为你的原表格ID";
  const sourceFile = DriveApp.getFileById(sourceFileId);
  
  // 导出原表格为Excel Blob
  const excelBlob = sourceFile.getAs(MimeType.MICROSOFT_EXCEL);
  const tempExcelFile = DriveApp.createFile(excelBlob).setName("临时Excel文件");
  
  // 将Excel文件导入为新的Google表格
  const newSheet = Drive.Files.insert(
    { title: "不带脚本的副本", mimeType: MimeType.GOOGLE_SHEETS },
    tempExcelFile.getBlob()
  );
  
  // 清理临时文件
  tempExcelFile.setTrashed(true);
  
  Logger.log("新表格已生成,ID:" + newSheet.id);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 04:40:20