Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google应用程序脚本条件格式(当无数字无法引用单元格时)_Google Apps Script_Conditional Formatting - Fatal编程技术网

Google apps script Google应用程序脚本条件格式(当无数字无法引用单元格时)

Google apps script Google应用程序脚本条件格式(当无数字无法引用单元格时),google-apps-script,conditional-formatting,Google Apps Script,Conditional Formatting,我正在建立一个电子表格,根据每个类别的预算值跟踪每个月的费用。如果值小于或等于预算金额,我想突出显示绿色单元格,如果值大于预算金额,则突出显示红色单元格 如果我传入金额的数字值,我可以让它工作,但是预算偶尔会每月更改,因此我希望能够旋转新的月份电子表格,并根据对预算单元格的引用有条件地格式化单元格颜色。我可以在excel VBA中执行此操作,但无法将引用传递到WhenNumber lessthan函数 //This errors out on trying to concatenate the

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

如果我传入金额的数字值,我可以让它工作,但是预算偶尔会每月更改,因此我希望能够旋转新的月份电子表格,并根据对预算单元格的引用有条件地格式化单元格颜色。我可以在excel VBA中执行此操作,但无法将引用传递到WhenNumber lessthan函数

//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();

在我看来,只使用工作表会更容易,因为您可以通过自定义公式使用条件格式(请参阅文档)。 但如果您确实需要使用谷歌脚本,有两种可能的选择:

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。使用.whenformulasuccessed

这应该使用与我提到的自定义公式的Coditional相同的概念

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

.WhenFormulaSuited(“=C1谢谢Jonas!这很有效!我本应该考虑这么做的。这是我最后使用的,以便我可以在C和D列上指定行

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

.WhenFormulaSuited(“=C”).concat(row)+“whenNumber lessthan
方法只接受数值。有一个解决方法,但需要使用高级工作表服务。请参阅