簡體   English   中英

Javascript 中的循環從 Google Apps 腳本中的錯誤索引開始

[英]Loops in Javascript starting at wrong index in Google Apps Script

我有一個表格,第四行填寫了學生姓名,根據 class 中有多少學生,我需要不同數量的頁面。 根據床單的類型,每張床單可容納 8 或 9 名學生。

我有一個外循環來處理每張紙,還有一個內循環來處理該行上的每個名稱。 只要學生人數是 8 或 9 的倍數(取決於表格),就會跳過姓氏。 當我開始調查問題並檢查計數器的值時,我注意到

  1. 記錄器 output 中打印的第一件事是行“i value:”及其值 1
  2. 索引 i 從 1 開始,h 從 7 開始,盡管初始化為 0

這非常令人困惑(更不用說令人沮喪了)。

這是代碼:

function insertNames(mealcountDoc, className, namesInTheClass, constructionDetails){
Logger.log("\nEntered 'insertNames' function\n");
  namesInTheClass = namesInTheClass.sort();
  var newlyCreatedSheet = mealcountDoc.getSheetByName(className);

// loop for each row in the tab
  var numOfRowsOfMealcounts = Math.ceil(namesInTheClass.length/constructionDetails.namesPerRow);
  for(var i = 0; i < numOfRowsOfMealcounts; i++){
  Logger.log("i value just after entering outer loop: " + i);
//  Logger.log("h value just after entering outer loop: " + h);
    //    var countOfStudentsEntered = 0;
    var classDetailsInfoRow = (i * 41 + 3);
    var footerRow = (i * 41 + 41);
    newlyCreatedSheet.getRange(classDetailsInfoRow - 2, mealcountDataPlacement.className).setValue(className).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(classDetailsInfoRow, mealcountDataPlacement.nameOfInstitution).setValue(mealcountDoc.getName().slice(0, mealcountDoc.getName().indexOf("-")-1)).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(classDetailsInfoRow, mealcountDataPlacement.agreementNum).setValue(constructionDetails.agreementNumber).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(classDetailsInfoRow, mealcountDataPlacement.facilityNameAndNum).setValue(mealcountDoc.getName().slice(0, mealcountDoc.getName().indexOf("-")-1)).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(classDetailsInfoRow, mealcountDataPlacement.monthYear).setValue(constructionDetails.monthAsAWord + " " + constructionDetails.currentYear).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(classDetailsInfoRow, mealcountDataPlacement.serviceDays).setValue(constructionDetails.numOfServiceDays).setHorizontalAlignment('Left');
    newlyCreatedSheet.getRange(footerRow, mealcountDataPlacement.pageNumber).setValue("Page " + (i + 1) + " of " + numOfRowsOfMealcounts).setHorizontalAlignment('Left');
    
    var startingRow = (i * 41 + 4);
    var loopLimit = constructionDetails.namesPerRow
    
    // loop for each name
    // if-else statement deals with the final row containing less than a full row of names (8 or 9, dep on type of sheet)
    if(numOfRowsOfMealcounts - i <= 1 && namesInTheClass.length % constructionDetails.namesPerRow != 0){
      loopLimit = namesInTheClass.length % constructionDetails.namesPerRow;
    }
    for(var h = 0; h < loopLimit; h++){
      Logger.log("\n");
      Logger.log(constructionDetails);
      Logger.log(namesInTheClass);
      Logger.log("i value: " + i );
      Logger.log("h value: " + h );
      Logger.log(namesInTheClass[i * constructionDetails.namesPerRow + h][1]+ "\n" + namesInTheClass[i * constructionDetails.namesPerRow + h][0]);
      newlyCreatedSheet.getRange(startingRow,h * constructionDetails.numOfColsPerEntry + 2,1,1).setValue(namesInTheClass[i * constructionDetails.namesPerRow + h][1]+ "\n" + namesInTheClass[i * constructionDetails.namesPerRow + h][0]);
    }    
  }

日志:


Stackdriver logs
Aug 19, 2020, 4:12:44 PM
Info
i value here: 1
Aug 19, 2020, 4:12:44 PM
Info
h value here: 7
Aug 19, 2020, 4:12:44 PM
Info
Cayden
Jimenez
Aug 19, 2020, 4:12:44 PM
Info
i value just after entering outer loop: 2
Aug 19, 2020, 4:12:44 PM
Info

Aug 19, 2020, 4:12:44 PM
Info
{currentYear=2020, initNumberOfCols=5.0, agreementNumber=0.0, firstSaturday=5.0, numOfColsPerEntry=4.0, numOfServiceDays=22.0, lastDateOfMonth=30.0, trackingColumnsLength=33.0, namesPerRow=8.0, mealcountType=Attendance, monthAsAWord=September}
Aug 19, 2020, 4:12:44 PM
Info
[[Barr, Serena, , , Fri Jun 19 00:00:00 GMT-07:00 2020, First Day of School, Shelly's Infants], [Bonilla, Klay, , , Wed Jan 18 00:00:00 GMT-08:00 2017, First Day of School, Shelly's Infants], [Brookes, River, , , Tue Feb 07 00:00:00 GMT-08:00 2017, First Day of School, Shelly's Infants], [Burnett, Kaden, , , Sun Sep 11 00:00:00 GMT-07:00 2016, First Day of School, Shelly's Infants], [Coulson, Camille, , , Mon Mar 23 00:00:00 GMT-07:00 2020, First Day of School, Shelly's Infants], [Cruz, Abdulahi, , , Wed Jan 10 00:00:00 GMT-08:00 2018, First Day of School, Shelly's Infants], [Duarte, Ben, , , Wed Jan 04 00:00:00 GMT-08:00 2017, First Day of School, Shelly's Infants], [Franco, Shaunie, , , Tue Feb 07 00:00:00 GMT-08:00 2017, First Day of School, Shelly's Infants], [Fuller, Roscoe, , , Wed Jan 10 00:00:00 GMT-08:00 2018, First Day of School, Shelly's Infants], [Giles, Gwen, , , Fri Jun 23 00:00:00 GMT-07:00 2017, First Day of School, Shelly's Infants], [Goodman, Jeremiah, , , Thu Aug 31 00:00:00 GMT-07:00 2017, First Day of School, Shelly's Infants], 

它繼續。

請注意,日志中的第一個條目不是代碼首先調用的打印條目。

Apps Script 對SpreadsheetApp的調用異步運行,這會使您的調用順序混亂。

  1. 您應該通過單個setValues()請求替換多個setValue() ) 請求,請參閱最佳實踐

  2. 您應該在對SpreadsheetApp的每個請求之后放置一個SpreadsheetApp.flush()調用,以確保循環迭代僅在對SpreadsheetApp的所有調用成功完成后繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM