|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
|||
|
|||
|
Re: Google Sheets OPR Calculator
Quote:
Quote:
printing T (creating full team list, sorting it, getting unique values, printing) is too fast to time manually printing b and A: ~21sec to create/print both printing formula: fast One weird thing I noted is that the first 188 rows of A (first 94 matches) are printed very quickly, then there's a long pause (at least 15sec the last time I ran it) before the remaining lines are printed. I'm not sure what's behind this, but I'll look into it more. Code:
function calculateOPR2() {
var data = SpreadsheetApp.getActive().getSheetByName('data');
var misc = SpreadsheetApp.getActive().getSheetByName('misc');
var OPR = SpreadsheetApp.getActive().getSheetByName('OPR');
var numMatches = data.getDataRange().getNumRows()-1; // number of matches
var alliances = data.getRange(2, 1, numMatches, 8).getValues(); //get all data
var allTeams = new Array();
var teams = new Array();
var teamIndex = new Array();
var c = 0;
data.getRange(1, 14).setValue('done initializing');
// list of all teams
for(var x = 0; x < numMatches; x++) {
for(var y = 0; y < 6; y++) {
allTeams[c] = alliances[x][y];
c++;
}
}
data.getRange(2, 14).setValue('list of all teams');
// sorts all teams
allTeams.sort(function(a, b) {return a - b;})
data.getRange(3, 14).setValue('list of sorted teams');
// list of all unique teams;
c = 1;
teams[0] = allTeams[0];
teamIndex[teams[0]] = 0;
OPR.getRange(2, 1).setValue(teams[0]);
for(var x = 1; x < allTeams.length; x++) {
if(allTeams[x] != allTeams[x-1]) {
teams[c] = allTeams[x];
teamIndex[teams[c]] = c;
OPR.getRange(c+2, 1).setValue(teams[c]);
c++;
}
}
data.getRange(3, 14).setValue('list of unique teams');
for(var x = 0; x < numMatches; x++) {
// create and set b
misc.getRange(2*x+1, teams.length+1).setValue(alliances[x][6]);
misc.getRange(2*x+2, teams.length+1).setValue(alliances[x][7]);
// create and set A
for(var y = 0; y < teams.length; y++) {
misc.getRange(2*x+1, y+1).setValue(0);
misc.getRange(2*x+2, y+1).setValue(0);
}
misc.getRange(2*x+1, teamIndex[(alliances[x][0])]+1).setValue(1);
misc.getRange(2*x+1, teamIndex[(alliances[x][1])]+1).setValue(1);
misc.getRange(2*x+1, teamIndex[(alliances[x][2])]+1).setValue(1);
misc.getRange(2*x+2, teamIndex[(alliances[x][3])]+1).setValue(1);
misc.getRange(2*x+2, teamIndex[(alliances[x][4])]+1).setValue(1);
misc.getRange(2*x+2, teamIndex[(alliances[x][5])]+1).setValue(1);
}
var formula = createFormula(teams.length, numMatches);
OPR.getRange(2, 2).setFormula(formula);
}
Quote:
|
|
#17
|
||||
|
||||
|
Re: Google Sheets OPR Calculator
Quote:
I think you missed the boat here. Your code has three passes through the matches, and the final pass has an inner loop which loops through the teams. The pseudo-code I posted does a single pass through the 8col match data, with no inner loops in that single pass. That single pass accomplishes everything you need. Give it a try! |
|
#18
|
|||
|
|||
|
Re: Google Sheets OPR Calculator
Quote:
I also figured out what's taking so much time: it's the repeated .getRange().setValue(). There's another function, setValues() that allows me to print the entire array at once that really speeds it up. However, the array must be a consistent size, so I had to keep the loop to create T so I could initialize A. It also seems like fill() doesn't work in scripts, so I had to loop through T to set A to zero, otherwise when I printed out A it'd be filled with "NOT_FOUND". I did cut out the loop for b (not sure why I had it there in the first place). Although I did manage to implement your pseudo-code, the only method I found to make that work would be to print A directly (instead of creating the array first), which took longer than looping. It also results in the OPRs not being sorted, which is a convenience I'd sacrifice a bit of time for. Hopefully that now addresses your point? I'll post my updated code once I clean it up a bit. |
|
#19
|
||||||
|
||||||
|
Re: Google Sheets OPR Calculator
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
![]() Last edited by Ether : 10-28-2016 at 06:21 PM. |
|
#20
|
||||||
|
||||||
|
Re: Google Sheets OPR Calculator
Quote:
Array values are set to null (I think--if I set a cell value to that, it comes out as either "undefined" or "NOT_FOUND" depending on whether I set an individual cell or range) Quote:
Quote:
Quote:
Quote:
Quote:
This has been an interesting conversation though, so thank you for that. I will be updating the posted spreadsheet with the new script soon (currently cleaning it up a bit, but it should be up by tonight) for anyone who wants it. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|