你的阵列是infoData
,这将是这样的:
[
[ A2, B2, C2, D2, E2 ], // infoData[0]
[ A3, B3, C3, D3, E3 ], // infoData[1]
...
[ A22, B2, C22, D22, E22 ] // infoData[20]
]
您的循环通过infoData [0]的元素逐列,寻找匹配location
的j
。这看起来很奇怪。通常情况下,我们希望“记录”在行中,其值为location
,出现在特定列中。
如果确实location
为A
,或在阵列中列0,那么你正在寻找的循环将是:
var newUserInfo = [];
for (var row=0; row<infoData.length; ++row){
if (location == infoData[row][0]);{
// Found matching location; get info for new user
for (var col=1; col<infoData[row].length; ++col) {
newUserInfo.push(infoData[row][col]);
}
break; // Exit after copying the matching row
}
}
if (newUserInfo.length > 0) {
// Now, the newUserInfo array contains the data that was
// to the RIGHT of the location column.
destSheet.getRange(destSheet.lastRow()+1,1,1,newUserInfo.length).setValues([newUserInfo]);
}
编辑:更新的代码...现在搜索将退出一旦匹配行已被找到,其数据已被复制到newUserData
,这被视为一个数组。在搜索循环之后,可以将复制的值写入目标工作表 - 在此示例中,假定它们作为工作表底部的新行添加。
我很抱歉没有提供有关该项目的更多详情。 @Mogsdad你是正确的,位置在A.我试图复制匹配“位置”行的B,C,D的数据。然后,该数据将被粘贴到另一个工作表中。 – dericcain
另外,在变量++ i之前递增和在它之后递增有什么区别? – dericcain
这里使用它的方式,不管你是预先还是后期增量都没有区别。 – Mogsdad