简体   繁体   English

将背景颜色从一列复制/粘贴到具有匹配值的列中的另一张工作表

[英]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.在一张表中,我有一列包含值和背景 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.如果它们的值匹配,我想要一个脚本,它将第一张表中列的背景颜色粘贴到第二张表中的列中。 Like this:像这样:

Source color:源颜色:

源颜色

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);

}

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:从: 在此处输入图像描述

Result:结果: 在此处输入图像描述


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.从您的代码中,我刚刚修改了目标行和列,因为您从第 4 行和 L 列开始固定值。您不需要在代码中使用动态范围getDataRange() ,因为它会影响中的所有其他单元格数据范围。

Suggestion:建议:

You can also add the code below to your onEdit()您还可以将下面的代码添加到您的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);
  }
}

在此处输入图像描述

Result:结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值从一个Google表格粘贴到另一个,然后根据ID列删除重复项 - Paste values from one Google Sheet to another and remove duplicates based on ID column 如何将值从一张纸粘贴到另一张纸到特定列的最后一行 - How to paste values from one sheet to another to last row of specific column 根据列中单元格的值,将范围从一张纸复制/粘贴到其他几张 - Copy/paste a range from one sheet to several others depending on the value of a cell in a column 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? 将特定范围复制到从一个工作表到另一个工作表中的第一个空行和特定列 - Copy a specific range to from one sheet to the first empty row and specific column in another sheet 将值从一张纸粘贴到另一张纸并删除重复的纸 - Paste values from one sheet to another and remove duplicates 使用Google表格脚本移动过滤后的值(复制和粘贴值),而忽略列标题/标题 - Moving filtered values (copy and paste values) using google sheet script while ignoring the column heading / title 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 复制单元格中的值并将值粘贴到同一工作表中的另一个单元格 - Copy values from cell and paste the values onto another cell in the same sheet Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM