简体   繁体   English

将特定数据从工作表复制到工作表

[英]Copy specific data from sheet to sheet

https://docs.google.com/spreadsheets/d/1mpALs4rdNj-TFFgCQ0CEBP453v-FfQJR_bBxOzQakWU/edit#gid=1256640730 https://docs.google.com/spreadsheets/d/1mpALs4rdNj-TFFgCQ0CEBP453v-FfQJR_bBxOzQakWU/edit#gid=1256640730

I have a "masterlist" sheet collect data from Google Forms and I need the script to copy the data from master list to the related sheet according to their "Location".我有一个“主列表”表从Google Forms收集数据,我需要脚本根据其“位置”将数据从主列表复制到相关表。

Example 1

Sheet 1: Contain all the data (Master List)表 1:包含所有数据(主列表)
Name......Address...............................Location姓名......地址............位置
Rose.......21,Radje Road,88888.........Kedah玫瑰.......21,Radje Road,88888.........吉打
Rose.......21,Radje Road,88888.........Kedah玫瑰.......21,Radje Road,88888.........吉打
Rose.......21,Radje Road,88888.........Penang玫瑰.......21,Radje Road,88888............槟城
Stone......5,Jae Road,22222...............Penang Stone......5,Jae Road,22222......槟城

Sheet 2: Copy data from Sheet 1 (Location-Kedah Only)表 2:复制表 1 中的数据(仅限位置 - 吉打)
Name......Address...............................Location姓名......地址............位置
Rose.......21,Radje Road,88888.........Kedah玫瑰.......21,Radje Road,88888.........吉打
Rose.......21,Radje Road,88888.........Kedah玫瑰.......21,Radje Road,88888.........吉打

Sheet 3: Copy data from Sheet 1 (Location-Penang Only)表 3:从表 1 复制数据(仅限地点-槟城)
Name......Address...............................Location姓名......地址............位置
Rose.......21,Radje Road,88888.........Penang玫瑰.......21,Radje Road,88888............槟城
Stone......5,Jae Road,22222.........Penang Stone......5,Jae Road,22222............槟城

Example 2

Sheet 1: Contain all the data (Master List)表 1:包含所有数据(主列表)
Name.....Gender姓名.....性别
Bryan.....Male布莱恩.....男
Mei.....Female梅.....女
Lily.....Female莉莉.....女
xx.....Female xx.....女
xx.....Male xx.....男

Sheet 2: Copy data from Sheet 1 (Gender-Female Only)工作表 2:从工作表 1 复制数据(仅限性别 - 女性)
Name.....Gender姓名.....性别
xx.....Female xx.....女
Lily.....Female莉莉.....女
Mei.....Female梅.....女

Sheet 3: Copy data from Sheet 1 (Gender-Male Only)表 3:从表 1 复制数据(仅限性别-男性)
Name.....Gender姓名.....性别
Bryan.....Male布莱恩.....男

  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Master List');
  const shsr=2;//data start row on master list
  const shhr=1;//master list header row
  const hA=sh.getRange(shhr,1,1,sh.getLastColumn()).getValues()[0];
  const vs=sh.getRange(shsr,1,sh.getLastRow()-shsr+1,sh.getLastColumn()).getValues();
  const base='Sheet';
  let shts=ss.getSheets();
  shts.forEach(function(s,i){if(s.getName()!='Master List'){ss.deleteSheet(s);}});//delete all other sheets
  const gvs=sh.getRange(shsr,7,sh.getLastRow()-shsr+1,1).getValues().map(function(r){return r[0]});
  const s=new Set(gvs);
  const g=[...s];//g has unique values now
  g.forEach(function(v,i){
    let nsh=ss.insertSheet(base + String(i+1),i+1);
    let aA=[]
    aA.push(hA);//start with header row
    vs.forEach(function(r,j){
      //compare to column G value
      if(r[6]==v) {
        aA.push(r);//add rows that match
      }
    });
    nsh.getRange(1,1,aA.length,aA[0].length).setValues(aA);
    SpreadsheetApp.flush();//not really necessary but fun to watch the progress
  });
}

Try this:尝试这个:

function copyDataUniqueToColG() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Master List');
  const shsr=2;//data start row on master list
  const shhr=1;//master list header row
  const hA=sh.getRange(shhr,1,1,sh.getLastColumn()).getValues().map(function(r){return[r[1],r[2],r[3],r[4],r[5],r[6]]})[0];
  const vs=sh.getRange(shsr,1,sh.getLastRow()-shsr+1,sh.getLastColumn()).getValues();
  const base='Sheet';
  let shts=ss.getSheets();
  //shts.forEach(function(s,i){if(s.getName()!='Master List'){ss.deleteSheet(s);}});//delete all other sheets
  const gvs=sh.getRange(shsr,7,sh.getLastRow()-shsr+1,1).getValues().map(function(r){return r[0]});
  const s=new Set(gvs);
  const g=[...s];//g has unique values now
  g.forEach(function(v,i){
    let sA=[];
    ss.getSheets().forEach(function(obj){sA.push(obj.getName())});
    if(sA.indexOf(v)==-1){ss.insertSheet(v);}
    let aA=[]
    aA.push(hA);//start with header row
    vs.forEach(function(r,j){
      //compare to column G value
      if(r[6]==v) {
        aA.push([r[1],r[2],r[3],r[4],r[5],r[6]]);//add rows that match
      }
    });
    let dsh=ss.getSheetByName(v);
    dsh.getRange(1,1,aA.length,aA[0].length).setValues(aA);
    SpreadsheetApp.flush();//not really necessary but fun to watch the progress
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从驱动器文件夹复制特定工作表并将数据粘贴到活动工作表 - Copy Specific Sheet from Drive Folder and Paste Data into Active Sheet 将数据从工作表中的特定单元格复制到另一个工作表 - copy data from specific cell in a sheet to another sheet 从 Sheet1 复制数据并粘贴到 Sheet 2 - Copy Data from Sheet1 and Paste in Sheet 2 在工作表之间传输特定的单元格数据 - Transferring specific cell data from sheet to sheet 从最后一行复制特定的列数据并粘贴到另一个工作表 - Copy specific Columns data from Last Row and paste to another sheet 将数据从一张纸复制到另一张电子表格,但不包括特定的列 - Copy data from one sheet to another spreadsheet exclude specific columns 根据特定的键值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on a specific key value 如何将特定列的数据从一张表复制到另一张表 - How to copy specific columns of data from one sheet to another 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets 根据单元格的值将一行数据从一个工作表复制到特定工作表的脚本 - script to copy a row of data from one sheet to a specific sheet based on the value of a cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM