Reputation: 769
I'm trying to do the following:
Set the same background colors from cells in Sheet2 to the cells in Sheet1 that do have the same values as in Sheet2.
The idea is to compare all the values from Sheet2 that are also present in Sheet1, and take the premade backgrounds from cells in Sheet2 and set those premade backgrounds from Sheet2 to the cells with matching values in Sheet1.
The goal is automatically set the backgrounds based on the cells values when matching regardless of order (to avoid having to do it manually for hundreds of different matching values).
In Sheet2, I have preset a range A1:A10 with 10 different values all with their unique background.
In Sheet1, I have the same 10 values as in Sheet2 and also other non matching values. Also, the 10 matching values in Sheet1 are 'scattered around' in range A1:AG20 (different range from Sheet2, could be any range).
The result I'm trying to get would be:
I've found close questions here:
Copying background color from cells on a different tab of the same sheet
and here:
Google Sheets: Change background color of cell using content of another cell
and here:
Change cell value based on the cell's background color in Google Sheets
Based on those other questions, I've come up with this code:
function myFunction() {
const sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
const rngsh2 = sheet2.getRange("A1:A10");
const colorssh2 = rngsh2.getBackgroundObjects();
const valuessh2 = rngsh2.getValues();
const sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const rngsh1 = sheet1.getRange("A1:G20");
const valuessh1 = rngsh1.getValues();
if(valuessh2 == valuessh1){
sheet1.getRange(rngsh1).setBackgroundObjects(colorssh2);
}
}
But it's not setting the backgrounds.
Here the sample sheet
What's wrong in the code above?
Many thanks for your help!
I've checked and tried those references:
taylor.2317 answer made me realize I had forgotten to say that values in Sheet1 will repeat, and colors need the same background to the repetitions as well (should make no difference in the code for the specified range A1:AG20):
Here are new screenshot showing the repeated values:
Sheet2 input:
The result I'm trying to get with repeated values in Sheet1 would be:
The current Sheet1 result:
Upvotes: 3
Views: 2751
Reputation: 64032
function copycolors() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const cA = sh.getRange(1,1,sh.getLastRow()).getDisplayValues().flat();//values
const bA = sh.getRange(1,1,sh.getLastRow()).getBackgrounds().flat();//colors
let colors = {pA:[]};
cA.forEach((c,i) => {
colors[c]=bA[i];
colors.pA.push(c);
});
const osh = ss.getSheetByName('Sheet1');
const vs = osh.getDataRange().getDisplayValues();
const bs = osh.getDataRange().getBackgrounds();
let bgA = vs.map((r,i) =>{
r.forEach((c,j) =>{
let idx = colors.pA.indexOf(c);
if(~idx) {
bs[i][j] = colors[c];
}
});
return bs[i];
});
osh.getRange(1,1,bgA.length,bgA[0].length).setBackgrounds(bgA);
}
Upvotes: 3
Reputation: 591
=Sheet1!A1=Sheet2!A1
5 Steps:
A1
Please note: when changing cell, change the code A1
to the current cell.
Upvotes: 1