我一直在Google床单的这个侧面项目工作了一段时间。 (link to copy)当它起作用时,它将在单独表(Miranda,Piper,Lowes和Golden)的第I列(是或否)中进行有条件的论证,并发送电子邮件至Master和B列中的地址工作表,以及通过在函数结尾处运行switch语句返回的任何电子邮件。通过我通过这个网站得到的建议,我已经能够大幅提高我的代码,我非常感谢!这里是我的最新障碍 -谷歌应用程序脚本 - for循环的麻烦(再次)
代码: The full thing is over here at JSFiddle
for (var ii = 0; ii < holdingData.masterStudNames.length; ii++) {
if (holdingData.masterStudNames[ii] === holdingData.selectedStudName) {
//Push Master column B to holdingData
holdingData.selectedTeacherEmail = holdingData.masterTeacherEmail[ii].toLowerCase();
//Push Master Column Q for selected student to holdingData
holdingData.selectedIEP = holdingData.allIEPContents[ii].toLowerCase();
}
}
for (var j = 0; j < holdingData.selectedIEP.length; j++) {
switch (holdingData.masterIEP[j]) {
case "dreier": holdingData.selectedIEPEmail = "[email protected]"; break;
case "forbes": holdingData.selectedIEPEmail = "[email protected]"; break;
case "green": holdingData.selectedIEPEmail = "[email protected]"; break;
case "marlo": holdingData.selectedIEPEmail = "[email protected]"; break;
case "mcDonald": holdingData.selectedIEPEmail = "[email protected]"; break;
case "nakkour": holdingData.selectedIEPEmail = "[email protected]"; break;
case "wright": holdingData.selectedIEPEmail = "[email protected]"; break;
default: break;
}
}
的问题: for循环看起来通过holdingData.selectedIEP [中的jsfiddle线79]是越来越举起。我相信它可能会使用它的[j]计数器错误,或者其他循环中的其中一个妨碍了计算。我承认,在一个函数中有3个for循环似乎对我来说太多了,但我想不出一种不同的方式来做到这一点。有什么明显的,我失踪?
我已经把代码削减了很多。所以我完全意识到可能还有更多改进的空间(我还在学习:)如果您有任何建议,我很乐意听到他们!
更新: 通过使用调试器,我发现的其中一个问题是,我的for循环只拉动选定列中的最后一个值。例如,在第一个循环中:
for (var h = 0; h < dataMaster.length; h++) {
//Push all student first names from master sheet for later use
var masterStudFirstName = masterSheetDataLooped[h][4];
}
这将masterStudentFirstName设置为“test”,它是第4列中的最后一个单元格。我需要的是获取该列中的所有值,并将它们存储在稍后可以调用的数组中。
我觉得我错过了简单的东西在这里... :)
你在脚本编辑器中以调试模式运行代码吗? –
好主,我怎么错过那个功能..... 。谢谢你,这将是非常有价值的:) – Jensen010
我在你的代码中加入了一些注释,以及一些更改(有关更改的意见和原始代码留在大头贴中,但注释掉了。) –