Advertisement
Guest User

old code

a guest
Dec 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createLogSpreadsheet() {
  2.  
  3.   var shLogs = newSheet('Logs');
  4.  
  5.   // Make the sheet 1 by 1 so because resizing after inserting info is broken
  6.   //if (shLogs.getMaxColumns() !== 1) {shLogs.deleteColumns(1,shLogs.getMaxColumns()-1);}
  7.   //if (shLogs.getMaxRows() !== 1) {shLogs.deleteRows(1,shLogs.getMaxRows()-1);}
  8.  
  9.   // Find out what pages we need to import
  10.   var jsonResultPages = ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results.json', '/page/total_pages', 'noHeaders')[0][0];
  11.   var jsonMatchPages = ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches.json', '/page/total_pages', 'noHeaders')[0][0];
  12.  
  13.   var clan1Id = [];
  14.   var clan1Name= [];
  15.   var clan2Id =[];
  16.   var clan2Name = [];
  17.   var skillLevel = [];
  18.   var week = [];
  19.   var map = [];
  20.  
  21.   // Import JSON for each page
  22.   for (var i = 1; i<=jsonResultPages; ++i) {
  23.     clan1Id = clan1Id.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/clan1/id', 'noHeaders'));
  24.     clan1Name = clan1Name.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/clan1/name', 'noHeaders'));
  25.     clan2Id = clan2Id.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/clan2/id', 'noHeaders'));
  26.     clan2Name = clan2Name.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/clan2/name', 'noHeaders'));
  27.     skillLevel = skillLevel.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/division/tier', 'noHeaders'));
  28.     week = week.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/week', 'noHeaders'));
  29.     map = map.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/results/' + i + '.json', '/results/maps', 'noHeaders'));
  30.   }
  31.  
  32.   for (var i = 1; i<=jsonMatchPages; ++i) {
  33.     clan1Id = clan1Id.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/clan1/id', 'noHeaders'));
  34.     clan1Name = clan1Name.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/clan1/name', 'noHeaders'));
  35.     clan2Id = clan2Id.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/clan2/id', 'noHeaders'));
  36.     clan2Name = clan2Name.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/clan2/name', 'noHeaders'));
  37.     skillLevel = skillLevel.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/division/tier', 'noHeaders'));
  38.     week = week.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/week', 'noHeaders'));
  39.     map = map.concat(ImportJSON('http://api.etf2l.org/competition/' + competitionId + '/matches/' + i + '.json', '/matches/maps', 'noHeaders'));
  40.   }
  41.  
  42.   // Import JSON for each page
  43.   for (var i = 1; i <=jsonResultPages; ++i) {
  44.     clan1Id = clan1Id.concat(JSON.parse(UrlFetchApp.fetch('http://api.etf2l.org/competition/' + competitionId + 'matches/' + i + 'json')))
  45.   }
  46.  
  47.   var numMatches = clan1Id.length;
  48.   var matches = [];
  49.  
  50.   // Create an object for each match from the JSON we just imported, and push it to an array
  51.   for (var i = 0; i < numMatches; ++i) {
  52.     var match = {};
  53.       match["homeID"] = clan1Id[i];
  54.       match["homeName"] = clan1Name[i];
  55.       match["awayID"] = clan2Id[i];
  56.       match["awayName"] = clan2Name[i];
  57.       match["skillLevel"] = skillLevel[i];
  58.       match["week"] = week[i];
  59.       match["map"] = map[i];
  60.     matches.push(match);
  61.   }
  62.  
  63.   var weekNumber = Math.max.apply(null, week);
  64.  
  65.   // Filter out the matches that aren't in the Tier we want
  66.   var tierMatches = matches.filter(function(match) {return match.skillLevel == tier;});
  67.   var weekMatches = [];
  68.  
  69.   // Put the matches into new arrays, organised by week number, then match number
  70.   // weekMatches[week][match].attribute
  71.   for (var w = 0; w < weekNumber; w++) {
  72.     weekMatches[w] = tierMatches.filter(function(match) {return match.week == w + 1;});
  73.   }
  74.  
  75.   // Create Table Headings
  76.   shLogs.getRange(1,2).setValue('Home Link');
  77.   shLogs.getRange(1,3).setValue('Home Name');
  78.   shLogs.getRange(1,4).setValue('Away Link');
  79.   shLogs.getRange(1,5).setValue('Away Name');
  80.   shLogs.getRange(1,6).setValue('Map(s)');
  81.   shLogs.getRange(1,7).setValue('Logs');
  82.   shLogs.getRange(1,8).setValue('Logs');
  83.   shLogs.getRange(1,9).setValue('Logs');
  84.  
  85.   // For each week, create a new week heading, then insert matches
  86.   var row = 1;
  87.   for (var w = 0; w < weekNumber; w++) {
  88.  
  89.     shLogs.getRange(row,1).setValue('Week ' + (+w + +1));
  90.     row += 1;
  91.    
  92.     for (i = 0; i < weekMatches[w].length; i++, row++) {
  93.       shLogs.getRange(row,2).setValue("http://etf2l.org/teams/" + weekMatches[w][i].homeID);
  94.       shLogs.getRange(row,3).setValue(weekMatches[w][i].homeName);
  95.       shLogs.getRange(row,4).setValue("http://etf2l.org/teams/" + weekMatches[w][i].awayID);
  96.       shLogs.getRange(row,5).setValue(weekMatches[w][i].awayName);
  97.       shLogs.getRange(row,6).setValue(weekMatches[w][i].map);
  98.     }
  99.   }
  100.  
  101.   // Resize columns to look clean
  102.   shLogs.setColumnWidth(1,100);
  103.   shLogs.setColumnWidth(2,100);
  104.   shLogs.setColumnWidth(3,200);
  105.   shLogs.setColumnWidth(4,100);
  106.   shLogs.setColumnWidth(5,200);
  107.   shLogs.setColumnWidth(6,300);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement