我想知道如何在下面的代码中使用 OR/管道运算符添加几个内容作为条件。
我尝试了那种方式和那种var customFormulaString = "=A1=1|2";
方式,var customFormulaString = "=A1=1 | =A1=2";
但没有格式化。var cellsInA1Notation = "A1:C9";
1
A1
2
C3
关于如何使用 OR/pipe 处理多个条件的任何提示?
感谢您的帮助,非常感谢!
function yourScript() {
// ...
var cellsInA1Notation = "A1"; // could also have been e.g. "C3:D4"
setRangeToRedBackground(cellsInA1Notation);
// ...
}
// This is a custom convenience function I made which is not provided directly by the Google Sheets API.
function addConditionalFormatRule(sheet, rule) {
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules)
}
// Adds a conditional format rule to a sheet that causes a cell to have red background color if it contains a 1.
// To illustrate that conditional formatting rules do not need to be spread out across (and hidden) in the spreadsheet GUI,
// but can be manipulated entirely in your script.
function setRangeToRedBackground(cellsInA1Notation) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(cellsInA1Notation);
var customFormulaString = "=A1=1";
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied(customFormulaString)
.setBackground("red")
.setRanges([range])
.build();
addConditionalFormatRule(sheet, rule);
}
@Magne 的原始答案: 如何通过 Google 表格中的代码格式化单元格