【发布时间】:2021-01-18 19:31:01
【问题描述】:
目标:在 ConditionalFormatRuleBuilder.copy() 中更改范围(工作表名称,而不是 a1Notation)
错误:条件格式规则不能引用不同的工作表。
我正在尝试使用(不是这样)explained 的复制方法。有了副本,我知道我有我需要的新条件格式的所有论据。我唯一需要更改的是工作表名称。添加范围工作正常,但更改/清除我似乎无法弄清楚的范围。我找到了post,但我希望它更通用。如果您知道使用的条件,则此示例很好。
在文档中还有一个 .build() 是我需要实现的选项吗?
主函数:
function copyFormattingToTargets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
//const input = SpreadsheetApp.getUi().prompt("Copy formatting from:").getResponseText();
const input = 'Data';
const targets = ['Log','Test'];
const templateSheet = ss.getSheetByName(input);
const inputRules = templateSheet.getConditionalFormatRules();
const rules = convertRules(inputRules,targets);
targets.forEach(target => {
const sheet = ss.getSheetByName(target);
target.clearConditionalFormatRules();
target.setConditionalFormatRules(rules);
})
}
转换函数:
function convertRules(rules,sheetnames){
const output = [];
const ss = SpreadsheetApp.getActiveSpreadsheet();
sheetnames.forEach(sh => {
rules.forEach(rule => {
const copy = rule.copy();
const newRanges = [];
const oldRanges = copy.getRanges();
oldRanges.forEach(range => {
const buildRange = ss.getSheetByName(sh).getRange(range.getA1Notation());
newRanges.push(buildRange);
});
copy.setRanges(newRanges);
output.push(copy);
});
});
return output;
}
【问题讨论】: