Google apps script Google sheets scrip条件格式错误:数据中的行数与范围中的行数不匹配
我正在尝试编写一个格式化脚本。我被一些条件格式卡住了。我需要让列根据单元格中的值更改颜色。我的代码如下。我得到以下错误: 异常:数据中的行数与 范围中的行。数据为920,但范围为999。(第52行, 文件“代码”)Google apps script Google sheets scrip条件格式错误:数据中的行数与范围中的行数不匹配,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在尝试编写一个格式化脚本。我被一些条件格式卡住了。我需要让列根据单元格中的值更改颜色。我的代码如下。我得到以下错误: 异常:数据中的行数与 范围中的行。数据为920,但范围为999。(第52行, 文件“代码”) //事务状态列的条件格式 var rangeA=sheet.getRange(“L2:L”); var valuesA=rangeA.getValues(); var背景=[]; var textColorTransaction=[]; 对于(var i=0;iP>使用完整的列 L>
//事务状态列的条件格式
var rangeA=sheet.getRange(“L2:L”);
var valuesA=rangeA.getValues();
var背景=[];
var textColorTransaction=[];
对于(var i=0;i
解释/问题:
- 当
语句的第一个块计算为true时,您忘记添加一行将元素推送到if
。因此,在textColorTransaction
循环完成后,for
的大小小于您的范围textColorTransaction
rangeA
- 这是因为
循环一直迭代到列的长度,但当单元格值等于for
时,您不会将任何元素推送到“谢绝”
。因此,textColorTransaction
最终小于textColorTransaction
的长度valuesA
sheet.getRange(“L2:L”)代码>与sheet.getRange(“L2:L”+sheet.getLastRow())代码>。这是一个可选步骤,因此不包括在我的解决方案中
var rangeA=sheet.getRange(“L2:L”);
var valuesA=rangeA.getValues();
var背景=[];
var textColorTransaction=[];
对于(var i=0;i textColorTransaction.push([“yellow”]);//您好,很有趣,不确定这是否有帮助
//Conditional Formatting for transaction status column
var rangeA = sheet.getRange("L2:L");
var valuesA = rangeA.getValues();
var backgrounds = [];
var textColorTransaction = [];
for(var i = 0; i < valuesA.length; i++) { //for each row that the data is present
var aValue = valuesA[i][0];
if(aValue == "Declined"){ //if value = Declied
backgrounds.push(["#F39581"]);
} else if(aValue == "Credit"){
backgrounds.push(["#FCE8B2"])
textColorTransaction.push(["red"]);
} else {
backgrounds.push([null]); //using null will reset the background color formatting
textColorTransaction.push([null])
}
}
rangeA.setBackgrounds(backgrounds); //Set the background colors all at once for speed.
rangeA.setFontColors(textColorTransaction);
var rangeA = sheet.getRange("L2:L");
var valuesA = rangeA.getValues();
var backgrounds = [];
var textColorTransaction = [];
for(var i = 0; i < valuesA.length; i++) { //for each row that the data is present
var aValue = valuesA[i][0];
if(aValue == "Declined"){ //if value = Declied
backgrounds.push(["#F39581"]);
textColorTransaction.push(["yellow"]); // <- NEW CODE ADDED
} else if(aValue == "Credit"){
backgrounds.push(["#FCE8B2"])
textColorTransaction.push(["red"]);
} else {
backgrounds.push([null]); //using null will reset the background color formatting
textColorTransaction.push([null])
}
}
rangeA.setBackgrounds(backgrounds); //Set the background colors all at once for speed.
rangeA.setFontColors(textColorTransaction);