Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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 sheets scrip条件格式错误:数据中的行数与范围中的行数不匹配_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script Google sheets scrip条件格式错误:数据中的行数与范围中的行数不匹配

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>

我正在尝试编写一个格式化脚本。我被一些条件格式卡住了。我需要让列根据单元格中的值更改颜色。我的代码如下。我得到以下错误:

异常:数据中的行数与 范围中的行。数据为920,但范围为999。(第52行, 文件“代码”)

//事务状态列的条件格式
var rangeA=sheet.getRange(“L2:L”);
var valuesA=rangeA.getValues();
var背景=[];
var textColorTransaction=[];
对于(var i=0;i
解释/问题:
  • if
    语句的第一个块计算为true时,您忘记添加一行将元素推送到
    textColorTransaction
    。因此,在
    for
    循环完成后,
    textColorTransaction
    大小小于您的范围
    rangeA

  • 这是因为
    for
    循环一直迭代到列的长度,但当单元格值等于
    “谢绝”
    时,您不会将任何元素推送到
    textColorTransaction
    。因此,
    textColorTransaction
    最终小于
    valuesA
    的长度

  • >P>使用完整的列<代码> L>代码>可能是您的目标,但您可能需要考虑到最后一行的内容。您可以替换
    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;itextColorTransaction.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);