简体   繁体   English

Google 应用程序脚本 - for 循环问题(再次)

[英]Google apps script - trouble with for loops (again)

I've been working on this side project in Google sheets for a while now.我已经在 Google 表格中从事这个副项目有一段时间了。 ( link to copy ) When it works, it would take a conditional argument in column I (yes or no) of the Individual sheets (Miranda, Piper, Lowes, and Golden) and send an email off to addresses in column B and Q of the Master sheet, as well as whatever email is returned by running through the switch statement at the end of the function. 复制链接)当它工作时,它将在个人工作表(Miranda、Piper、Lowes 和 Golden)的 I 列(是或否)中接受一个条件参数,并向 B 列和 Q 列中的地址发送电子邮件主表,以及通过运行函数末尾的 switch 语句返回的任何电子邮件。 Through suggestions I've gotten through this site, I've been able to drastically improve my code, and I am very grateful!通过我通过这个网站得到的建议,我已经能够极大地改进我的代码,我非常感激! Here's my latest hurdle -这是我最近的障碍-

Code: The full thing is over here at JSFiddle代码: 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 = "Fake-email1@hotmail.com"; break;
           case "forbes": holdingData.selectedIEPEmail = "Fake-email2@hotmail.com"; break;
           case "green": holdingData.selectedIEPEmail = "Fake-email3@hotmail.com"; break;
           case "marlo": holdingData.selectedIEPEmail = "Fake-email4@hotmail.com"; break;
           case "mcDonald": holdingData.selectedIEPEmail = "Fake-email5@hotmail.com"; break;
           case "nakkour": holdingData.selectedIEPEmail = "Fake-email6@hotmail.com"; break;
           case "wright": holdingData.selectedIEPEmail = "Fake-email7@hotmail.com"; break;
           default: break;
       } 
    } 

The Issue: The for loop that looks through holdingData.selectedIEP [line 79 in JSFiddle] is getting held up.问题:查看 holdingData.selectedIEP [JSFiddle 中的第 79 行] 的 for 循环被阻塞了。 I believe it may be using its [j] counter wrong, or maybe one of the other loops is getting in the way.我相信它可能使用它的 [j] 计数器错误,或者其他循环之一正在妨碍。 I admit, having 3 for loops in a function seems like too many to me, but I couldn't think of a different way to do it.我承认,一个函数中有 3 个 for 循环对我来说似乎太多了,但我想不出不同的方法来做到这一点。 Is there anything glaringly obvious that I'm missing?有什么明显的我遗漏了吗?

I have already cut the code down by.....a lot.我已经将代码减少了......很多。 So I'm fully aware that there is probably room for more improvement (I'm still learning:) If you have any suggestions, I'd love to hear them!所以我完全意识到可能还有更多改进的空间(我仍在学习中:)如果您有任何建议,我很乐意听取他们的意见!

UPDATE: By using the debugger, One of the issues I've found is that my for loops are only pulling the last value in the selected column.更新:通过使用调试器,我发现的问题之一是我的 for 循环只提取所选列中的最后一个值。 For example, in the first loop:例如,在第一个循环中:

for (var h = 0; h < dataMaster.length; h++) { 
  //Push all student first names from master sheet for later use
  var masterStudFirstName = masterSheetDataLooped[h][4];
}

This sets masterStudentFirstName as "test" which is the last cell in the 4th column.这将 masterStudentFirstName 设置为“test”,这是第 4 列中的最后一个单元格。 What I need is to grab all of the values in that column, and store them in an array that I can call later.我需要的是获取该列中的所有值,并将它们存储在一个我可以稍后调用的数组中。

I feel like I missed something simple here... :)我觉得我在这里错过了一些简单的东西...... :)

After some troubleshooting, and a look at a debugger as suggested by Karl_S (Thank you!) I now have refined the issue I was having, and gotten it solved.经过一些故障排除,并按照 Karl_S 的建议查看调试器(谢谢!)我现在已经完善了我遇到的问题,并得到了解决。

The problem with my code, was that the for loops were not pulling the correct data.我的代码的问题是 for 循环没有提取正确的数据。 I have 4 tabs on this sheet, and each of them contain some data I need to pull.我在这张表上有 4 个标签,每个标签都包含一些我需要提取的数据。 In the first case (the "Master" tab) I need to pull the values of column E "Student First Name" and store them in an array object within my master "holdingData" object.在第一种情况下(“Master”选项卡),我需要提取 E 列“Student First Name”的值并将它们存储在我的主“holdingData”对象内的数组对象中。

My earlier approach was to just loop through that column:我之前的方法是循环遍历该列:

for (var h = 0; h < dataMaster.length; h++) { 
  //Push all student first names from master sheet for later use
  var masterStudFirstName = masterSheetDataLooped[h][4];
}

But I found that, while this is useful, it would require me to write conditions to make other parts of the code functional.但我发现,虽然这很有用,但它需要我编写条件以使代码的其他部分起作用。 So what I ended up doing is this (posting a few more variables for context):所以我最终做的是这个(发布更多上下文变量):

//Set a new object to hold data
var holdingData = new Object();
holdingData.studFirstNames = [];
holdingData.allSheetsNamesAndIds = [[sheets[1], sheets[1].getSheetName(),
543328548], [sheets[3], sheets[3].getSheetName(), 1377446903], [sheets[4], 
sheets[4].getSheetName(), 748028814], [sheets[5], sheets[5].getSheetName(), 
431951580], [sheets[6], sheets[6].getSheetName(), 193755985]];

//Skippng a few lines....

//Load the master sheet, get its entire range, and then get all values
SpreadsheetApp.setActiveSheet(holdingData.allSheetsNamesAndIds[0][0]);
var numRowsMaster = masterSheet.getLastRow();
var dataRangeMaster = masterSheet.getRange(2, 1, numRowsMaster - 1, 26);
var dataMaster = dataRangeMaster.getValues();

//Loop dataMaster and pull the relevant columns
for (var h = 0; h < dataMaster.length; ++h) {
  holdingData.studFirstNames.push(dataMaster[h][4]);
}

Basically, this loops through every row of the Master tab.基本上,这会遍历 Master 选项卡的每一行。 Then every time we get to the 4th column, we push the contents of that column within the specific row to the holdingData array.然后每次到达第 4 列时,我们将特定行中该列的内容推送到holdingData 数组。

To sum up, the loop is saying "go through dataMaster row by row."总而言之,循环是说“逐行通过 dataMaster”。 then we say "every row you loop through push the contents of the 4th column within that row to holdingData.studFirstNames"然后我们说“您循环的每一行都将该行中第 4 列的内容推送到holdingData.studFirstNames”

You could certainly take a different approach at how you store data (variable) For this application, it's easier for me pass data upstairs to a holding Object.您当然可以采用不同的方法来存储数据(变量) 对于此应用程序,我可以更轻松地将楼上的数据传递给一个持有对象。 I realize I'm mostly explaining this to myself :) but I hope in the future someone comes across this post and finds a quicker answer than I did.我意识到我主要是在向自己解释这个 :) 但我希望将来有人看到这篇文章并找到比我更快的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM