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.
The range through which the function runs is this:
The function runs and produces the following:
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.
The function I am using is as follows: (column F has index 5 in the function)
function result(range) {
var output2 = [];
for(var i=0, iLen=range.length; i<iLen; i++) {
var s = range[i][5].split("\n");
for(var j=0, jLen=s.length; j<jLen; j++) {
var output1 = [];
for(var k=0, kLen=range[0].length; k<kLen; k++) {
if(k == 5) {
output1.push(s[j]);
} else {
output1.push(range[i][k]);
}
}
output2.push(output1);
}
}
return output2;
}
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.
If I have something like this:
I would want the function to turn it into this:
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.
Columns E and F are just an example. The script would have to run through multi-line cells ins more than two columns.
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?
Thank you very much.





