Advertisement
Guest User

STAR Calculator Script

a guest
Apr 9th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ADDON_TITLE = 'STAR Calculator';
  2.  
  3. function onOpen(e) {
  4.   FormApp.getUi()
  5.       .createAddonMenu()
  6.       .addItem('Run Election', 'runElection')
  7.       .addToUi();
  8. }
  9.  
  10. function onInstall(e) {
  11.   onOpen(e);
  12. }
  13.  
  14. function runElection() {
  15.   var form = FormApp.getActiveForm();
  16.   var responses = form.getResponses();
  17.   if(responses.length == 0)
  18.     return;
  19.  
  20.   candidate_names = [];
  21.   formItems = form.getItems();
  22.   for (var i = 0; i < formItems.length; i++)
  23.     candidate_names[i] = formItems[i].getTitle();
  24.  
  25.   num_candidates = formItems.length;
  26.  
  27.   var spreadsheet = SpreadsheetApp.create(form.getTitle() + " Results");
  28.   url = spreadsheet.getUrl();
  29.   SpreadsheetApp.openByUrl(url);
  30.  
  31.   result_sheet = spreadsheet.getActiveSheet();
  32.   result_sheet.setName("Results");
  33.  
  34.   result_range = result_sheet.getDataRange();
  35.   result_values = result_range.getValues();
  36.    
  37.   num_ballots = responses.length;
  38.  
  39.   // create empty score list and preference matrix
  40.   candidate_scores = [];
  41.   pref_matrix = [];
  42.   for(i = 0; i < num_candidates; i++)
  43.   {
  44.     candidate_scores[i] = 0;
  45.     pref_matrix[i] = [];
  46.     for(j = 0; j < num_candidates; j++)
  47.       pref_matrix[i][j] = 0;
  48.   }
  49.  
  50.   total_votes = 0;
  51.  
  52.   for(b = 0; b < num_ballots; b++)
  53.   {
  54.     var ballot = responses[b].getItemResponses();
  55.     total_votes += 1;
  56.     scores = [];
  57.     for(z = 0; z < num_candidates; z++)
  58.       scores[z] = 0;
  59.    
  60.     for(i = 0; i < ballot.length; i++)
  61.       scores[ballot[i].getItem().getIndex()] = parseInt(ballot[i].getResponse());
  62.    
  63.     for(c = 0; c < num_candidates; c++)
  64.     {
  65.       candidate_scores[c] += scores[c];
  66.       for(d = 0; d < num_candidates; d++)
  67.         if(scores[c] > scores[d])
  68.           pref_matrix[c][d] += 1;
  69.     }
  70.   }
  71.   function versus(x) { return 'vs. ' + x }
  72.   result_sheet.appendRow([ 'Election Results:' ]);
  73.   result_sheet.appendRow([ '', 'Total', 'Average Score'].concat(candidate_names.map( versus ) ) );
  74.   result_sheet.getRange(2, 2, 1, 2 + num_candidates).setHorizontalAlignment("right");
  75.  
  76.   for(c = 0; c < num_candidates; c++)
  77.   {
  78.     pref_matrix[c][c] = '';
  79.     result_sheet.appendRow([candidate_names[c], candidate_scores[c], candidate_scores[c]/total_votes].concat(pref_matrix[c]));
  80.     result_sheet.getRange(3 + c, 3).setNumberFormat("0.00");
  81.     result_sheet.getRange(3 + c, 4 + c).setBackgroundRGB(180, 180, 180);
  82.   }
  83.  
  84.   // find the top two
  85.   top_a = -1;
  86.   top_b = -1;
  87.  
  88.   score_a = -1;
  89.   score_b = -1;
  90.   top_a_options = [];
  91.   top_b_options = [];
  92.  
  93.   for(c = 0; c < num_candidates; c++)
  94.   {
  95.     if(candidate_scores[c] > score_a)
  96.     {
  97.       score_a = candidate_scores[c];
  98.       top_a_options = [c];
  99.     }
  100.     else if(candidate_scores[c] == score_a)
  101.         top_a_options.push(c);
  102.     else if(candidate_scores[c] > score_b)
  103.     {
  104.       score_b = candidate_scores[c];
  105.       top_b_options = [c];
  106.     }
  107.     else if(candidate_scores[c] == score_b)
  108.         top_b_options.push(c);
  109.   }
  110.  
  111.   top_a = top_a_options[Math.floor(Math.random() * top_a_options.length)];
  112.  
  113.   if(top_a_options.length > 1)
  114.     top_b_options = top_a_options.filter(function(ele){ return ele != top_a; });
  115.  
  116.   top_b = top_b_options[Math.floor(Math.random() * top_b_options.length)];
  117.  
  118.   result_sheet.getRange(3 + top_a, 1, 1, 2).setBackgroundRGB(128, 255, 128);
  119.   result_sheet.getRange(3 + top_b, 1, 1, 2).setBackgroundRGB(128, 255, 128);
  120.   result_sheet.getRange(3 + top_a, 4 + top_b).setBackgroundRGB(128, 255, 128);
  121.   result_sheet.getRange(3 + top_b, 4 + top_a).setBackgroundRGB(128, 255, 128);
  122.  
  123.   result_sheet.appendRow([' ']);
  124.   result_sheet.appendRow(['Top two are ' + candidate_names[top_a] + ", with a score of " + candidate_scores[top_a] + ", and " + candidate_names[top_b] + ", with a score of " + candidate_scores[top_b] + ", overall."]);
  125.   if(pref_matrix[top_a][top_b] > pref_matrix[top_b][top_a])
  126.   {
  127.     winner = top_a;
  128.     loser = top_b;
  129.   }
  130.   else if(pref_matrix[top_a][top_b] < pref_matrix[top_b][top_a])
  131.   {
  132.     winner = top_b;
  133.     loser = top_a;
  134.   }
  135.   else if(candidate_scores[top_a] > candidate_scores[top_b])
  136.   {
  137.     winner = top_a;
  138.     loser = top_b;
  139.   }
  140.   else if(candidate_scores[top_a] < candidate_scores[top_b])
  141.   {
  142.     winner = top_b;
  143.     loser = top_a;
  144.   }
  145.   else if(Math.random() < 0.5)
  146.   {
  147.     winner = top_a;
  148.     loser = top_b;
  149.   }
  150.   else
  151.   {
  152.     winner = top_b;
  153.     loser = top_a;
  154.   }
  155.   result_sheet.appendRow([candidate_names[winner] + " was preferred over " + candidate_names[loser] + ", " + pref_matrix[winner][loser] + " to " + pref_matrix[loser][winner]+ "."] );
  156.   result_sheet.appendRow([' ']);
  157.   result_sheet.appendRow(['The winner is: ' + candidate_names[winner]]);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement