How to Split Multi-line Cells Into Different Rows in Google Sheet

放肆的年华 提交于 2021-02-08 10:13:12

问题


I have a Google sheet that is very messy. In some cells, there are multiple email addresses, multiple names, multiple phone numbers, etc. Raw Data

I want to split the cells with multiple email addresses into different rows with the exact same data for the other columns. Something like this: Cleaned sheet

I used a script that I found another answer here on StackOverflow, however, the script only works if there's a return or space between the email address. The issue is that the sheet is very inconsistent, so I have emails that are separated by ";" and some are separated by a return or space.

Do you know how I can easily and quickly clean this data and split these emails into different rows? Any help will be appreciated.

Here's the script that I used:

// Main of this script.
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;
}

// For testing this script.
function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  var data = ss.getDataRange().getValues();
  var r = result(data);
  ss.getRange(ss.getLastRow() + 1, 1, r.length, r[0].length).setValues(r);
} ```



回答1:


Use SPLIT() with corresponding delimiters in an array and apply a TRANSPOSE() so instead of consequent cells horizontally, it will return emails in rows.

=TRANSPOSE(SPLIT(A1, {"|" ; char(10) ; ";" ; ","}))

Add any email-separating character you want inside the curly braces above. char(10) is the line break character.

If you want to apply the formula to a whole column or an array of cells, put the formula inside an ARRAYFORMULA() and change the cell address to an array:

=ARRAYFORMULA(TRANSPOSE(SPLIT(A1:A, {"|" ; char(10) ; ";" ; ","})))

Toggle with TRANSPOSE() and see which one returns your desired results.




回答2:


In the code you provided you are doing this var cell = e.toString().split("\n"); if you want to split by "\n" and ";" you can chain split and joins.

This is some code that illustrates what I mean:

var x = "a\nb\nc";
var y = "a;b;c";
var z = x+";"+y;

console.log(x,x.split('\n').join(';').split(';'));
console.log(y,y.split('\n').join(';').split(';'));
console.log(z,z.split('\n').join(';').split(';'));


来源:https://stackoverflow.com/questions/58878449/how-to-split-multi-line-cells-into-different-rows-in-google-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!