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

by Kevin Acogido   Last Updated June 13, 2019 08:26 AM

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.



Answers 1


Copy everything

If you want to copy everything, including Range values, color, background-color, 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:

/**
 * Copies only specific props;
 * @param {Range} source copy from range;
 * @param {Range} target copy to range;
 * @param {String} props 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 background color

Use the getBackgrounds() and setBackgrounds() respectively.

/**
 * 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);
}
Oleg Valter
Oleg Valter
June 13, 2019 08:09 AM

Related Questions


Updated June 07, 2019 22:26 PM

Updated January 19, 2019 04:26 AM

Updated March 11, 2019 01:26 AM

Updated December 19, 2018 06:26 AM

Updated June 02, 2019 05:26 AM