title: The coordinates of the target range are outside the dimensions when using getValue within loop
body: <p>I have an google sheet app script that I am running manually. When executing the clearForecast() function, I receive the following exception:</p>
  
  <blockquote>
    <p>Exception: The coordinates of the target range are outside the
    dimensions of the sheet. (line 30, file "Code")</p>
  </blockquote>
  
  <pre><code>sh = SpreadsheetApp.getActiveSpreadsheet();
  
  function clearForecast(){
    moveProjectionActions();
    // sh.getRange("forecast!A2:D").clearContent();
  
  }
  
  function moveProjectionActions() {
    var locSheets = ["clt","chi","kan","mil","nnj","nva","nyc","sfl","uny"];
  
    for (var i = 0; i &lt; locSheets.length; i++) {
      friendlyName = locSheets[i];
  
      // the numComments line below is line 30.
      numComments = sh.getSheetByName(friendlyName).getRange("Y13").getValue();
  
      if (numComments &gt; 0) {
        locValues = sh.getSheetByName(friendlyName).getRange('U6:Y12').getValues();
  
        shFrom = friendlyName + "!U6:Y12";
        var source = sh.getRange(shFrom);
        var destSheet = sh.getSheetByName("forecast_log");
  
        var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
        source.copyTo (destRange, {contentsOnly: true});
  
        sh.getSheetByName(friendlyName).getRange('X6:Y12').clearContent();
      }  
    }
  }
  </code></pre>
  
  <p>The formula in the Y13 cell is:</p>
  
  <blockquote>
    <p>=counta(X6:X12)</p>
  </blockquote>
  
  <p>This exception occurs ONLY when there is a number greater than 0. If it's 0, or text, all is well.</p>
  
  <p>Strangely, I made another function:</p>
  
  <pre><code>function testFunc() {
    num_comments = sh.getSheetByName("clt").getRange("Y13").getValue();
  }
  </code></pre>
  
  <p>When this is executed, no exception. I have been hours trying to get to the bottom of this and any assistance is greatly appreciated.</p>