意外地推送嵌套数组和信息 [英] push nesting array unexpectedly and info

查看:60
本文介绍了意外地推送嵌套数组和信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要::在google apps脚本(表格为v8)中,我试图将一个数组元素切出一个数组并将其推入另一个数组中.数组将是[ [] [] [] ],但我越来越像[ [] [[]] [[]] ].我(我认为)正在使用它来获取长度范围从getrange到setvalues的getRange/setValues带来以后的问题.我将把问题分开,但是我不知道它们是否相关.

Summary: In google apps script (v8 for sheets), I'm trying to slice an array element out of one array and push it into a different array. Array will be [ [] [] [] ] but I'm getting like [ [] [[]] [[]] ]. This (I think) is creating later issues with using that to getRange/setValues where the length changes from getrange to setvalues. I would separate the issues, but I don't know if they're related.

这对我来说似乎很多,但是在发布简短答案时,我被要求提供可重现的代码,因此会更长.

This seems like a lot to me, but when posting a short answer, I was asked for reproducible code, so this is longer.

问题1:数组意外嵌套

所需的结果1 :单个嵌套(适用于粘贴到Google表格中).

Desired outcome1: single nesting (suitable for pasting into google sheets).

问题2::尝试获取Range/setValues时,get范围中的data [0] .length与同一行代码中setValues(data)所需的列不匹配.

Issue2: When trying to getRange/setValues, the data[0].length in get range doesn't match the columns needed for setValues(data) in the same line of code.

所需的结果2::getRange数据[0] .length = setValues数据长度,用于getRange和setValues(列)

Desired outcome2: getRange data[0].length = setValues data length for purposes of getRange and setValues (columns)

代码: 有关代码的信息(不能放在下面或将其出错): 函数tester2进行推/切片并返回对象上的变异数组. 函数tester1拉出返回的对象并记录两个数组. (不确定这是否相关,但这是否是较大代码的一部分). 日志位于代码下方.

Code: info about code (can't put below or it errors it out): Function tester2 does push/slice and returns the mutated arrays as on object. Function tester1 pulls the returned object and logs two arrays. (not sure if this was relevant or not but it is part of the larger code). Logs are below the Code.


    function tester2(){
    
    var arrnew = tester();
    
    llog(arrnew.r1);
    llog(arrnew.r1.length);
    llog(arrnew.r1[0].length);
    llog(arrnew.r2);
    llog(arrnew.r2.length);
    llog(arrnew.r2[0].length);
    
    
    var sh = SpreadsheetApp.getActive().getSheetByName('temp');
    sh.getRange(1,1,arrnew.r1.length,arrnew.r1[0].length).setValues(arrnew.r1);
    sh.getRange(1,1,arrnew.r2.length,arrnew.r2[0].length).setValues(arrnew.r2);
    
    }
    
    function tester(){
    
    var arr1 = [[1,2,3],[4,5,6],[7,8,9],[112,211,311],[411,511,611],[711,811,911]];
    var arr2 = [[11,12,13],[14,15,16],[17,18,19]];
    var arr3 = [['hdr1','hdr2','hdr3']];
    
    for (var r=1;r<arr1.length;r++){
      if(arr1[r][0] % 2 == 0) {
        arr3.push(arr1.splice(1,1));
      }
    }
    
      var rtn = {r1: arr1, r2: arr3};
      return rtn;
    }

日志:

上面的日志在一起并为了便于匹配. (这不是代码,但我不断收到缩进您的代码"错误...

the logs above are together and in order for easy matching. (this isn't code but I keep getting 'indent your code' error...

>[20-11-11 19:50:47:811 EST] [[1.0, 2.0, 3.0], [112.0, 211.0, 311.0], [411.0, 511.0, 611.0], [711.0, 811.0, 911.0]] 
>
>[20-11-11 19:50:47:815 EST] 4.0 
>
>[20-11-11 19:50:47:818 EST] 3.0 
>
>[20-11-11 19:50:47:821 EST] [[hdr1, hdr2, hdr3], [[4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0]]]  *** notice double-nesting
>
>[20-11-11 19:50:47:823 EST] 3.0 
>
>[20-11-11 19:50:47:826 EST] 3.0 
>
>[20-11-11 19:50:48:240 EST] Exception: The number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 3. at tester2(dcmv1:229:55)
>*** code line 229 begins:  sh.getRange(1,1,arrnew.r1

推荐答案

说明:

问题确实是第二和第三要素中的双括号. 第二个和第三个元素具有[[]]的结构,因此它们的长度是1,而不是第一个元素的3.您可以看到,如果执行Logger.log(arrnew.r2[1].length),则将得到1,因为方括号[[]]中只有一个元素[].但是第一个括号[hdr1, hdr2, hdr3]中有3个元素hdr1, hdr2, hdr3,因此Logger.log(arrnew.r2[0].length)返回3.

Explanation:

The issue is indeed the double brackets in the second and third element. The second and third element has the structure of [[]] therefore their length is 1 and not 3 as it is the case for the first element. You can see that if you do Logger.log(arrnew.r2[1].length) you will get 1 because there is only one element [] inside the brackets [[]]. But there are 3 elements hdr1, hdr2, hdr3 inside the first bracket [hdr1, hdr2, hdr3] therefore Logger.log(arrnew.r2[0].length) returns 3.

我不确定您要达到的目标,但是您可以修复"通过引入具有正确格式的新数组来实现:

I am not sure what you are trying to achieve but you can "fix" that by introducing a new array with the correct format:

data2 = [arrnew.r2[0],...arrnew.r2[1],...arrnew.r2[2]]

,并使用它代替arrnew.r2.

 function tester2(){
    
    var arrnew = tester();
    data2 = [arrnew.r2[0],...arrnew.r2[1],...arrnew.r2[2]]  
    var sh = SpreadsheetApp.getActive().getSheetByName('temp');
    sh.getRange(1,1,arrnew.r1.length,arrnew.r1[0].length).setValues(arrnew.r1);
    sh.getRange(1,1,arrnew.r2.length,arrnew.r2[0].length).setValues(data2);
    
    }
    
    function tester(){
    
    var arr1 = [[1,2,3],[4,5,6],[7,8,9],[112,211,311],[411,511,611],[711,811,911]];
    var arr2 = [[11,12,13],[14,15,16],[17,18,19]];
    var arr3 = [['hdr1','hdr2','hdr3']];
    
    for (var r=1;r<arr1.length;r++){
      if(arr1[r][0] % 2 == 0) {
        arr3.push(arr1.splice(1,1));
      }
    }
    
      var rtn = {r1: arr1, r2: arr3};
      return rtn;
    }

这篇关于意外地推送嵌套数组和信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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