I am currently working on a Google Sheet and I want to split a few columns that have strings into rows across the entire worksheet.
I used a script found on this platform which was helpful but the data came out with some errors. Wonder if someone could help me solve this.
I am very new to javascript.
Original Data looks like this. enter image description here
I would like it to look like this. enter image description here
However, it turns out like this, where it duplicates some of the texts. I've highlighted it in red. enter image description here
Here's the script that I used and the link to a sample worksheet.
function result(range) {
var output = [];
for (var i in range) {
var celLen = 1;
var c1 = range[i].map(function(e, i){
var cell = e.toString().split("\n"); // Modified
var len = cell.length;
if (len == 1) {
return cell[0];
} else if (len > 1) {
celLen = celLen > len ? celLen : len;
var t2 = [];
for (var k=0; k<cell.length; k++) {
t2.push(cell[k]);
}
return t2;
}
});
var c2 = c1.map(function(e, i){
var r = [];
if (!Array.isArray(e)) {
for (var k=0; k<celLen; k++) {
r.push(e);
}
} else {
for (var k in e) {
r.push(e[k]);
}
if (e.length < celLen) {
for (var m=0; m<celLen - e.length; m++) {
r.push("");
}
}
}
return r;
});
var c3 = c2[0].map(function(e, i){return c2.map(function(f, j){return c2[j][i]})});
Array.prototype.push.apply(output, c3);
}
return output;
}

