title: javascript output time interval
body: <p>I'm working to automate a google sheet to google calendar, but I'm stuck.</p>
  
  <p>I have an array of strings that correspond to hours</p>
  
  <pre><code>ex: time = [8, 9, 10, 2, 3, 4]
  </code></pre>
  
  <p>and want to an output a string</p>
  
  <pre><code>ex: range = "8-11, 2-5"
  </code></pre>
  
  <p>I need to write this in google app script, any suggestions?</p>
  
  <p>I'm new to google app script and having a hard time writing the function. My initial process was to convert the array of strings to military time integers, and create two for loops, but I'm sure there is a more efficient way to do this.</p>
  
  <p>Thanks for the help!</p>
  
  <p>This is my current code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var time = [8, 9, 10, 2, 3, 4]
  
  // if (currentTime == 13) {currentTime -= 12;}
  function timeRange(cellInput, hourList) {
    var start = parseInt(hourList[0]);
    for (var i = 1; i &lt; hourList.length; ++i) {
      if (hourList[i] == start + i) {
        var end = parseInt(hourList[i]);
      } else {
        cellInput.setValue(start + " - " + (end + 1));
      }
    }
  }
  
  function soloTime(cellInput, hour) {
    //convert hour string to hour
    var hour = parseInt(hour)
    var start = hour
    var end = hour + 1
    cellInput.setValue(start + "-" + end);
  }</code></pre>
  </div>
  </div>
  </p>