0
votes

How to get Colored cell with text from another sheet in google spreadsheet?

Hi, I have 2 sheets and I'm trying to get the colored cell with text from sheet 1 to sheet 2.

Can someone help me, please?

Example:

In sheet 1 i put a data in Column 1 Row 1 and then colored it with yellow at the same time it will appear in sheet 2 Column 1 Row 1.

1

1 Answers

1
votes

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

  1. copyTo() reference;
  2. CopyPasteType enum reference;
  3. Range class reference;