在条件满足Javascript后继续迭代for循环

时间:2017-04-27 19:05:21

标签: javascript arrays google-apps-script

我写过有一个for循环,它会运行额外的代码if (sheetData[i] !== "")

理想情况下,我希望在条件之后运行以下代码,但然后循环回来并再次运行它以匹配条件的下一个项目。关于如何实现这一目标的任何想法?

function getSheetSectionDataTest(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Params"); // get sheet
  var sheetData = sheet.getDataRange().getValues(); //get all sheet data
  var sectionNames = normalizeHeaders(normalizeHeaders(sheet.getRange('A1:A').getValues()));  
  var sectionData = []; // main array to contain all section data

  // create an array for each section
  for(h = 0; h < sectionNames.length; h++) {
   sectionData[sectionNames[h]]  /*property name or key of choice*/
           = []; 
  }

  for (var i = 0; i < sheetData.length; i++){ //loop through each row in the spreadsheet
      var sectionName = normalizeHeaders(sheetData[i]); //return normalized camelCase section name found in column A.

      if (sheetData[i] !== ""){ // Test - stop at a cell that matches the criteria and return the data table.    
          var headerRow = normalizeHeaders(sheetData[i+1]); //define and normalize the table headers
          for (var j = i+2; j < sheetData.length; j++) { //loop through each row in the data table.
              if (sheetData[j][1] !== ""){ //if there are contents in the table keep looping.
                var obj = {};
                sectionData[sectionName[0]].push(obj); // Need to replace ranges with a dynamic variable that gives us the current sectionName value as an object?

                for (var rowColumn = 0; rowColumn < headerRow.length; rowColumn++) { //loop through each column in the current row of the table.
                  obj[headerRow[rowColumn]]=sheetData[j][rowColumn+1];
                }              
              }
              else { //stop when an empty cell is reached
                return sectionData; //when the data table loop runs into an empty cell stop loop and return the data
              }
          }
      }   
    }
};

0 个答案:

没有答案