[英]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...............PenangStone......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.........PenangStone......5,Jae Road,22222............槟城
Example 2
Sheet 1: Contain all the data (Master List)
表 1:包含所有数据(主列表)
Name.....Gender姓名.....性别
Bryan.....Male布莱恩.....男
Mei.....Female梅.....女
Lily.....Female莉莉.....女
xx.....Femalexx.....女
xx.....Malexx.....男
Sheet 2: Copy data from Sheet 1 (Gender-Female Only)
工作表 2:从工作表 1 复制数据(仅限性别 - 女性)
Name.....Gender姓名.....性别
xx.....Femalexx.....女
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.