循环后循环丢弃正确的值

时间:2016-02-29 17:37:03

标签: google-apps-script google-sheets

我需要帮助理解为什么我的for循环会在数组中找到正确的行号但是一旦它离开循环就会输出不正确的值。

Code.gs

  //Determines the row the Field Office is in
  for (var r = 0; r < dataValues.length; r++) {
    if (dataValues[r][1] == theFormObject.fieldOfficeFFO) {
      var q = dataValues[r][1];
      Logger.log('q inside loop: ' + q); //Row Value inside loop
      Logger.log('r inside loop: ' + r); // Row number inside loop
    }
  }
  Logger.log('q outside loop: ' + q); //Row Value outside loop
  Logger.log('r outside loop: ' + r); // Row number outside loop

Logger.logs enter image description here

1 个答案:

答案 0 :(得分:0)

你是对的罗宾,我在Java方面太过分了。当然你需要休息一下。否则循环继续,直到达到dataValues的长度,因此r是rizing。 中断告诉循环直接离开它而不进行任何进一步处理。

 //Determines the row the Field Office is in
 for (var r = 0; r < dataValues.length; r++) {
     if (dataValues[r][1] == theFormObject.fieldOfficeFFO) {
         var q = dataValues[r][1];
         Logger.log('q inside loop: ' + q); //Row Value inside loop
         Logger.log('r inside loop: ' + r); // Row number inside loop
         break;
     }
 }
 Logger.log('q outside loop: ' + q); //Row Value outside loop
 Logger.log('r outside loop: ' + r); // Row number outside loop