为什么我使用数组推送无法正常工作?

时间:2018-10-08 10:25:27

标签: arrays push

在下面的程序中,我首先构建两个数组“ a”和“ b”。然后,我尝试将一些数据从“ b”添加到“ a”。因此,数组“ a”的开头有2行,结尾应该有4行。虽然我确实得到了四行,但结果与我预期的并不完全相同。这是代码:

function mytry(){
   var b = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1:F2").getValues()
   b.push(["1","a","b","c","d","e"])
   b.push(["2","f","g","h","i","j"])
   b.shift()
   b.shift()
   Logger.log("b.length "+b.length)
   Logger.log("b "+b)
   var a = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1:C1").getValues()
   a.push(["100","A","B"])
   a.push(["200","C","D"])
   a.shift()
   Logger.log("a.length "+a.length)
   Logger.log("a "+a)
   Logger.log(" ")
   var temp=[]
   for (ro = b.length-1; ro >= 0; ro--){
     if (b[ro][5].toString() != "" ) {
       Logger.log(ro)
       temp[0]=b[ro][2];
       temp[1]=b[ro][4];
       temp[2]=b[ro][5];
       Logger.log(temp)
       a.push(temp)
       Logger.log("a.length "+a.length)
       Logger.log("a "+a)
     } 
   }  
}

该程序的日志为:

[18-10-08 06:09:37:807 EDT] b.length 2
[18-10-08 06:09:37:808 EDT] b 1,a,b,c,d,e,2,f,g,h,i,j
[18-10-08 06:09:37:811 EDT] a.length 2
[18-10-08 06:09:37:811 EDT] a 100,A,B,200,C,D
[18-10-08 06:09:37:812 EDT]  
[18-10-08 06:09:37:813 EDT] 1.0
[18-10-08 06:09:37:813 EDT] [g, i, j]
[18-10-08 06:09:37:814 EDT] a.length 3
[18-10-08 06:09:37:815 EDT] a 100,A,B,200,C,D,g,i,j
[18-10-08 06:09:37:815 EDT] 0.0
[18-10-08 06:09:37:816 EDT] [b, d, e]
[18-10-08 06:09:37:816 EDT] a.length 4
[18-10-08 06:09:37:817 EDT] a 100,A,B,200,C,D,b,d,e,

在运行程序结束时,我期望数组“ a”为“ 100,A,B,200,C,D,g,i,j,b,d,e”而不是“ 100” ,A,B,200,C,D,b,d,e,[空白]”。我已经知道解决此问题的几种方法,因此不需要解决方案。相反,我想了解“ temp”和“ a”数组的作用,以产生这样的意外结果(至少对我而言)。有人可以帮我吗?

0 个答案:

没有答案