簡體   English   中英

根據分隔符將 Google 表格單元格拆分為多行

[英]Split Google Sheets Cells into Multiple Rows Based on a Delimiter

我正在嘗試提出一個 function,它將基於分隔符(在本例中為“\n”)將存儲在單元格中的數據拆分為多行。 我在這里找到了一個非常好的解決方案:

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

但是,此解決方案僅允許您將一列拆分為多行; 我需要拆分多個列。

這是我現在擁有的一些數據的示例:數據

這是我希望最終產品看起來像的示例:結果

我知道我可以通過使用=TRANSPOSE(SPLIT(cell,CHAR(10)))然后復制其他單元格來得到這個結果,但是我有很多數據,所以我真的更喜歡像上面那樣自動化這個關聯。 您能給我的任何幫助都將不勝感激。 謝謝!

重組行以在單個單元格中容納多個項目

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:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM