Advertisement
RemcoE33

AXIE

Sep 12th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onOpen(e){
  2.   SpreadsheetApp.getUi().createMenu('AXIE')
  3.     .addItem('Refresh Axies','axie')
  4.     .addItem('Enlarge image', 'enlargeImage')
  5.     .addToUi();
  6. }
  7.  
  8. function axie() {
  9.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  10.   const sheet = ss.getSheetByName('Axie');
  11.   const axies = sheet.getRange(4, 1, sheet.getLastRow() - 3).getValues().flat();
  12.   const requests = [];
  13.   const output = [];
  14.   const imageFormulas = [];
  15.  
  16.   axies.forEach(axie => {
  17.     const query = {
  18.       "operationName": "GetAxieDetail",
  19.       "variables": {
  20.         "axieId": axie
  21.       },
  22.       "query": "query GetAxieDetail($axieId: ID!) {  axie(axieId: $axieId) {    ...AxieDetail    __typename  }}fragment AxieDetail on Axie {  id  image  class  chain  name  genes  owner  birthDate  bodyShape  class  sireId  sireClass  matronId  matronClass  stage  title  breedCount  level  figure {    atlas    model    image    __typename  }  parts {    ...AxiePart    __typename  }  stats {    ...AxieStats    __typename  }  auction {    ...AxieAuction    __typename  }  ownerProfile {    name    __typename  }  battleInfo {    ...AxieBattleInfo    __typename  }  children {    id    name    class    image    title    stage    __typename  }  __typename}fragment AxieBattleInfo on AxieBattleInfo {  banned  banUntil  level  __typename}fragment AxiePart on AxiePart {  id  name  class  type  specialGenes  stage  abilities {    ...AxieCardAbility    __typename  }  __typename}fragment AxieCardAbility on AxieCardAbility {  id  name  attack  defense  energy  description  backgroundUrl  effectIconUrl  __typename}fragment AxieStats on AxieStats {  hp  speed  skill  morale  __typename}fragment AxieAuction on Auction {  startingPrice  endingPrice  startingTimestamp  endingTimestamp  duration  timeLeft  currentPrice  currentPriceUSD  suggestedPrice  seller  listingIndex  state  __typename}"
  23.     }
  24.  
  25.     const config = {
  26.       method: 'post',
  27.       url: 'https://graphql-gateway.axieinfinity.com/graphql',
  28.       headers: {
  29.         'Content-Type': 'application/json'
  30.       },
  31.       payload: JSON.stringify(query)
  32.     };
  33.  
  34.     requests.push(config);
  35.   });
  36.  
  37.   const responses = UrlFetchApp.fetchAll(requests);
  38.   const objects = responses.map(res => {
  39.     const data = JSON.parse(res.getContentText()).data.axie;
  40.     imageFormulas.push([`=IF($B$1 = true,IMAGE("${data.image}"),)`]);
  41.     const object = {
  42.       Name: data.name,
  43.       Stage: data.stage,
  44.       Class: data.matronClass,
  45.       Chain: data.chain,
  46.       'Breed Count': data.breedCount,
  47.       Level: data.level,
  48.       'Auction USD': data.auction?.currentPriceUSD,
  49.       Birthdate: new Date(data.birthDate * 1000).toISOString().split('T')[0],
  50.       Shape: data.bodyShape,
  51.       HP: data.stats.hp,
  52.       Speed: data.stats.speed,
  53.       Skill: data.stats.skill,
  54.       Morale: data.stats.morale,
  55.       Owner: data.ownerProfile?.name,
  56.       Banned: data.battleInfo.banned
  57.     }
  58.     return object
  59.   })
  60.  
  61.   objects.forEach((obj, index) => {
  62.     if (index == 0){
  63.       output.push(Object.keys(obj))
  64.     }
  65.     output.push(Object.values(obj))
  66.   })
  67.  
  68.   sheet.getRange(3,2,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
  69.   sheet.getRange(3,2,output.length,output[0].length).setValues(output);
  70.   sheet.getRange(3,sheet.getLastColumn()).setValue('Image');
  71.   sheet.getRange(4,sheet.getLastColumn(),imageFormulas.length,1).setFormulas(imageFormulas);
  72.  
  73. }
  74.  
  75. function enlargeImage() {
  76.   const formula = SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getFormula();
  77.   const url = /IMAGE\(\"(.*?)\"/g.exec(formula)[1];
  78.   const html = `
  79.     <!DOCTYPE html>
  80.     <html>
  81.       <head>
  82.         <style>
  83.           img {
  84.               max-width: 100%;
  85.               max-height: 100%;
  86.           }
  87.         </style>
  88.         <base target="_top">
  89.       </head>
  90.       <body>
  91.         <div class=img>
  92.           <img src="${url}">
  93.         </div>
  94.       </body>
  95.     </html>
  96.   `
  97.   SpreadsheetApp.getUi().showDialog(HtmlService.createHtmlOutput(html).setHeight(500).setWidth(800));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement