Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templatize the script #2

Open
LesterCovax opened this issue Dec 26, 2017 · 1 comment
Open

Templatize the script #2

LesterCovax opened this issue Dec 26, 2017 · 1 comment

Comments

@LesterCovax
Copy link
Owner

It should be able to pull in lists of currencies and wallet addresses from the spreadsheet

@Project-42
Copy link
Collaborator

Project-42 commented Dec 27, 2017

What about this creating the variables from the column A?



  // Set Rates sheet Titles
  ssRates.getRange('A1').setValue("Currency ID");
  ssRates.getRange('B1').setValue("Currency Name");
  ssRates.getRange('C1').setValue("Symbol");
  ssRates.getRange('D1').setValue("Price GBP");
  ssRates.getRange('E1').setValue("Change 1h");
  ssRates.getRange('F1').setValue("Change 24h");
  ssRates.getRange('G1').setValue("Change 7d");
  ssRates.getRange('H1').setValue("Market Cap USD");
    
  
  //Grabbing values from CoinMarketCapAPI
  //Setup your coins in the sheet Rates putting the names to match the 'id' field from https://api.coinmarketcap.com/v1/ticker/ in Column A Starting in Cell A2
  
  
  var Coin1 = ssRates.getRange('A2').getValue();
  var Coin2 = ssRates.getRange('A3').getValue();
  var Coin3 = ssRates.getRange('A4').getValue();
  var Coin4 = ssRates.getRange('A5').getValue();
  var Coin5 = ssRates.getRange('A6').getValue();
  var Coin6 = ssRates.getRange('A7').getValue();
  var Coin7 = ssRates.getRange('A8').getValue();
  var Coin8 = ssRates.getRange('A9').getValue();
  var Coin9 = ssRates.getRange('A10').getValue();
  var Coin10 = ssRates.getRange('A11').getValue();

  // Set Coins names
  
  ssRates.getRange("B2:B11").setValues
  ([ 
  [getName(Coin1)], [getName(Coin2)], [getName(Coin3)],  [getName(Coin4)], [getName(Coin5)], [getName(Coin6)], [getName(Coin7)], [getName(Coin8)], [getName(Coin9)], [getName(Coin10)] 
  ]);
  

  
  
  // Set Coins Symbol

  ssRates.getRange("C2:C11").setValues
  ([ 
  [getSymbol(Coin1)], [getSymbol(Coin2)], [getSymbol(Coin3)],  [getSymbol(Coin4)], [getSymbol(Coin5)], [getSymbol(Coin6)], [getSymbol(Coin7)], [getSymbol(Coin8)], [getSymbol(Coin9)], [getSymbol(Coin10)] 
  ]);

  
  
  
  // Set Current Prices
  
  ssRates.getRange("D2:D11").setValues
  ([ 
  [getRate(Coin1)], [getRate(Coin2)], [getRate(Coin3)],  [getRate(Coin4)], [getRate(Coin5)], [getRate(Coin6)], [getRate(Coin7)], [getRate(Coin8)], [getRate(Coin9)], [getRate(Coin10)] 
  ]);
  
  
  
 // Set Change 1h
  
  ssRates.getRange("E2:E11").setValues
  ([ 
  [getCh1h(Coin1)], [getCh1h(Coin2)], [getCh1h(Coin3)],  [getCh1h(Coin4)], [getCh1h(Coin5)], [getCh1h(Coin6)], [getCh1h(Coin7)], [getCh1h(Coin8)], [getCh1h(Coin9)], [getCh1h(Coin10)] 
  ]);

  
  // Set Change 24h
  
  ssRates.getRange("F2:F11").setValues
  ([ 
  [getCh24h(Coin1)], [getCh24h(Coin2)], [getCh24h(Coin3)],  [getCh24h(Coin4)], [getCh24h(Coin5)], [getCh24h(Coin6)], [getCh24h(Coin7)], [getCh24h(Coin8)], [getCh24h(Coin9)], [getCh24h(Coin10)] 
  ]);
  
  
  // Set Change 7d
  
  ssRates.getRange("G2:G11").setValues
  ([ 
  [getCh7d(Coin1)], [getCh7d(Coin2)], [getCh7d(Coin3)],  [getCh7d(Coin4)], [getCh7d(Coin5)], [getCh7d(Coin6)], [getCh7d(Coin7)], [getCh7d(Coin8)], [getCh7d(Coin9)], [getCh7d(Coin10)] 
  ]);
  
  
  
  // Set Market Cap USD
  ssRates.getRange("H2:H11").setValues
  ([ 
  [getMarketCap(Coin1)], [getMarketCap(Coin2)], [getMarketCap(Coin3)],  [getMarketCap(Coin4)], [getMarketCap(Coin5)], [getMarketCap(Coin6)], [getMarketCap(Coin7)], [getMarketCap(Coin8)], [getMarketCap(Coin9)], [getMarketCap(Coin10)] 
  ]);
   

These are the Other Functions called:


function getName(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return (data[0]['name']);
}



function getSymbol(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return (data[0]['symbol']);
}






function getRate(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/?convert=GBP';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return parseFloat(data[0]['price_gbp']);
}




function getCh1h(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/?convert=GBP';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return parseFloat(data[0]['percent_change_1h']);
}


function getCh24h(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/?convert=GBP';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return parseFloat(data[0]['percent_change_24h']);
}




function getCh7d(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/?convert=GBP';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return parseFloat(data[0]['percent_change_7d']);
}



function getMarketCap(currencyId) {
  
  var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/';
  var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return (data[0]['market_cap_usd']);
}


EDIT: I just realised the UrlFetchApp.fetch has some limits, so is not good idea to have that many individual requests to avoid this error:

Service invoked too many times for one day: urlfetch

LesterCovax pushed a commit that referenced this issue Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants