title: How to add a 2D array several times at the end of an existing 2D array using a for loop?
body: <p><strong>Starting point (boundary conditions):</strong></p>
  
  <ul>
  <li>There is a set of recurring events each week (weekly events section
  <code>row 9:11</code>).</li>
  <li>My colleagues can set a starting date <code>A6</code> and how many weeks they'd like to add <code>A5</code>.
  Start situation:<img src="https://i.stack.imgur.com/5VvqC.png" alt=""></li>
  </ul>
  
  <p><strong>Goal:</strong><br>
  Running the script should add <code>A5</code> number of weeks starting from the date <code>A6</code> to the "upcoming events" section (<code>row 22 and following</code>) with the correct date.
  This is how it would look like after the script ran successfully:<img src="https://i.stack.imgur.com/XV65x.png" alt=""></p>
  
  <p><strong>What works so far:</strong><br>
  The script is able to add the recurring events for one week in the right order to the upcoming events section. It works as well if the starting date is in the middle of the week. (Not shown here as it is probably not relevant.)</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>function recurringWeeks() {
    var ss = SpreadsheetApp.getActiveSheet(); // selects the active spreadsheet
    var repeatingWeeks = ss.getRange(5,1).getValue(); // gets how many weeks it should repeat
    var regWeek = ss.getRange(9, 2, 3, 7).getValues(); // gets the regular week data
    var regWeekRepeated = ss.getRange(9, 2, repeatingWeeks*3, 7); // create an array to store the events for all weeks
  
    // fill regWeekRepeated with regWeek
    for (var j = 0; j &lt; repeatingWeeks; j++){
      for (var i = 0; i &lt; 3; i++){
        regWeekRepeated[i+j*3] = regWeek[i];
    }
    }  
  
    // Repeat week for "A5" times and add to start/end date a week
    for (var j = 0; j &lt; repeatingWeeks; j++){
      for (var i = 0; i &lt; 3; i++){
      regWeekRepeated[i+j*3][0] = new Date(regWeek[i][0].getTime() + j*7*3600000*24); // &lt;-This line leads to an error message.
      }
    }
  
    ss.getRange(ss.getLastRow()+1,2,repeatingWeeks*3,7).setValues(regWeekRepeated); // copies weekly events after the last row
  }
  </code></pre>
  
  <p><strong>Edit of [i+j*6] to [i+j*3] in</strong> <code>Repeat week for "A5" times and add to start/end date a week</code></p>
  
  <p><strong>Approach:</strong><br>
  As I have solved how to add one week of recurring events with the correct date and right order, I use this as my "point of attack". I'm pretty sure that a <code>for-loop</code> does the job and this is currently my preferred tool.</p>
  
  <ul>
  <li><p>create an array (<code>regWeek</code>) filled with the recurring event for one 
  week with the right order and dates. <strong>DONE</strong></p></li>
  <li><p>create an array (<code>regWeekRepeated</code>) and fill it with <code>A5</code> number of
  regular weeks (<code>regWeek</code>) starting from the date <code>A6</code>. <strong>ERROR 1</strong>:
  <em>Object does not allow properties to be added or changed.</em></p></li>
  <li>make changes to the filled array <code>regWeekRepeated</code>. <strong>ERROR 2</strong>: <em>TypeError: Cannot set property "0.0" of undefined to "(class)@3d8e4650"</em></li>
  <li>Copy the values into the "upcoming events" section. <strong>DONE</strong></li>
  </ul>
  
  <p><strong>Best hit I've found in the search results:</strong>
  <a href="https://stackoverflow.com/questions/13575077/creating-2-dimension-arrays">creating 2 dimension arrays</a>
  However, this uses .push and as far as I understand this means an element (can be a row) is placed at the end of an array. I've tried to use push as well but have not been successful yet.</p>
  
  <p><strong>Questions</strong>:  </p>
  
  <ul>
  <li>ERROR 1: Why is it not possible to assign the value of an element from array <code>regWeek</code> to array <code>regWeekRepeated</code>? <strong>Solved</strong></li>
  <li><p>ERROR 2: Is this property issue related to ERROR 1 or something different? I've tried to solve both errors individually. <strong>Solved</strong></p></li>
  <li><p>Which approach makes more sense (logically or performance wise) in this context: push individual rows at the end of an existing array or use the whole week as array building blocks?</p></li>
  </ul>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1NP7pPfcXiQGOjNFjVV0DeHZY0_fH1kExO_-FVQuAHsg/edit?usp=sharing" rel="nofollow noreferrer">A demo version of the spreadsheet</a></p>
  
  <p><strong>Update V01</strong>:<br>
  Changes: regWeekRepeated is now an array.<br>
  I've changed the <code>for loop</code> due to the feedback I've received.</p>
  
  <pre><code>  // fill regWeekRepeated with regWeek
    var regWeekRepeated = [];
    for (var j = 0; j &lt; repeatingWeeks; j++){
      for (var i = 0; i &lt; 3; i++){
        regWeekRepeated.push(regWeek[i]);
      }
    }
    Logger.log(regWeekRepeated)
  </code></pre>
  
  <p><strong>Update V02</strong>:  </p>
  
  <pre><code>  // Repeat week for "A5" times and add to start/end date a week
    for (var j = 0; j &lt; repeatingWeeks; j++){
      for (var i = 0; i &lt; 3; i++){
      regWeekRepeated[i+j*3][0] = new Date(regWeek[i][0].getTime() + j*7*3600000*24); //adds a week to the dates for each cycle
      //Logger.log(regWeekRepeated[i]); // log is as expected and desired
      }
      Logger.log(regWeekRepeated); // second part of log not as expected.
    }
    //Logger.log(regWeekRepeated);
    ss.getRange(ss.getLastRow()+1,2,repeatingWeeks*3,7).setValues(regWeekRepeated); // copies weekly events after the last row
  </code></pre>
  
  <p>Here the log output placed in the "outer" <code>for loop</code>.
  <a href="https://i.stack.imgur.com/Uuteg.png" rel="nofollow noreferrer">1 represents the first cycle, 2 the second cycle</a>
  It looks like the second <code>for loop</code> overwrites the elements 0 to 2.</p>
  
  <p>And here the output in <a href="https://i.stack.imgur.com/ZecoT.png" rel="nofollow noreferrer">google sheets</a></p>
  
  <p><strong>Update V03</strong>:<br>
  This makes sure that the changes don't affect the copy.</p>
  
  <pre><code>  // fill regWeekRepeated with regWeek
    for (var j = 0; j &lt; repeatingWeeks; j++){
      for (var i = 0; i &lt; 3; i++){
        regWeekRepeated[i+j*3] = regWeek[i].slice(); // shallow copy of an array
    }
    }
  </code></pre>