似乎无缘无故地获得不正确的范围高度?

Getting Incorrect range height for seemingly no reason?

我正在编写一个脚本,用于将范围从一个 sheet 复制并粘贴到另一个。粘贴的范围大小应该通过使用两个函数来减小:一个是删除具有特定值的行,另一个是聚合函数。

我在引入 aggregate 函数后开始出现此错误 该函数基本上是使用 reduce JS 函数来减小数组大小。

我已经复制了我的问题 here 并且可以在脚本编辑器中访问代码。

当我 运行 脚本时出现以下错误:

Incorrect range height was 28 but should be 7 (line 36, file "test")

我不知道为什么会出现此错误。我的聚合函数 returns 一个格式正确且长度正确的数组。

 function append_range(){    


       var origin_sheet = SpreadsheetApp.openById('1-2ZheMz1p01qwtwY3ghbNjJedYfGXeylnLEjDMCLpMw');//open the file
       origin_sheet = origin_sheet.getSheetByName('test');
       var rangeStart = 2;

       var range = origin_sheet.getRange('A'+ (rangeStart.toString())+':T'+ (origin_sheet.getLastRow()).toString());


      var dataFromRange = range.getValues();
      var dataFromRangeLength = dataFromRange.length;


      var destination_sheet = SpreadsheetApp.openById('1-2ZheMz1p01qwtwY3ghbNjJedYfGXeylnLEjDMCLpMw');
       destination_sheet = destination_sheet.getSheetByName('append');

      var rowLast = destination_sheet.getLastRow()+1;
      Logger.log("row last" + rowLast);
      var formattedRange = deleteRows(dataFromRange);

       var groups = aggregate(formattedRange);

       var aggregates = [];

       for(var group in groups)
       {
        aggregates.push(groups[group]);
       }    

           Logger.log(aggregates);
      var formattedRangeLength = aggregates.length;
         Logger.log("formattedRangeLength" + formattedRangeLength);


      destination_sheet.getRange(rowLast,1,formattedRangeLength, 20).setValues(deleteRows(dataFromRange));





function isDate(sDate) {
  if (isValidDate(sDate)) {
    sDate = Utilities.formatDate(new Date(sDate), "PST", "yyyy-MM-dd");
  }
  return sDate;
}

function isValidDate(d) {
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
    return false;
  return !isNaN(d.getTime());
}    
//
function deleteRows(dataRange){//just pass the range in an array and this method will return another array with filtered range

  var formatted = dataRange.filter(function(e) {
    return e[8]||e[9]||e[10]||e[11]||e[12]||e[13]||e[14]||e[15]||e[16]||e[17]||e[18]||e[19];
  });
 return formatted;

}

   function aggregate(data)
    {

       var groups = data.reduce(
            function(accumulator, previous){
            {
                var key = previous[1] + previous[3] + previous[5] + previous[6];
                var group = accumulator[key];
                if(group == null || typeof group == 'undefined')
                {
                    accumulator[key] = previous;
                }
                else {
                    var startIndex = 8;
                    for(var i = startIndex; i < previous.length;i++)
                    {
                        group[i] += previous[i];
                    }
                }

               return accumulator;
            }},
            {});
        return groups;
    }



}

.setValues() 没有设置您的 aggregates 数组,它试图设置 deleteRows(dataFromRange)

// Change the setValues() to your reduced array
 destination_sheet.getRange(rowLast,1,formattedRangeLength, 20).setValues(aggregates);

我认为这可能有效:

var output=deleteRows(dataFromRange));
destination_sheet.getRange(rowLast,1,output.length, output[0].length).setValues(deleteRows(output));

This assumes a non jagged array.