所以我想将条件格式应用于此工作表,如果第5列是Project A,我希望它是蓝色,如果是Project B,我希望它是红色
使用以下代码,将应用项目A,然后项目B将其覆盖/删除。
我认为这不应该因为拼接而发生。我在做什么错了?
如果我删除第13和15行,那么我根本看不到第一个应用的规则。
function Colors() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('A1:Z1000')])
.whenFormulaSatisfied('=$E1="Project A"')
.setFontColor('#0000FF')
.build());
spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('A1:Z1000')])
.whenFormulaSatisfied('=$E1="Project B"')
.setFontColor('#FF0000')
.build());
spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
};
答案 0 :(得分:1)
因此.splice(conditionalFormatRules.length - 1, 1, ..)
正在从数组 [1] 中删除最后一项。如果该数组中唯一的项目是您以前的格式设置规则,则您将删除'A'规则,并用新规则替换为'B'。
如果您只想在数组上添加规则,请尝试使用push()函数!
function Colors() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('A1:Z1000')])
.whenFormulaSatisfied('=$E1="Project A"')
.setFontColor('#0000FF')
.build());
//We can also remove this and do it all at once at the bottom!
//spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange('A1:Z1000')])
.whenFormulaSatisfied('=$E1="Project B"')
.setFontColor('#FF0000')
.build());
spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
}