title: Google Apps Script Pushing to Array Issue
body: <p>I use Google Sheets to help track pay to our contractors. Sometimes the work order has two or more contractors and rather than using a new row for the same information just a different contractor and pay amount, we separate the contractor's names as well as their pay with a <code>/</code>. So for instance, in the contractor column we have:</p>
  
  <pre><code>John Doe/Frank
  </code></pre>
  
  <p>And in the pay column we have:</p>
  
  <pre><code>468/65
  </code></pre>
  
  <p>The pay is respective, so John is owed $468 and Frank $65. </p>
  
  <p>What I'm trying to do is setup a separate sheet for each of the contractor's so that they can see in almost real time the status of their own order's payments without jeopardizing other contractor's information. I'm using the Google Apps Scripts to transfer information and for the most part it is working correctly. The problem I'm having is when the script reaches a row where there are two contractors for an order. Strangely, John's name and pay are written to the correct corresponding array (johnDest), but then Frank's name and pay overwrites the John's previous entry for this row. I setup the function for writing to the arrays to be dependent on the individual name of the contractor I'm passing in, not the full value of the contractor's name cell.
  I start with arrays for each of the contractors the spreadsheet id's for copying:</p>
  
  <pre><code>var johnDest = [];
  var john = "this-is-the-link-to-johns-sheet";
  var frankDest = [];
  var frank = "this-is-the-link-to-franks-sheet";
  </code></pre>
  
  <p>Then I enter a loop to add the row values to these arrays so that in the end I can write the array to their respective spreadsheets:</p>
  
  <pre><code>function exportData() {
  
      var columnM = thisWorksheet.getRange(2, 1, thisWorksheet.getLastRow(), thisWorksheet.getLastColumn());
      var mValues = columnM.getValues();
  
      for(var i = 0; i &lt; mValues.length; i++){
        var mName = mValues[i][0];
        if(mName.indexOf('/') &gt; -1){ //If contractor name column contains a '/' split it.
          var names = mName.split('/');
          var pays = mValues[i][1];
          pays = pays.split('/');
          for(var g = 0; g &lt; names.length; g++){ //For each name in split array, get name and corresponding pay to add to array.
             var cName = names[g];
             mValues[i][0] = names[g];
             mValues[i][1] = pays[g];
             Logger.log(cName); //To log the contractor's name that I am currently working with in the loop.
             switchcontractor(cName, mValues[i]);
          }
        }else{
          switchcontractor(mName, mValues[i]);
        }
      }
      copyData(john, johnDest); //Once loop is through and arrays are completed, copy data to respective sheets.
      copyData(frank, frankDest);
  }
  </code></pre>
  
  <p>Here is the <code>switchcontractor</code> function:</p>
  
  <pre><code>function switchcontractor(cName, contValues){
    Logger.log(johnDest.length + ' ' + cName); //Log the length of johnDest and the current contractor in the loop.
    if(cName == 'John'){
      johnDest.push(contValues);
    }else if(cName == 'Frank'){
      frankDest.push(contValues);
      Logger.log(johnDest.length + ' ' + cName + ' ' + contValues);
    }
  }
  </code></pre>
  
  <p>If I run the script as is, the Logger shows this information:</p>
  
  <pre><code>[14-11-13 16:16:01:843 MST] John  //Current contractor I'm working with
  [14-11-13 16:16:01:843 MST] 23 John  //Current length of johnDest before row information is pushed to it and contractor's name
  [14-11-13 16:16:01:844 MST] 24 John John,468  //Updated length of johnDest, current contractor, and row information
  [14-11-13 16:16:01:844 MST] Frank  //Current contractor
  [14-11-13 16:16:01:844 MST] 24 Frank  //Current length of johnDest and contractor's name
  [14-11-13 16:16:01:845 MST] 24 Frank Frank,65  //Length stays the same for johnDest, current contractor and row information 
  </code></pre>
  
  <p>To verify that the script is actually in the switchcase for Frank, I commented out the Logger line just after the <code>frankDest.push(contValues);</code> line above and I'm given this:</p>
  
  <pre><code>[14-11-13 16:22:52:684 MST] John
  [14-11-13 16:22:52:684 MST] 23 John
  [14-11-13 16:22:52:684 MST] 24 John John,468
  [14-11-13 16:22:52:685 MST] Frank
  [14-11-13 16:22:52:685 MST] 24 Frank
  //The Logger line that was commented out in the switchcase for Frank doesn't show, so obviously I'm in that case, right?
  </code></pre>
  
  <p>However, when Frank's name comes through the loop, it is written to the johnDest array as well as Frank's own array.
  The end result in John's sheet is this:</p>
  
  <pre><code>John     |   55   //This is an example of a previous row
  Frank    |   65   //This is the row in question
  John     |   125  //This is an example of a following row
  </code></pre>
  
  <p>This is what is on Frank's sheet:</p>
  
  <pre><code>Frank    |    25  //This is an example of a previous row
  Frank    |    65  //This is the row in question
  Frank    |    15  //This is an example of a following row
  </code></pre>
  
  <p>I'm very confused as to why Frank's information is written to John's array AND his own for the row. Any help is greatly appreciated. Thank you!</p>