title: Replace multiple GoTo by a loop in Google Apps Script
body: <p>/!\ ON GOOGLE APPS SCRIPT /!\</p>
  <p>I need an example to replace the existing GoTo statements in VBA with switch or while loop etc.</p>
  <p>For the moment, I managed to understand a little bit the principle of GoTo on this example:</p>
  <p>VBA INSTRUCTIONS :</p>
  <pre><code>date_deb:
      lig = lig + 1
      If Cells(lig, cc).Value = Cells(lig - 1, cc).Value And _
          Cells(lig, cc).Value &lt;&gt; &quot;&quot; Then GoTo date_deb
  </code></pre>
  <p>GAS INSTRUCTION (Google Apps Script):</p>
  <pre><code>  var ss = SpreadsheetApp.getActive().getSheetByName(&quot;GoTo&quot;);
    var lig = 2;
    var cc = 9;
    var temp = ss.getDataRange().getDisplayValues();
    date_deb:while(true){
      lig++;
      if(temp[lig][cc-1] == temp[lig-1][cc-1] &amp;&amp; temp[lig][cc-1] !=&quot;&quot;){
        continue date_deb;
      }
      break;
    }
  </code></pre>
  <p>These instructions allow me to increment the lig variable by 1 each time I find the same value in column 9.</p>
  <p>Now let's imagine that I add a new GoTo statement in VBA.</p>
  <p>VBA INSTRUCTIONS :</p>
  <pre><code>date_deb:
      lig = lig + 1
      If Cells(lig, cc).Value = Cells(lig - 1, cc).Value And _
          Cells(lig, cc).Value &lt;&gt; &quot;&quot; Then GoTo date_deb
      Rows(&quot;2:&quot; &amp; lig - 1).Select
      Selection.Delete Shift:=xlUp
  chg_sem:
      lig = 2
      day = Cells(lig, cc).Value
      week = CInt(Format(day, &quot;ww&quot;, vbMonday, vbFirstFourDays))
  date_eq:
      lig = lig + 1
      If Cells(lig, cc).Value = Cells(lig - 1, cc).Value And _
          Cells(lig, cc).Value &lt;&gt; &quot;&quot; Then GoTo date_eq
      If Cells(lig, cc).Value &lt;&gt; &quot;&quot; Then
          If week = CInt(Format(Cells(lig, cc).Value, &quot;ww&quot;, vbMonday, vbFirstFourDays)) _
              Then GoTo date_eq
      End If
      lig2 = lig - 1
  </code></pre>
  <p>Instinctively, I'll tell myself that in GAS, I should do :</p>
  <pre><code>  var ss = SpreadsheetApp.getActive().getSheetByName(&quot;GoTo&quot;);
    var lig = 2;
    var cc = 9;
    var temp = ss.getDataRange().getDisplayValues();
    date_deb:while(true){
      lig++;
      if(temp[lig][cc-1] == temp[lig-1][cc-1] &amp;&amp; temp[lig][cc-1] !=&quot;&quot;){
        continue date_deb;
      }
      break;
    }
    chg_sem:while(true){
      lig = 2;
      day = temp[lig-1][cc-1];
      week = [Using of FORMULA weekday etc.].
      break;
    }
  
  date_eq: while(true){
        lig ++;
        if(temp[lig][cc-1] == temp[lig-1][cc-1] &amp;&amp; temp[lig][cc-1] != &quot;&quot;){
           continue date_eq;
        }
        if[...]{
          continue date_eq;
        }
       break
  }
  </code></pre>
  <p>For example, if in the next part of my program I need to reuse the chg_sem:while(true) loop (or another one) how should I proceed in Google Apps Script?</p>
  <p>What I mean is that the GoTo in VBA can be used anywhere in the program, so what about in Google Apps Script?</p>
  <p>EDIT :</p>
  <p>As Marti said, I was on the wrong way to solve my issue, so I'm actually trying with this :</p>
  <pre><code>function date_deb(lig,cc){
  
      var file= &quot;file&quot;;
      var fichier_loc;
        var feuille1_loc;
  
      var lig = lig;
      var cc = cc;
  
      var ss = SpreadsheetApp.getActive();
      var s2 = ss.getSheets()[2];
  
      var temp;
  
      const r_local = &quot;Local_Temp&quot;;
      const r_logis = &quot;flux&quot;;
  
      fichier_loc = DriveApp.getFoldersByName(r_local).next().getFilesByName(file).next();
  
      temp = fichier_loc.getId();
  
      fichier_loc = SpreadsheetApp.openById(temp);
  
      feuille1_loc = fichier_loc.getSheets()[0];
  
      temp = feuille1_loc.getDataRange().getDisplayValues();
      
      date_deb: while(true){
        lig++;
        if(temp[lig][cc-1] == temp[lig-1][cc-1] &amp;&amp; temp[lig-1][cc-1]){
          continue date_deb;
        }
        break;
      }
  
      return lig;
  }
  
  </code></pre>
  <p>I know that it's not the best mean, if you have got something easier to reach my goal, I would appreciate that.</p>