Advertisement
RemcoE33

coinMarketCap

Jun 25th, 2021 (edited)
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Created by: RemcoE33
  3. */
  4.  
  5. function onOpen(e){
  6.   SpreadsheetApp.getUi()
  7.     .createMenu('CoinMarketCap')
  8.       .addItem('Update coins', 'coinMarketCap')
  9.       .addToUi()
  10. }
  11.  
  12. function coinMarketCap() {
  13.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  14.  
  15.   //Change sheetname
  16.   const sheet = ss.getSheetByName("RemcoE33")
  17.   const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=5000`
  18.  
  19.   //Change API key
  20.   const headers = {
  21.     "X-CMC_PRO_API_KEY": 'd7e20337-xxxx-xxxx-xxxx-7ed99dd02903'
  22.  
  23.   };
  24.  
  25.   const options = {
  26.     "method" : "get",
  27.     "headers" : headers
  28.   };
  29.  
  30.   const response = UrlFetchApp.fetch(url, options);
  31.   const resonseData = JSON.parse(response.getContentText()).data;
  32.   const newObject = resonseData.map(obj => {
  33.     const coin = obj.symbol
  34.     return {
  35.       id: obj.id,
  36.       name: obj.name,
  37.       symbol: obj.symbol,
  38.       slug: obj.slug,
  39.       cmc_rank: obj.cmc_rank,
  40.       num_market_pairs: obj.num_market_pairs,
  41.       circulating_supply: obj.circulating_supply,
  42.       total_supply: obj.total_supply,
  43.       max_supply: obj.max_supply,
  44.       last_updated: new Date(obj.last_updated),
  45.       date_added: new Date(obj.date_added),
  46.       tags: obj.tags.join(', '),
  47.       platform: obj.platformm,
  48.       USD_price: obj.quote.USD.price,
  49.       USD_volume_24h: obj.quote.USD.volume_24h,
  50.       USD_percent_change_1h: obj.quote.USD.percent_change_1h,
  51.       USD_percent_change_24h: obj.quote.USD.percent_change_24h,
  52.       USD_percent_change_7d: obj.quote.USD.percent_change_7d,
  53.       USD_market_cap: obj.quote.USD.market_cap,
  54.       USD_last_updated: new Date(obj.quote.USD.market_cap)
  55.     }
  56.   })
  57.  
  58.   const columnNames = Object.keys(newObject[0])
  59.   const rowValues = newObject.map(row => { return Object.values(row) })
  60.  
  61.  
  62.   //Entiere sheet get's cleared with every call.
  63.   sheet.getDataRange().clearContent()
  64.   sheet.getRange(1, 1, 1, columnNames.length).setValues([columnNames]);
  65.   sheet.getRange(2, 1, rowValues.length, rowValues[0].length).setValues(rowValues);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement