【问题标题】:Google Apps Script Conditional Formatting whenNumberLessThan Unable to Reference Cell当NumberLessThan 无法引用单元格时,Google Apps 脚本条件格式
【发布时间】:2019-09-13 17:23:06
【问题描述】:

我正在构建一个电子表格,以根据每个类别的预算值跟踪每个月的费用。如果值小于或等于预算金额,我想突出显示一个绿色的单元格,如果它大于预算金额,我想突出显示红色。

如果我传入金额的数值,我可以让它工作,但是预算偶尔会每月变化,所以我希望能够启动一个新月份的电子表格并让它有条件地格式化基于单元格颜色参考预算单元格。我可以在 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();

【问题讨论】:

标签: google-apps-script conditional-formatting


【解决方案1】:

从我的角度来看,您计划做的事情会更容易使用表格,因为您可以通过自定义公式使用条件格式(请参阅文档 here)。 但如果您确实需要使用 Google Scripts,有两种可能的选择:

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

这应该使用我提到的关于带有自定义公式的 Coditional 的相同概念

var ruleRed = SpreadsheetApp.newConditionalFormatRuleBuilder()
.whenFormulaSatisfied("=C1<$D$".concat(row))
.setBackground("#FF0000")
.setRanges([month.getRange("C".concat(row))])
.build();

【讨论】:

    【解决方案2】:

    感谢乔纳斯!这很好用!我应该想到这样做的。这是我最终使用的,所以我可以在 C 和 D 列上指定行

    var ruleRed = SpreadsheetApp.newConditionalFormatRule()
    .whenFormulaSatisfied("=C".concat(row)+"<$D$".concat(row))
    .setBackground("#FF0000")
    .setRanges([month.getRange("C".concat(row))])
    .build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      相关资源
      最近更新 更多