复制整个电子表格,仅保留值

War*_*ton 5 google-sheets google-apps-script google-sheets-api google-sheets-vlookup

我想将包含 20 多张工作表的整个电子表格复制到云端硬盘中的其他位置;但是,我只想保留每个单元格中的硬值和格式,而不是公式(基本上只是拍摄值的快照)。我一直在考虑如何写这个,但我不知道什么是最好的方法。我刚刚开始学习条件语句,例如在我的谷歌表格培训中循环,任何帮助将不胜感激。谢谢!

在此输入图像描述

那些绿色的单元格都是 vlookup,它们是从另一个电子表格上的数组更新的。我们的想法是获取数组中的所有正确数据,让这张表完全填写当天的正确值,然后最好将其保存为谷歌表,但只保存值,以便事后可以编辑它们(如果有)数组数据中有错误。

Tan*_*ike 5

  • 您想要将源电子表格复制到 Google 云端硬盘的特定文件夹中。
  • 您想用作a date stamped on it for a name文件名。
  • 您不想复制公式。您只想复制显示值。
  • many vlookups and other outside reference cells在您的电子表格中,包含的公式。
  • 您希望使用 Google Apps 脚本来实现此目的。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。

实现上述目标的示例脚本的流程如下。

流动:

  1. 将源电子表格中的所有工作表复制为临时工作表。
  2. 在复制的工作表中,单元格仅被值覆盖。这样就可以去掉公式了。
  3. 复制源电子表格。
  4. 删除源电子表格中的临时工作表。
  5. 删除目标电子表格中的原始工作表。
  6. 将复制的电子表格移动到特定文件夹。

示例脚本:

在运行脚本之前,请设置源电子表格 ID 和目标文件夹 ID。

function myFunction() {
  var spreadsheetId = "###"; // Please set the source Spreadsheet ID.
  var destFolderId = "###";  // Please set the destination folder ID.

  // Copy each sheet in the source Spreadsheet by removing the formulas as the temporal sheets.
  var ss = SpreadsheetApp.openById(spreadsheetId);
  var tempSheets = ss.getSheets().map(function(sheet) {
    var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName() + "_temp");
    var src = dstSheet.getDataRange();
    src.copyTo(src, {contentsOnly: true});
    return dstSheet;
  });
  
  // Copy the source Spreadsheet.
  var destination = ss.copy(ss.getName() + " - " + new Date().toLocaleString());
  
  // Delete the temporal sheets in the source Spreadsheet.
  tempSheets.forEach(function(sheet) {ss.deleteSheet(sheet)});
  
  // Delete the original sheets from the copied Spreadsheet and rename the copied sheets.
  destination.getSheets().forEach(function(sheet) {
    var sheetName = sheet.getSheetName();
    if (sheetName.indexOf("_temp") == -1) {
      destination.deleteSheet(sheet);
    } else {
      sheet.setName(sheetName.slice(0, -5));
    }
  });

  // Move file to the destination folder.
  var file = DriveApp.getFileById(destination.getId());
  DriveApp.getFolderById(destFolderId).addFile(file);
  file.getParents().next().removeFile(file);
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 在此示例脚本中,使用了此答案的脚本。
  • 这是一个简单的示例脚本。所以请根据您的实际情况进行修改。

参考: