Savoir Apprendre
Savoir Apprendre

Reputation: 59

Copy / paste background color from one column to another sheet in a column with matched values

In a sheet I have a column with values and background colors. In another sheet, I have a column with the same values but in a random order.I would like a script that would paste the background color of the column from the first sheet into the column in the second sheet if their values match.

Source color:

Source color

Wished result:

wished result

My sheet

I tried to modify a script that works but removes the background color from the whole sheet. Here it is:

function copycolors() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('VALIDATION');
  const cA = sh.getRange(6,3,sh.getLastRow()).getDisplayValues().flat();//values
  const bA = sh.getRange(6,3,sh.getLastRow()).getBackgrounds().flat();//colors
  let colors = {pA:[]};
  cA.forEach((c,i) => {
    colors[c]=bA[i];
    colors.pA.push(c);
  });
  const osh = ss.getSheetByName('DATA');
  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: 1

Views: 174

Answers (1)

Logan
Logan

Reputation: 2140

Try this modification:

function copycolors() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('VALIDATION');
  const cA = sh.getRange(6,3,sh.getLastRow()).getDisplayValues().flat();//values
  const bA = sh.getRange(6,3,sh.getLastRow()).getBackgrounds().flat();//colors
  let colors = {pA:[]};
  cA.forEach((c,i) => {
    colors[c]=bA[i];
    colors.pA.push(c);
  });  

  const osh = ss.getSheetByName('DATA');
  const vs = osh.getRange(4,12,osh.getLastRow()).getDisplayValues();
  const bs = osh.getRange(4,12,osh.getLastRow()).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(4,12,bgA.length,bgA[0].length).setBackgrounds(bgA);
}

From: enter image description here

Result: enter image description here


From your code I just modifed the destination row and column since you have the values fixed starting from row 4 and in column L. You don't need the Dynamic range getDataRange() used in your code as it will impact all the other cells in the Data Range.

Suggestion:

You can also add the code below to your onEdit()

  if (col == 12) {
    var value = range.getValue();
    var validationSheet = ss.getSheetByName("VALIDATION");
    var optionColor = validationSheet.createTextFinder(value).findNext().getBackground();
    range.setBackground(optionColor);
  }

Final Code:

function onEdit(e) {
  var ss = e.source;
  var sourceSheet = ss.getActiveSheet();
  var shName = sourceSheet.getName();
  var range = e.range;
  var row = range.getRow();
  var col = range.getColumn();

  var destSheet = ss.getSheetByName("COMMENTAIRES");

  if ((shName == "DATA" || shName == "EPHAD" || shName == "LIVRET") && (row > 4 && col >= 12 || col <= 16)) {
    var dataToCopy = range.getValue();
    var email = sourceSheet.getRange(row, 8).getValue();
    var destRow = destSheet.createTextFinder(email).findNext().getRow();
    destSheet.getRange(destRow, col).setValue(dataToCopy);
  }

  //ADDED SECTION
  if (col == 12) {
    var value = range.getValue();
    var validationSheet = ss.getSheetByName("VALIDATION");
    var optionColor = validationSheet.createTextFinder(value).findNext().getBackground();
    range.setBackground(optionColor);
  }
}

enter image description here

Result:

enter image description here

Upvotes: 1

Related Questions