title: Google Spreadsheet Script - How to Transpose / Rotate Multi-dimensional Array?
body: <p>I'm trying to create a function that takes in any array and transpose it so that rows turns to  columns and columns to rows. </p>
  
  <p>Not sure what I've done wrong or missing but keep getting this message once the array is pass through....</p>
  
  <p>TypeError: Cannot set property "0.0" of undefined to "xxxxxx".</p>
  
  <p>the error is on line</p>
  
  <p>result[row][col] = array[col][row]; // Rotate</p>
  
  <p>Any pointer would be much appreciated.</p>
  
  <pre><code>function transposeArray(array){
          var result = [];
          for(var row = 0; row &lt; array.length; row++){ // Loop over rows
            for(var col = 0; col &lt; array[row].length; col++){ // Loop over columns
              result[row][col] = array[col][row]; // Rotate
            }
          }
          return result;
      }
  </code></pre>