How to Copy Data from One Google Sheet to Another Sheet at the next blank row

Viewed 253

I am not much familiar with Google Sheet. I am using a template from the Internet to make it usable after view the youtube videos. Here is the link to my Google Sheet. https://docs.google.com/spreadsheets/d/1KD_qFT6J6ife72OhQRzr0pHSiyyRNA3X8KCDTc2A-iE/edit?usp=sharing

What I am doing. 1- I copy data from finance and paste it into the History Tab. 2- Then I filter data according to the tab name like "BNB", "BTC", etc. and paste particular filter data in that particular sheet. It is really time taking work and makes me tired. I want to automate the entire process, just click the button "Populate Data" and data should be populated in all the sheets automatically. Is it possible to do this? If yes then please guide me on how to do it? Thanks

3 Answers

The task looks pretty easy. As a simplest naive solution you can try to extend this example:

function copy_rows_from_history() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  // get data from 'History1'
  var s = ss.getSheetByName('History1');
  var data = s.getRange(1,1,s.getLastRow(),8).getDisplayValues();

  // get all XPR rows from the data
  var XRP = data.filter(x => x[1] == 'XRPUSDT');

  // add the XPR rows to the 'XPR' sheet
  s = ss.getSheetByName('XRP');
  var lastRow = s.getRange('A1:A').getValues().filter(String).length;
  s.getRange(lastRow+1,1,XRP.length,XRP[0].length).setValues(XRP);

  // get all SHIB rows from the data
  var SHIB = data.filter(x => x[1] == 'SHIBUSDT');

  // add the SHIB rows to the 'SHIB' sheet
  s = ss.getSheetByName('SHIB');
  var lastRow = s.getRange('A1:A').getValues().filter(String).length;
  s.getRange(lastRow+1,1,SHIB.length,SHIB[0].length).setValues(SHIB);

  // etc...
}

Of course it would be more efficient to replace these 'XPR', 'SHIB', etc. with some array (dynamically created from the values of 2nd column of the 'History1' sheet) and loop trough this array with a couple universal functions. Etc. But I'm not sure if you ready to dive so deep in JS coding? Sometimes the simper the better.

Full-fledged variant of the script is here:

function copy_rows_from_history() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();

  // make the object with short and full names all the markets
  const MARKETS = {};

  [ 
    ['ADAUSDT'  , 'ADA'  ], ['BNBUSDT'  , 'BNB'  ], ['BTCUSDT'  , 'BTC'  ], ['BTTUSDT'  , 'BTT'  ],
    ['CELRUSDT' , 'CELR' ], ['DENTUSDT' , 'DENT' ], ['DOGEUSDT' , 'DOGE' ], ['EOSUSDT'  , 'EOS'  ],
    ['ETHUSDT'  , 'ETH'  ], ['FTMUSDT'  , 'FTM'  ], ['LUNAUSDT' , 'LUNA' ], ['MATICUSDT', 'MATIC'],
    ['NEOUSDT'  , 'NEO'  ], ['ONGUSDT'  , 'ONG'  ], ['QTUMUSDT' , 'QTUM' ], ['SHIBUSDT' , 'SHIB' ],
    ['STRAXUSDT', 'STRAX'], ['SUNUSDT'  , 'SUN'  ], ['SUSHIUSDT', 'SUSHI'], ['SXPUSDT'  , 'SXP'  ],
    ['TLMUSDT'  , 'TLM'  ], ['TRXUSDT'  , 'TRX'  ], ['XEMUSDT'  , 'XEM'  ], ['XRPUSDT'  , 'XRP'  ],
  ];
  .forEach(m => MARKETS[m[0]] = {name: m[0], shortname: m[1]});

  const s = ss.getSheetByName('History1');

  // get all data from 'History1'
  const data = s.getRange(1,1,s.getLastRow(),8).getDisplayValues();

  // get list of markets from column 2 (only unique values)
  const markets = s.getRange(2,2,s.getLastRow(),1).getValues()
  .flat().filter(String).filter((item,i,ar)=>ar.indexOf(item)==i);

  // fill all sheets with data
  markets.forEach(m => fill_sheet(MARKETS[m], data));
  
  // test:
  // console.log(markets[0]);               // show first market
  // fill_sheet(MARKETS[markets[0]], data); // fill first sheet

}

function fill_sheet(market, data) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();

  // get from data only the rows of given market
  var data = data.filter(x => x[1] == market.name);

  // go to sheet and add the rows
  const s = ss.getSheetByName(market.shortname);
  const lastRow = s.getRange('A1:A').getValues().filter(String).length;
  s.getRange(lastRow+1,1,data.length,data[0].length).setValues(data);

}

Before

enter image description here

After

enter image description here

you can use simple formula like:

=FILTER(History1!A:H, History1!B:B="ETHUSDT")

so after you paste your date into History1 sheet all will be sorted automatically in those other sheets of yours

enter image description here

function split_history(){
  
  var ss=SpreadsheetApp.getActive();
  var hissht=ss.getSheetByName("History1");
  var hisvals=hissht.getDataRange().getValues();
  var sheets=ss.getSheets();
  for (i=0;i<sheets.length;i++){
    if (sheets[i].getName()=="Summary" || sheets[i].getName()=="History1" || sheets[i].getName()=="Main Menu" ) {} else {
      var sn=sheets[i].getName();
      var vals=[];
      for (j=1;j<hisvals.length;j++){
        if (hisvals[j][8]==sn){
          vals.push([hisvals[j][0],hisvals[j][1],hisvals[j][2],hisvals[j][3],hisvals[j][4],hisvals[j][5],hisvals[j][6],hisvals[j][7]]) ; //modify it as per your need
        }
      }
      var dsht=ss.getSheetByName(sn);
      var dlr=dsht.getLastRow();
      dsht.getRange(dlr+1,1,vals.length,8).setValues(vals); //i have used only 4 columns A,B,C,D - you can modify it

    }
  }
}
//
Related