-1

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".

Example 1

Sheet 1: Contain all the data (Master List)
Name......Address...............................Location
Rose.......21,Radje Road,88888.........Kedah
Rose.......21,Radje Road,88888.........Kedah
Rose.......21,Radje Road,88888.........Penang
Stone......5,Jae Road,22222...............Penang

Sheet 2: Copy data from Sheet 1 (Location-Kedah Only)
Name......Address...............................Location
Rose.......21,Radje Road,88888.........Kedah
Rose.......21,Radje Road,88888.........Kedah

Sheet 3: Copy data from Sheet 1 (Location-Penang Only)
Name......Address...............................Location
Rose.......21,Radje Road,88888.........Penang
Stone......5,Jae Road,22222.........Penang

Example 2

Sheet 1: Contain all the data (Master List)
Name.....Gender
Bryan.....Male
Mei.....Female
Lily.....Female
xx.....Female
xx.....Male

Sheet 2: Copy data from Sheet 1 (Gender-Female Only)
Name.....Gender
xx.....Female
Lily.....Female
Mei.....Female

Sheet 3: Copy data from Sheet 1 (Gender-Male Only)
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
  });
}
Karyee
  • 3
  • 3
  • I have to apologize for my poor English skill. Unfortunately, from `I need the script to duplicate / transfer / copy the data to another sheet by classification in column G "Location" to the related new sheet.`, I cannot understand about your goal and the current issue of your script. So can I ask you about them? For example, how about explaining your script by adding it to your question? It seems that your sample Spreadsheet has the script. – Tanaike Jul 16 '20 at 02:48
  • I'm sorry it was my poor English... What I need is actually "Copy" the data from my Master List to the new sheet with specific Location. – Karyee Jul 16 '20 at 03:55
  • I will explain by using another example, see whether you get what I means.. **Sheet 1: Contain all the data (Master List)** Bryan Male Mei Female Lily Female xx Female xx Male **Sheet 2: Copy data from Sheet 1 (Female Only)** xx Female Lily Female Mei Female **Sheet 3: Copy data from Sheet 1 (Male Only)** Bryan Male – Karyee Jul 16 '20 at 03:59
  • Thank you for replying and adding more information. About your script in your shared Spreadsheet, I thought that to explain your current issue using your current script will help users think of the solution. How about this? – Tanaike Jul 16 '20 at 04:25
  • I don't have any script for this yet, because I have no idea what to do to get this result... – Karyee Jul 16 '20 at 07:04
  • Kindly take a look at these questions here since they offer explanations on how to copy data from one sheet to another: [1](https://stackoverflow.com/questions/48691872/google-apps-script-copy-data-to-different-worksheet-and-append), [2](https://stackoverflow.com/questions/44967086/). – ale13 Jul 16 '20 at 08:54

1 Answers1

0

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
  });
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Hi Cooper, thank you so much for your reply, but it is not working for me.. And the function that I need is actually copy the data from master list to the related sheet according to their "Location". https://docs.google.com/spreadsheets/d/1mpALs4rdNj-TFFgCQ0CEBP453v-FfQJR_bBxOzQakWU/edit#gid=1256640730 You may edit the script using this shared spreadsheet. – Karyee Jul 17 '20 at 09:18
  • My "master list" is used to collect all the customer database from different place, and you can see my other sheet like "Penang","Perak","Negeri Sembilan","Melaka","Johor,"Johor Bahru","Kedah", is still empty, So I need the function to copy the customer data from master list to these sheet according to their "Location". – Karyee Jul 17 '20 at 09:23
  • I tested this and it works for me. Also I will not follow links to spreadsheets. Everything that you need in your question must be posted in the question . Don’t depend upon external links. What does not working for me mean? – Cooper Jul 17 '20 at 14:00
  • It didn't work for copy & paste the source from "Master List" to other sheet... – Karyee Jul 20 '20 at 01:26
  • Sorry I don't know what to tell you it works for me – Cooper Jul 20 '20 at 01:31
  • Is it your script only copy data in one column? Maybe there's some misunderstanding through my explanation because I am not very good in english. Actually I need to copy the (complete data) in *whole column* instead of just one column. – Karyee Jul 20 '20 at 01:31
  • Can you share me your example spreadsheet link? I'll try to figure it out.. – Karyee Jul 20 '20 at 01:32
  • How about showing me an image of the master list and a few of the pages – Cooper Jul 20 '20 at 01:45
  • And post the code you have now at the end of your question – Cooper Jul 20 '20 at 01:46
  • Oh so the names of the page come from column G – Cooper Jul 20 '20 at 05:35
  • Okay I think that is what you wish. – Cooper Jul 20 '20 at 19:24