Google Apps 脚本条件格式 whenNumberLessThan 无法引用单元格
Google Apps Script Conditional Formatting whenNumberLessThan Unable to Reference Cell
我正在构建一个电子表格,以根据每个类别的预算值跟踪每个月的费用。如果值小于或等于预算金额,我想突出显示一个单元格,如果它大于预算金额,我想突出显示红色。
如果我传入金额的数值,我可以让它工作,但是预算偶尔会每月变化,所以我希望能够启动一个新的月份电子表格并让它有条件地格式化单元格基于对预算单元格的引用的颜色。我可以在 excel VBA 中执行此操作,但无法将引用传递给 whenNumberLessThan 函数。
//This errors out on trying to concatenate the row onto what is in the D column
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan("=D".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
//This works with 1200 or any other numeric value
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan(1200)
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
在我看来,您计划使用表格会更容易,因为您可以通过自定义公式使用条件格式(请参阅文档 here)。
但是,如果您确实需要使用 Google 脚本,则有 2 个可能的选择:
1.在脚本中获取预算单元格值并使用 .whenNumberLessThan
您应该获取单元格值,然后使用 .whenNumberLessThan,因为如前所述,此函数只接受数字。代码看起来像这样:
var budgetValue = SpreadsheetApp.getRange("D".concat(row)).getValue();
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan(budgetValue)
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
2。使用 .whenFormulaSatisfied
这应该使用我提到的关于使用自定义公式的条件相同的概念
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenFormulaSatisfied("=C1<$D$".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
谢谢乔纳斯!这很好用!我早该想到这样做的。这是我最终使用的,所以我可以在 C 和 D 列上指定行
var ruleRed = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied("=C".concat(row)+"<$D$".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
我正在构建一个电子表格,以根据每个类别的预算值跟踪每个月的费用。如果值小于或等于预算金额,我想突出显示一个单元格,如果它大于预算金额,我想突出显示红色。
如果我传入金额的数值,我可以让它工作,但是预算偶尔会每月变化,所以我希望能够启动一个新的月份电子表格并让它有条件地格式化单元格基于对预算单元格的引用的颜色。我可以在 excel VBA 中执行此操作,但无法将引用传递给 whenNumberLessThan 函数。
//This errors out on trying to concatenate the row onto what is in the D column
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan("=D".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
//This works with 1200 or any other numeric value
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan(1200)
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
在我看来,您计划使用表格会更容易,因为您可以通过自定义公式使用条件格式(请参阅文档 here)。 但是,如果您确实需要使用 Google 脚本,则有 2 个可能的选择:
1.在脚本中获取预算单元格值并使用 .whenNumberLessThan
您应该获取单元格值,然后使用 .whenNumberLessThan,因为如前所述,此函数只接受数字。代码看起来像这样:
var budgetValue = SpreadsheetApp.getRange("D".concat(row)).getValue();
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenNumberLessThan(budgetValue)
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
2。使用 .whenFormulaSatisfied
这应该使用我提到的关于使用自定义公式的条件相同的概念
var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenFormulaSatisfied("=C1<$D$".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();
谢谢乔纳斯!这很好用!我早该想到这样做的。这是我最终使用的,所以我可以在 C 和 D 列上指定行
var ruleRed = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied("=C".concat(row)+"<$D$".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();