简体   繁体   中英

Split Google Sheets Cells into Multiple Rows Based on a Delimiter

I'm trying to come up with a function that will split data stored in a cell into multiple rows based on a delimiter (in this case, "\n"). I found a really good solution here:

https://webapps.stackexchange.com/questions/60861/google-sheets-split-multi-line-cell-into-new-rows-duplicate-surrounding-row-e

However, this solution only allows you to split up ONE column into multiple rows; I need to split up multiple columns.

Here is an example of some data I have now:数据

Here is an example of what I would like the final product to look like:结果

I know I can get this result by using =TRANSPOSE(SPLIT(cell,CHAR(10))) and then copying down the other cells, but I have a lot of data so I'd really prefer to automate this like in the above link. Any help you can give me is appreciated. Thank you!

Restructure rows to accomodate multiple items in a single cell

function restructureRows() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet2');
  const shsr=2;//data start row
  const rg=sh.getDataRange();//this includes one header row
  const dlm='\n';//cell delimiter
  let vs=rg.getValues();//this includes header row
  let hA=vs.shift();//this put the header row in hA
  var ttl={};//converts array indices to column names
  var a=0;//added row counter
  hA.forEach(function(h,i){ttl[i]=h;});
  //vs nolonger contains header row
  for(var i=0;i<vs.length;i++) {
    var robj={max:1,maxidx:''};
    for(var j=0;j<vs[i].length;j++) {
      var temp=vs[i][j].toString();
      //if data contain line feed then that row will be expanded
      if(vs[i][j].toString().indexOf(dlm)!=-1) {
        let t=vs[i][j].toString().split(dlm);//split cell on dlm
        robj[ttl[j]]=[];//row expansion object
        t.forEach(function(e,k){
          robj[ttl[j]].push(e);//push expanded cell data into obj property arrays
        });
        if(robj[ttl[j]].length>robj.max) {
          robj.max=robj[ttl[j]].length;//keeping record of the max number of delimited terms
          robj.maxidx=j;//never actually used this yet
        }
      }else{
        robj[ttl[j]]=[];
        robj[ttl[j]].push(vs[i][j]);//if no dlm the just same the one term
      }
    }
    if(robj.max>1) {
      for(var k=0;k<vs[i].length;k++) {
        const l=robj[ttl[k]].length;
        if(l>1 && l<robj.max) {
          for(let m=l;m<robj.max;m++) {
            robj[ttl[k]].push(undefined);//This section fills in the cells that had multiple dlms with undefined so that all columns have the same amount of terms in their array
          }
        }else if(l==1) {
          for(let m=1;m<robj.max;m++) {
            robj[ttl[k]].push(vs[i][k]);//this section fills in the cells that have no dlms with the same value for all rows
          }
        }
      }
      sh.insertRows(i+shsr+1+a, robj.max-1);//insert addtional row
      var oA=[];
      for(var r=0;r<robj.max;r++) {
        oA[r]=[];
        for(var k=0;k<vs[i].length;k++) {
          oA[r].push(robj[ttl[k]][r]);//This section loads data into the ouput array in preparation for a single setvalues() to load all new rows at one time.
        }
      }
      sh.getRange(i+shsr+a,1,oA.length,oA[0].length).setValues(oA);//load data
      a+=robj.max-1;//increment added row counter
    } 
  }
}

Animation:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM