title: Split multi-line cells into different rows
body: <p>I have a script with a function which runs through a range of rows in a Google spreadsheet and splits multi-line cells in column F into multiple rows. It then proceeds to copy down the cells in other columns of the same row to the newly created rows. Following is an example of what I mean.</p>
  
  <p>The range through which the function runs is this:</p>
  
  <p><a href="https://i.stack.imgur.com/wG37V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wG37V.png" alt="enter image description here"></a></p>
  
  <p>The function runs and produces the following:</p>
  
  <p><a href="https://i.stack.imgur.com/7lwxU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7lwxU.png" alt="enter image description here"></a></p>
  
  <p>As you can see, in the first image, some cells in column F have more than one line. The function detects those, splits them into as many rows as there are line breaks and then copies the remaining cells in the other columns. For both "John Doe" and "Candice Len", column F has multi-line entries and, as such, both of these names got as many rows as addresses in column F after the function ran.</p>
  
  <p>The function I am using is as follows: (column F has index 5 in the function)</p>
  
  <pre><code>    function result(range) {
    var output2 = [];
    for(var i=0, iLen=range.length; i&lt;iLen; i++) {
      var s = range[i][5].split("\n");    
      for(var j=0, jLen=s.length; j&lt;jLen; j++) {
        var output1 = []; 
        for(var k=0, kLen=range[0].length; k&lt;kLen; k++) {
          if(k == 5) {
            output1.push(s[j]);
          } else {
            output1.push(range[i][k]);
          }
        }
        output2.push(output1);
      }    
    }
    return output2;
  }
  </code></pre>
  
  <p>This is working fine. However, I would like the function to do the same for other columns beyond column F. So, if I have other columns with multi-line entries, I would like the function to run through the rows, split the multi-line cells in as many rows as there are line breaks and copy the content of the cells in the remaining columns to the newly created rows. Below is an example where both column E and F have multi-line entries.</p>
  
  <p>If I have something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Yv0Hy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yv0Hy.png" alt="enter image description here"></a></p>
  
  <p>I would want the function to turn it into this:</p>
  
  <p><a href="https://i.stack.imgur.com/bWhil.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bWhil.png" alt="enter image description here"></a></p>
  
  <p>In this case, it's important to note that each row would have to have the same number of line breaks for each multi-line cell as the different lines would have to map against the corresponding lines in different columns. In other words, taking the last two screenshots as an example, in the "John Doe" entry, "San Francisco" would have to end up in the same row as "address1" and "Cincinnati" in the same row as "address2". The same for "Washington", "Phoenix", "address1" and "address2", for the "Candice Len" entries.</p>
  
  <p><strong>Columns E and F are just an example. The script would have to run through multi-line cells ins more than two columns.</strong></p>
  
  <p>How would I be able to change the script in order for it to be able to do the same for additional columns and not just column F?</p>
  
  <p>Thank you very much.</p>