2015-08-14 20 views
1

我可以使用一点帮助来计算.push如何与Multidimensional Arrays一起使用。我正在撰写一个简单的程序,用于从Google Sheets页面中的单元格收集信息,并自动生成需要更新的列的列表,并向团队发送电子邮件以查看可见性。我正在从VB.Net转换回JavaScript,所以我一直想使用VB中使用的方法。我已经阅读了这里的几篇文章,但似乎无法绕过它。我可视化二维数组的方式就像一个电子表格,但在例子中,我发现它与我的相似程度并不高。。多维数组超过两列推出

[雇员姓名] [状态] [开始日期]

我看到上面empRec[0,0]empRec[0,1]empRec[0,2]

但在推的例子,我看到empRec.push({"Employee Name", "Status"});

难道我明白,,分离第二个值中的值?是否会增加另一个,,并将值转换为第二个值的位置2?

感谢您输入高级内容。

代码剪断

// Extract the users who need review 
for (r=2; r <= eName.length - 1; r++) { 

if (eName[r] != "") { 

    if (eAcceptNDA[r] == "" || eAvailability[r] == "" || eType[r] == "" || eDays[r] == "") { 
    mailRecList[mailRecCount, 0] = eName[r]; 

    if (eAcceptNDA[r] == "") { 
     mailRecList[mailRecCount, 1] = "Needed"; 
    } else { mailRecList[mailRecCount, 1] = "----"; } 

    if (eAvailability[r] == "") { 
     mailRecList.push[mailRecCount, 2] = "Needed"; 
    } else { mailRecList[mailRecCount, 2] = "----"; } 

    if (eType[r] == "") { 
     mailRecList[mailRecCount, 3] = "Needed"; 
    } else { mailRecList[mailRecCount, 3] = "----"; } 

    if (eDays[r] == "") { 
     mailRecList[mailRecCount, 4] = "Needed"; 
    } else { mailRecList[mailRecCount, 4] = "----"; } 

    Logger.log(mailRecList[mailRecCount, 0] + ", " + mailRecList[mailRecCount, 1] + ", " + mailRecList[mailRecCount, 2] + ", " + mailRecList[mailRecCount, 3] + ", " + mailRecList[mailRecCount, 4]); 
    Logger.log(mailRecList.length/5); 

    } 
    mailRecCount += 1; 
} 
    } 

当我运行上面的代码只返回单行。当前在工作表中有3个值,它只返回最后一个值。 for loop以2开始的原因是因为前两个值是表单上的标题信息。

+0

在这一行中:'mailRecList.push [mailRecCount,2] =“Needed”;'你在'push'中使用括号。你不能那样做。您必须使用括号。我不知道你为什么使用赋值运算符。看起来好像你正在试图向数组中添加元素,就像你为对象添加一个“key/value”元素对一样。数组和对象不使用相同的语法。 [链接 - JavaScript - 推](http://www.w3schools.com/jsref/jsref_push.asp) –

+0

谢谢。让我看一看。我认为这是我以前没有取代的失败尝试的遗留物。我使用了赋值运算符,因为在之前的语言中是这样完成的。语言跳跃让我的大脑困惑。那么如果我想推到第三列,我会使用'mailRecList(eName [r],“Needed”,“Needed”);'??? – xxSithRagexx

回答

0

您需要构建内部数组,然后将其推送到您的二维数组中。由于这种更新你的代码片断所示:

function fillMailRecList() { 
 

 
    var eName =  ['Ignore1', 'Ignore2', 'Fred', 'Bill', 'Sam'] 
 
    var eAcceptNDA = ['',  '',  '',  'Ignore', ''] 
 
    
 
    var mailRecList = [] 
 
    var mailRecListItem = [] 
 
    var mailRecCount = 0 
 

 
    // Extract the users who need review 
 
    for (var r = 2; r < eName.length; r++) { 
 
    
 
    if (eName[r] !== "") { 
 
    
 
     if (eAcceptNDA[r] === "") { 
 
     
 
//  mailRecList[mailRecCount, 0] = eName[r] 
 
     
 
     mailRecListItem.push(eName[r]) 
 
     
 
     if (eAcceptNDA[r] == "") { 
 
      
 
//   mailRecList[mailRecCount, 1] = "Needed" 
 

 
     mailRecListItem.push("Needed") 
 
      
 
     } else { 
 
      
 
//   mailRecList[mailRecCount, 1] = "----"; 
 

 
      mailRecListItem.push("----") 
 
     } 
 
     
 
     // mailRecListItem is a reference to the array that will be used 
 
     // again next time around, so you need to take a new copy of it 
 
     // to keep. This also has the effect of clearing the working array 
 
     // ready for the next loop 
 
     mailRecList.push(mailRecListItem.splice(0, mailRecListItem.length))   
 
     }  
 
    }  
 
    } 
 
    
 
    Logger.log(mailRecList) // [[Fred, Needed], [Sam, Needed]] 
 
}

你可以看到这个运行here