The goal is to create frozen backups of a spreadsheet with multiple sheets. The backup spreadsheet needs to copy over values and format, but not the formulas.
Some of the sheets have =IMPORTRANGE
, which becomes a problem if the spreadsheet is copied first and then the values copied over the same range, again, to get rid of formulas, because the new spreadsheet requires access to the imported range in the middle of the function (which leads to #REF in A1).
This question was clearly asked many times, but none of the answers seem to solve this issue. I have looked at these questions (and more, actually): Link 1, Link 2, Link 3, Link 4, Link 5, and Link 6. I tried implementing snippets from the answers to those questions or combinations of them to no avail. It became to difficult to track every bit of snippet I tested.
I tried using copyTo()
either to copy entire sheets in a forEach
function, copy with the options {contentsOnly:true}
and {formatOnly:true}
, but have been unsuccessful. Either copyTo()
wants the ranges to be in the same spreadsheet/sheet or the getDataRange()
doesn't match the backup range...
Here's the script I'm currently using, which successfully creates a copy of the entire spreadsheet with values only. I cannot recall from which question I got it from.
function copyEntireSpreadsheet(){
var ss,ssName,sheet,sheetName,data,destination
ss = SpreadsheetApp.openById("id").getSheets();
ssName = SpreadsheetApp.openById("id").getName();
destination = SpreadsheetApp.create(ssName + " - " + new Date().toLocaleString());
for (var i = 0; i < ss.length; i++){
sheet = ss[i];
sheetName = sheet.getSheetName();
data = sheet.getSheetValues(1, 1, sheet.getLastRow(), sheet.getLastColumn());
destination.insertSheet(sheetName);
destination.getSheets()[i+1].getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).setValues(data);
}
destination.deleteSheet(destination.getSheetByName('Sheet1'));
}
The source spreadsheet has a lot of formatting with merged cells, conditional formatting, etc., which would be ideal to copy. I can also hard code certain sheets into the script, if that would make any difference with an alternative method.
In short: I'm looking for a frozen backup of a spreadsheet with multiple sheets. Values and formatting need to be copied, not the formulas.
I might be missing something in one of the answers to all those questions, so I'll keep trying. In the meantime, any help/direction would be appreciated. Thanks in advance!
SOLVED: @Tanaike's second sample script below is an excellent workaround to copying sheets that have references to other spreadsheets and hence require access to them before being overwritten by values only. Big thanks to @Tanaike for the extensive help with this -- much appreciated.