脚本:如何在编辑时将条件格式设置规则复制并重新应用到某个范围? [英] Script: How to copy and reapply conditional formatting rules to a range on edit?
问题描述
在Google表格中,粘贴新值时,可以更改条件格式应用于的单元格范围.我正在寻找一个脚本,该脚本将复制工作表中存在的条件格式,并在人们粘贴数据时将其重新应用于新的单元格.
In Google Sheets, the range of cells that conditional formatting applies to can change when you paste in new values. I'm looking for a script that will copy the conditional formatting present in the sheet and reapply it to new cells as people paste in data.
实际上我并没有做太多的脚本编写工作,但是我偷了别人的脚本并将科学怪人在一起,直到它们模糊地起作用为止.到目前为止,这是我偷的东西:
I don't actually do much scripting, but I steal other people's scripts and Frankenstein them together until they're vaguely functional. Here's what I've stolen so far:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
range.clearFormat();
//clears formatting just on data that is pasted into the sheet
var rules = sheet.getConditionalFormatRules();
var newRules = [];
for(var r = 0; r < rules.length; r++) {
var booleanCondition = rules[r].getBooleanCondition();
if(booleanCondition != null) {
var rule = SpreadsheetApp.newConditionalFormatRule()
.withCriteria(booleanCondition.getCriteriaType(), booleanCondition.getCriteriaValues())
.setBackground(booleanCondition.getBackground())
.setRanges([sheet.getRange("A:A"),sheet.getRange("C:C")])
.build();
newRules.push(rule);
sheet.setConditionalFormatRules(newRules);
}
}
}
当然,问题是我正在获取该工作表的所有条件格式设置规则,并将其全部应用于两列(A:A和C:C).不管它们曾经应用于什么列,都将应用这些规则.
The problem, of course, is that I'm getting ALL of the conditional formatting rules for that sheet and applying them all to two columns (A:A and C:C). These rules are applied regardless of what columns they used to apply to.
有人可以建议一种方法来复制现有的条件格式设置规则并将其重新应用到我从中复制它们的列中吗?
Can someone suggest a way to copy the pre-existing Conditional Formatting rules and reapply them to the columns I copied them from?
推荐答案
在setRanges()函数[1]中,您需要设置要应用格式的范围.在这种情况下,我使用了从onEdit触发器[2]的事件对象获得的已编辑范围:
The setRanges() function [1] is where you need to set up the range to which you want the formatting to be applied. In this case, I used the edited range which you're getting from the event object of the onEdit trigger [2]:
function onEdit(e) {
var range = e.range;
var column = range.getColumn();
var sheet = range.getSheet();
range.clearFormat();
//clears formatting just on data that is pasted into the sheet
//Get all Sheet rules and iterate through them
var rules = sheet.getConditionalFormatRules();
var newRules = [];
newRules = newRules.concat(rules);
for(var r = 0; r < rules.length; r++) {
var rule = rules[r];
//Get condition for each rule
var booleanCondition = rule.getBooleanCondition();
//Get the ranges to which each rule applies and iterate through
var ranges = rule.getRanges();
for (var i = 0; i < ranges.length; i++) {
var ruleColumn = ranges[i].getColumn();
//If condition isn't null and edited column is the same as the one in the range, add rule
if((ruleColumn == column) && (booleanCondition != null)) {
var newRule = SpreadsheetApp.newConditionalFormatRule()
.withCriteria(booleanCondition.getCriteriaType(), booleanCondition.getCriteriaValues())
.setBackground(booleanCondition.getBackground())
.setRanges([range])
.build();
newRules.push(newRule);
}
}
}
sheet.setConditionalFormatRules(newRules);
}
我还将这一行放在for循环之外,因为只需要一次在工作表中插入所有格式设置规则:
I also put this line outside the for loop because inserting all the formatting rules in the sheet is needed only once:
sheet.setConditionalFormatRules(newRules);
[2] https://developers.google.com/apps-脚本/指南/触发器/事件
这篇关于脚本:如何在编辑时将条件格式设置规则复制并重新应用到某个范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!