Copy everything
If you want to copy everything, including Range
values, colour, background-colour, etc - use the simple copyTo()
method. Just remember that Range
dimensions should be equal (ofc, you can always add handling to account for that).
/**
* Copies all;
* @param {Range} source copy from range;
* @param {Range} target copy to range;
*/
function copyAll(source,target) {
source.copyTo(target);
}
Conditional copy
If you wish to copy only specific properties of the Range
, the abovementioned copyTo()
method can be invoked with CopyPasteType
enum, like this (please, note that due to conflict with two-arg invocation copyTo(range,options)
, transpose arg is necessary - you can hardcode it inside the function if you don't expect orientation changes):
/**
* Copies only specific props;
* @param {Range} source copy from range;
* @param {Range} target copy to range;
* @param {String} type enum CopyPasteType;
* @param {Boolean} transposed change orientation or not;
*/
function copyConditional(source,target,type,transposed) {
var t = SpreadsheetApp.CopyPasteType[type];
source.copyTo(target,t,transposed); //if transpose is not specified, will be treated as copyTo(destination,options)!;
}
Copy via getter and setter
For example, use the getBackgrounds()
and setBackgrounds()
respectively (there are a lot of other getter-setter methods you can use separately instead of the copyTo()
, see Range
reference in useful links).
/**
* Copies background color;
* @param {Range} source copy from range;
* @param {Range} target copy to range;
*/
function copyColor(source,target) {
var color = source.getBackgrounds();
target.setBackgrounds(color);
}
Useful links
copyTo()
reference;
CopyPasteType
enum reference;
Range
class reference;