簡體   English   中英

While 循環不會停止,即使條件已經滿足。 它又持續了 9 次。 沒有邏輯意義

[英]While loop won't stop, even though the condition has been met. It continues for another 9 times. Makes no logical sense

我正在努力解決這個問題,因為它沒有意義。 while 循環的條件不滿足,但它在停止之前繼續執行另外 9 次。 我在 while 循環之前 console.log out 變量“donewithLoads”。 即使它是 True,while 循環也會繼續。 沒有意義,任何幫助表示贊賞。

function gatherLoads() {
  var donewithLoads =
    loadSheet
      .getRange(currentRow, 1)
      .getValue()
      .toString().length == 0;
  console.log('donewithLoads value: ' + donewithLoads);
  console.log('isFound Value: ' + isFound);
  console.log(
    loadSheet
      .getRange(currentRow, 1)
      .getValue()
      .toString().length
  );
  while (donewithLoads == false) {
    //while there are still invoice numbers, do this function.
    var isFound = false;
    var currentLoad = loadSheet.getRange(currentRow, 1).getValue(); //record invoice number to
    currentLoad;
    for (var s in allDispatcherSpreadSheets) {
      //search allDispatcherSheets //iterates through  different dispatcher spreadsheets
      var currentDispatchSpreadSheet = allDispatcherSpreadSheets[s];
      var currentSheets = currentDispatchSpreadSheet.getSheets();
      for (var o in currentSheets) {
        //iterates through different sheets in spreadsheet
        var currentSingleSheet = currentSheets[o];
        if (currentSingleSheet.isSheetHidden() != true) {
          // if sheet is visible and not hidden
          //testerstuff
          var testsheetname1 = currentSingleSheet.getName();
          testsheetname1.toString();
          var testSpreadSheetName = currentDispatchSpreadSheet.getName();
          testSpreadSheetName.toString();
          //currentRow2 is the row in the working single sheet that will  be iterated to find   matching invoice number
          for (
            var currentRow2 = 2;
            currentRow2 <= numRowsDispatchSheet;
            currentRow2++ //iterates through rows in column 14 (invoice)
          ) {
            var currentLoad2 = currentSingleSheet
              .getRange(currentRow2, 14)
              .getValue();
            if (currentLoad2 == currentLoad) {
              //if found
              var targetRange = currentSingleSheet
                .getRange(currentRow2, 1, 1, 15)
                .getValues();
              foundLoads
                .getRange(foundLoadscounter + 2, 1, 1, 15)
                .setValues(targetRange);
              foundLoadscounter++;
              isFound = true;
              if (isFound == true) {
                loadSheet.getRange(currentRow, 2).setValue('found');
                loadSheet.getRange(currentRow, 1).setBackground('green');
                currentRow++;
                processedLoadscounter++;
                console.log(
                  'load found: ' +
                    'row ' +
                    currentRow +
                    'processed loads:' +
                    processedLoadscounter +
                    ' found Loads: ' +
                    foundLoadscounter
                );
                console.log(loadSheet.getRange(currentRow, 1).getValue());
                gatherLoads(); // start function again
                break;
              }
            }
          }
        }
      }
    }
    if (isFound == false) {
      loadSheet.getRange(currentRow, 2).setValue('not found');
      loadSheet.getRange(currentRow, 1).setBackground('red');
    }
    currentRow++;
    processedLoadscounter++;
    console.log(
      'load found: ' +
        'row ' +
        currentRow +
        'processed loads:' +
        processedLoadscounter +
        ' found Loads: ' +
        foundLoadscounter
    );
    console.log(loadSheet.getRange(currentRow, 1).getValue());
    gatherLoads();
    break;
  }
}

該代碼遍歷了 9 行數據,然后遇到了空白,但仍繼續執行。 該代碼確實有效,它標記負載“找到”或“未找到”並更改顏色。

控制台 output:

10:02:12 AM Notice  Execution started

10:02:12 AM Info    donewithLoads value: false

10:02:12 AM Info    isFound Value: undefined

10:02:12 AM Info    6

10:02:24 AM Info    load found: row 3processed loads:1 found Loads: 1

10:02:24 AM Info    128361

10:02:24 AM Info    donewithLoads value: false

10:02:24 AM Info    isFound Value: undefined

10:02:24 AM Info    6

10:02:31 AM Info    load found: row 4processed loads:2 found Loads: 2

10:02:31 AM Info    128362

10:02:31 AM Info    donewithLoads value: false

