google-apps-scriptgoogle-sheetsgoogle-sheets-apigoogle-sheets-vlookup

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; 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!

enter image description here

Those cells in green are all vlookups and they update from an array I have on another spreadsheet. 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.


Solution

  • 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.

    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:

    References: