简体   繁体   English

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

[英]Copy an entire Spreadsheet, Just Preserve the Values

I want to copy an entire spreadsheet of some 20+ sheets to a different location in Drive;我想将包含 20 多张工作表的整个电子表格复制到云端硬盘中的其他位置; however, I only want to preserve the hard values and formatting in each cell, and not the formulas (basically just taking a snapshot of the values).但是,我只想保留每个单元格中的硬值和格式,而不是公式(基本上只是对值进行快照)。 I have been playing around with how to write this but I don't have a solid idea on what's the best approach.我一直在研究如何写这个,但我对什么是最好的方法没有一个可靠的想法。 I'm just starting to learn conditionals like looping in my google sheets training, any help would be appreciated.我刚刚开始在我的谷歌表格培训中学习循环等条件,任何帮助将不胜感激。 Thanks!谢谢!

在此处输入图像描述

Those cells in green are all vlookups and they update from an array I have on another spreadsheet.那些绿色的单元格都是 vlookup,它们从我在另一个电子表格上的数组中更新。 The idea is to get all the right data in the array, have this sheet fully fill out with the correct values for the day, then save it preferably as a google sheet but just the values, so that they are editable after the fact if there was an error in the array data.这个想法是在数组中获取所有正确的数据,让这张表完全填写当天的正确值,然后最好将其保存为谷歌表,但只保存值,以便在事后可以编辑它们是数组数据中的错误。

  • You want to copy the source Spreadsheet in the specific folder in your Google Drive.您想将源电子表格复制到 Google 云端硬盘的特定文件夹中。
  • You want to use a date stamped on it for a name as the filename.您想将a date stamped on it for a name用作文件名。
  • You don't want to copy the formulas.您不想复制公式。 You want to copy only displaying values.您只想复制显示值。
  • In your Spreadsheet, the formulas of many vlookups and other outside reference cells are included.在您的电子表格中,包含many vlookups and other outside reference cells的公式。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请认为这只是几个可能的答案之一。

The flow of the sample script for the above goal is as follows.上述目标的示例脚本流程如下。

Flow:流动:

  1. Copy all sheets in the source Spreadsheet as the temporal sheets.将源电子表格中的所有工作表复制为临时工作表。
  2. At the copied sheets, the cells are overwritten by only the values.在复制的工作表中,单元格仅被值覆盖。 By this, the formulas can be removed.这样,可以删除公式。
  3. Copy the source Spreadsheet.复制源电子表格。
  4. Delete the temporal sheets in the source Spreadsheet.删除源电子表格中的临时工作表。
  5. Delete the original sheets in the destination Spreadsheet.删除目标电子表格中的原始工作表。
  6. Move the copied Spreadsheet to the specific folder.将复制的电子表格移动到特定文件夹。

Sample script:示例脚本:

Before you run the script, please set the source Spreadsheet ID and the destination folder ID.在运行脚本之前,请设置源电子表格 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);
}

Note:笔记:

  • In this sample script, the script of this answer was used.在此示例脚本中,使用了此答案的脚本
  • This is a simple sample script.这是一个简单的示例脚本。 So please modify this for your actual situation.所以请根据您的实际情况进行修改。

References:参考:

If you actually plan on using this data you probably want to think twice of just randomly copying everything.如果你真的打算使用这些数据,你可能要三思而后行,只是随机复制所有内容。 If you want to just archive Just add export?format=zip to the end of your link, then you will have a saved copy with values and formatting.如果您只想存档只需将export?format=zip添加到链接的末尾,您将获得一个包含值和格式的保存副本。

Another very simple way is to Download the sheet to Excel.另一种非常简单的方法是将工作表下载到 Excel。 It will keep local formulas but remove external references (like importrange, etc), so data will stay frozen;) You can even open the Excel file from Gsheets (without converting it to sheets).它将保留本地公式但删除外部引用(如 importrange 等),因此数据将保持冻结状态;)您甚至可以从 Gsheets 打开 Excel 文件(无需将其转换为工作表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保留Google电子表格副本的编辑权限? - How preserve edit permissions for the copy of a Google Spreadsheet? 使用 AppScript 将值复制到另一个电子表格 - Copy Values to another Spreadsheet with AppScript 此功能目前仅应用于一页,但是否可以将其应用于整个电子表格 - This function is currently applied to just one page, but is it possible to apply it to the entire spreadsheet 在Google电子表格中通过Google脚本复制值 - Copy values by Google script in Google spreadsheet 如何将整个数组值输入到谷歌电子表格的单元格中? - How to enter an entire array values into a cell in google spreadsheet? 如何使用公式(而不仅仅是值)制作 Google 电子表格的副本? - How to make a copy of a Google spreadsheet with formulas, not only values? 将电子表格文件复制到 Google Drive 并将所有公式替换为值 - Copy a spreadsheet file to Google Drive and replace all the formulas with values 使用Google Apps脚本将值和格式复制到另一个Google电子表格 - Copy values and formatting to another Google spreadsheet with Google Apps Script 仅将值从多个选项卡复制到另一个电子表格的脚本 - Script to copy values only from multiple tabs to another spreadsheet 将值从一个范围复制到另一个电子表格末尾的脚本 - Script to copy values from a range to the end of another spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM