-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetWeeklyTeamScores.js
67 lines (64 loc) · 2.07 KB
/
getWeeklyTeamScores.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* refreshes the weekly scores table for each team and each week
*/
function getWeeklyScores() {
let ss = SpreadsheetApp.openById(GLOBALS.sheetId);
let weeklyScoreTableSheet =ss.getSheetByName("Weekly Score Table");
if(!weeklyScoreTableSheet){
ss.insertSheet("Weekly Score Table", 5);
let weeklyScoreTableSheet = ss.getSheetByName("Weekly Score Table");
}
weeklyScoreTableSheet.clear();
let titleRow = [];
titleRow.push("Team Name");
for(let i=1;i<=GLOBALS.totalWeeks;i++){
titleRow.push(`Week ${i} Score`);
titleRow.push(`Week ${i} Projected Score`);
}
weeklyScoreTableSheet.appendRow(titleRow);
let teamSheet = ss.getSheetByName("Teams");
let teamData =teamSheet.getDataRange().getValues()
let service = getService();
let weeklyValues = [];
if (service.hasAccess()) {
let response;
let teamKeys = "team_keys=" ;
for(td=1;td<teamData.length;td++){
weeklyValues.push([teamData[td][2]])
if (td < teamData.length-1){
teamKeys = teamKeys + teamData[td][0]+ ",";
}
else{
teamKeys = teamKeys + teamData[td][0] ;
}
}
for(let w=1; w<=GLOBALS.totalWeeks;w++){
let tempValues = [];
let url = `https://fantasysports.yahooapis.com/fantasy/v2/teams;${teamKeys}/stats;type=week;week=${w}`;
try{
response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + service.getAccessToken()
}
});
}
catch(e){
Logger.log(e)
}
let xml = response.getContentText();
let json = XML_to_JSON(xml);
//Logger.log(JSON.stringify(json))
let weeklyData = json.fantasy_content.teams.team
let iterator = 1;
for (let i=0;i<weeklyData.length;i++){
let totalScore = weeklyData[i].team_points.total.Text;
let projectedScore = weeklyData[i].team_projected_points.total.Text
weeklyValues[i].push(totalScore,projectedScore);
}
}
weeklyScoreTableSheet.getRange(2, 1, GLOBALS.teamCount, 35).setValues(weeklyValues);
}
else{
showSidebar();
}
}