例外:尽管从数据创建了范围,但数据中的列数与范围中的列数不匹配 [英] Exception: The number of columns in the data does not match the number of columns in the range despite the range being created from data

查看:358
本文介绍了例外:尽管从数据创建了范围,但数据中的列数与范围中的列数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出此Google脚本中我所缺少的内容.我不断收到错误消息

I'm trying to figure out what I'm missing here in this Google script. I keep getting the error

异常:数据中的列数与范围中的列数不匹配.数据为0,但范围为15.

但是对我来说这没有意义.

but it doesn't make sense to me.

这是有问题的代码:

var range = ReleaseSchedule.getRange(1,1,output_data_sheet.length,output_data_sheet[0].length);
range.setValues(output_data_sheet);

ReleaseSchedule是活动电子表格中的新工作表output_data_sheet是二维数组.我在脚本的较早版本中使用过类似的代码,但是由于某种原因,该代码引发了该异常,但我不知道为什么.任何帮助或想法都将不胜感激.

ReleaseSchedule is a new sheet in the active spreadsheet output_data_sheet is a 2 dimensional array. I have similar code working earlier in the script, but for some reason this one is throwing that exception and I can't figure out why. Any help or thoughts would be greatly appreciated.

推荐答案

我认为Exception: The number of columns in the data does not match the number of columns in the range. The data has 0 but the range has 15.错误的原因可能如下.

I thought that the reason of this error of Exception: The number of columns in the data does not match the number of columns in the range. The data has 0 but the range has 15. might be as follows.

  • output_data_sheet是二维数组.
  • output_data_sheet的第一个索引的数组长度为15
  • 除了第一个索引之外,索引中还包含一个空数组(数组长度为0).
  • output_data_sheet is 2 dimensional array.
  • The array length of the 1st index of output_data_sheet is 15
  • The empty array (the array length is 0) is included in the index except for 1st index.

output_data_sheet上的output_data_sheet用于setValues()时,可能会发生这种错误.为了消除此问题,如何进行以下修改?在这里,我想提出两种模式.

When above output_data_sheet is used for setValues(), such error might occur. In order to remove this issue, how about the following modifications? Here, I would like to propose 2 patterns.

在这种模式下,空元素将从output_data_sheet中删除.

In this pattern, the empty elements are removed from output_data_sheet.

var range = ReleaseSchedule.getRange(1,1,output_data_sheet.length,output_data_sheet[0].length);
range.setValues(output_data_sheet);

到:

output_data_sheet = output_data_sheet.filter(String);  // <--- Added
var range = ReleaseSchedule.getRange(1,1,output_data_sheet.length,output_data_sheet[0].length);
range.setValues(output_data_sheet);

模式2:

在此模式中,空元素和小于15的元素将添加元素,以保持output_data_sheet中每个索引的长度15.

Pattern 2:

In this pattern, the empty elements and the element smaller than the length of 15 add the elements for keeping the length of 15 for each index in output_data_sheet.

var range = ReleaseSchedule.getRange(1,1,output_data_sheet.length,output_data_sheet[0].length);
range.setValues(output_data_sheet);

到:

output_data_sheet = output_data_sheet.map(r => r.length == 15 ? r : r.concat(Array(15 - r.length).fill("")));  // <--- Added
var range = ReleaseSchedule.getRange(1,1,output_data_sheet.length,output_data_sheet[0].length);
range.setValues(output_data_sheet);

参考文献:

  • filter()
  • map()
  • References:

    • filter()
    • map()
    • 这篇关于例外:尽管从数据创建了范围,但数据中的列数与范围中的列数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