10:02:31 AM Info    isFound Value: undefined

10:02:31 AM Info    6

10:02:37 AM Info    load found: row 5processed loads:3 found Loads: 3

10:02:37 AM Info    128363

10:02:37 AM Info    donewithLoads value: false

10:02:37 AM Info    isFound Value: undefined

10:02:37 AM Info    6

10:02:44 AM Info    load found: row 6processed loads:4 found Loads: 4

10:02:44 AM Info    128365

10:02:44 AM Info    donewithLoads value: false

10:02:44 AM Info    isFound Value: undefined

10:02:44 AM Info    6

10:02:45 AM Info    load found: row 7processed loads:5 found Loads: 5

10:02:45 AM Info    129488

10:02:45 AM Info    donewithLoads value: false

10:02:45 AM Info    isFound Value: undefined

10:02:45 AM Info    6

10:02:55 AM Info    load found: row 8processed loads:6 found Loads: 5

10:02:55 AM Info    128369

10:02:55 AM Info    donewithLoads value: false

10:02:55 AM Info    isFound Value: undefined

10:02:55 AM Info    6

10:02:55 AM Info    load found: row 9processed loads:7 found Loads: 6

10:02:56 AM Info    128371

10:02:56 AM Info    donewithLoads value: false

10:02:56 AM Info    isFound Value: undefined

10:02:56 AM Info    6

10:02:56 AM Info    load found: row 10processed loads:8 found Loads: 7

10:02:56 AM Info    128372

10:02:56 AM Info    donewithLoads value: false

10:02:56 AM Info    isFound Value: undefined

10:02:56 AM Info    6

10:02:57 AM Info    load found: row 11processed loads:9 found Loads: 8

10:02:57 AM Info    

10:02:57 AM Info    donewithLoads value: true

10:02:57 AM Info    isFound Value: undefined

10:02:57 AM Info    0

10:03:05 AM Info    load found: row 12processed loads:10 found Loads: 8

10:03:05 AM Info    

10:03:05 AM Info    donewithLoads value: true

10:03:05 AM Info    isFound Value: undefined

10:03:05 AM Info    0

10:03:13 AM Info    load found: row 13processed loads:11 found Loads: 8

10:03:13 AM Info    

10:03:13 AM Info    donewithLoads value: true

10:03:13 AM Info    isFound Value: undefined

10:03:13 AM Info    0

10:03:20 AM Info    load found: row 14processed loads:12 found Loads: 8

10:03:20 AM Info    

10:03:20 AM Info    donewithLoads value: true

10:03:20 AM Info    isFound Value: undefined

10:03:20 AM Info    0

10:03:27 AM Info    load found: row 15processed loads:13 found Loads: 8

10:03:27 AM Info    

10:03:28 AM Info    donewithLoads value: true

10:03:28 AM Info    isFound Value: undefined

10:03:28 AM Info    0

10:03:29 AM Info    load found: row 16processed loads:14 found Loads: 8

10:03:29 AM Info    

10:03:29 AM Info    donewithLoads value: true

10:03:29 AM Info    isFound Value: undefined

10:03:29 AM Info    0

10:03:30 AM Info    load found: row 17processed loads:15 found Loads: 8

10:03:30 AM Info    

10:03:30 AM Info    donewithLoads value: true

10:03:30 AM Info    isFound Value: undefined

10:03:30 AM Info    0

10:03:32 AM Info    load found: row 18processed loads:16 found Loads: 8

10:03:32 AM Info    

10:03:32 AM Info    donewithLoads value: true

10:03:32 AM Info    isFound Value: undefined

10:03:32 AM Info    0

10:03:34 AM Info    load found: row 19processed loads:17 found Loads: 8

10:03:34 AM Info    

10:03:34 AM Info    donewithLoads value: true

10:03:34 AM Info    isFound Value: undefined

10:03:34 AM Info    0

10:03:35 AM Notice  Execution completed

@VLAZ 提出了一個有效的觀點。 這就是我的建議:

步驟 1 不使用變量 doneWithLoad,而是定義並使用 function numLoads 來計算加載次數:

function numLoads(ls) {
//ls is a loadSheet object
return ls.getRange(currentRow, 1).getValue().toString().length;
}

第 2 步在 while 循環控制中調用 numLoads 以在每次迭代時更新其值,如 @VLAZ 所建議的那樣。

while(numLoads(loadSheet) > 0) {
//Your code here
}

暫無
暫無

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

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