[英]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:希望的结果:
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);
}
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 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()
,因为它会影响中的所有其他单元格数据范围。
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.