I have written an Apps Script code that creates a conditional format on a range. After running the script, the conditional format is created perfectly but it doesn't work on its own: I have to manually enter on Format > Conditional format > Enter the valdation I created and save it (withouth changing a thing) for it to work as it should.
var hojaCamada = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(camada);
var range = hojaCamada.getRange("K6:K100");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied('=Y($J6 < $I$3;$K6<1)')
.setBackground("#ea9999")
.setRanges([range])
.build();
var rules = hojaCamada.getConditionalFormatRules();
rules.push(rule);
hojaCamada.setConditionalFormatRules(rules);
Can anyone help me figure out what the problem is? Thanks in advance.
Marcos
答案 0 :(得分:1)
我没发现您的代码有什么问题,不过我会分享一些我做不同的事情,
我通过清除现有规则来启动颜色功能。
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName("x");
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
sheet.clearConditionalFormatRules();
var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
我也在每个规则中手动设置范围
var rule = SpreadsheetApp.newConditionalFormatRule()
.setFontColor('#a2c8ec')
.setBackground('black')
.whenFormulaSatisfied('=$D1=1')
.setRanges([spreadsheet.getRange('A:J')])
.build();
conditionalFormatRules.push(rule);
sheet.setConditionalFormatRules(conditionalFormatRules);
在您要执行的两件事中,我将更改您选择范围的方式,并在每次运行该功能时清除所有旧格式。