[英]How do I apply conditional formatting to cells based upon the result of the .getValues( ) method in Google Apps Script?
I am writing this script to change the formatting of a row of checkboxes that total 0 selections.我正在编写此脚本以更改总共 0 个选择的一排复选框的格式。 In my data validation, "true" = 1 and "false" = 0. The range is represented as an object [ ][ ].
在我的数据验证中,“true” = 1 和“false” = 0。范围表示为 object [ ][ ]。
The logic below determines which rows need to be formatted, but how do I actually apply the formatting to this row?下面的逻辑决定了哪些行需要格式化,但我如何将格式化实际应用到这一行? I am having trouble accessing the row's formatting because I am working at the value level with the resulting object [ ] [ ].
我无法访问行的格式,因为我正在使用生成的 object [ ] [ ] 在值级别上工作。
Here is what the end result should look like: Camping Gear Tally最终结果应该是这样的:野营装备理货
(I recognize there is probably an easy way to do this with conditional formatting built in, but I'm just trying to get better at coding) (我认识到可能有一种简单的方法可以使用内置的条件格式来做到这一点,但我只是想在编码方面做得更好)
function updateFormatting() { var cellValues = SpreadsheetApp.getActiveSheet().getRange("C2:I37").getValues(); for (let i = 0; i < cellValues.length; i++) { let rowTotal = 0; for (let j = 0; j < cellValues[i].length; j++) { rowTotal += cellValues[i][j]; } if (rowTotal == 0) { /* Apply formatting to this row */ } } }
Thank you for your guidance!感谢您的指导!
I believe your goal is as follows.我相信你的目标如下。
1
and 0
, respectively.1
和0
。1
and 0
, respectively, when those values are retrieved by getValues
, those are retrieved as the string type.1
和0
时,当这些值通过getValues
检索时,将这些值作为字符串类型检索。 So, in your script, when rowTotal += cellValues[i][j];
rowTotal += cellValues[i][j];
is run, the values are added as the string./* Apply formatting to this row */
, when the script for setting the cell format is put in a loop, the process cost will be high./* Apply formatting to this row */
,当设置单元格格式的脚本放在循环中时,处理成本会很高。 When these points are reflected in your script, how about the following modification?当这些点反映在你的脚本中时,下面的修改怎么样?
function updateFormatting() {
var sheet = SpreadsheetApp.getActiveSheet();
var cellValues = sheet.getRange("C2:I37").getValues();
var ranges = [];
for (let i = 0; i < cellValues.length; i++) {
let rowTotal = 0;
for (let j = 0; j < cellValues[i].length; j++) {
rowTotal += Number(cellValues[i][j]);
}
if (rowTotal == 0) {
var row = i + 2;
ranges.push(`C${row}:I${row}`);
}
}
sheet.getRangeList(ranges).setBackground("#fce5cd");
}
sheet.getRangeList(ranges).setBackground("#fce5cd")
and the background color of rows are set as #fce5cd
.sheet.getRangeList(ranges).setBackground("#fce5cd")
使用,并且行的背景颜色设置为#fce5cd
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.