Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ADDON_TITLE = 'STAR Calculator';
- function onOpen(e) {
- FormApp.getUi()
- .createAddonMenu()
- .addItem('Run Election', 'runElection')
- .addToUi();
- }
- function onInstall(e) {
- onOpen(e);
- }
- function runElection() {
- var form = FormApp.getActiveForm();
- var responses = form.getResponses();
- if(responses.length == 0)
- return;
- candidate_names = [];
- formItems = form.getItems();
- for (var i = 0; i < formItems.length; i++)
- candidate_names[i] = formItems[i].getTitle();
- num_candidates = formItems.length;
- var spreadsheet = SpreadsheetApp.create(form.getTitle() + " Results");
- url = spreadsheet.getUrl();
- SpreadsheetApp.openByUrl(url);
- result_sheet = spreadsheet.getActiveSheet();
- result_sheet.setName("Results");
- result_range = result_sheet.getDataRange();
- result_values = result_range.getValues();
- num_ballots = responses.length;
- // create empty score list and preference matrix
- candidate_scores = [];
- pref_matrix = [];
- for(i = 0; i < num_candidates; i++)
- {
- candidate_scores[i] = 0;
- pref_matrix[i] = [];
- for(j = 0; j < num_candidates; j++)
- pref_matrix[i][j] = 0;
- }
- total_votes = 0;
- for(b = 0; b < num_ballots; b++)
- {
- var ballot = responses[b].getItemResponses();
- total_votes += 1;
- scores = [];
- for(z = 0; z < num_candidates; z++)
- scores[z] = 0;
- for(i = 0; i < ballot.length; i++)
- scores[ballot[i].getItem().getIndex()] = parseInt(ballot[i].getResponse());
- for(c = 0; c < num_candidates; c++)
- {
- candidate_scores[c] += scores[c];
- for(d = 0; d < num_candidates; d++)
- if(scores[c] > scores[d])
- pref_matrix[c][d] += 1;
- }
- }
- function versus(x) { return 'vs. ' + x }
- result_sheet.appendRow([ 'Election Results:' ]);
- result_sheet.appendRow([ '', 'Total', 'Average Score'].concat(candidate_names.map( versus ) ) );
- result_sheet.getRange(2, 2, 1, 2 + num_candidates).setHorizontalAlignment("right");
- for(c = 0; c < num_candidates; c++)
- {
- pref_matrix[c][c] = '';
- result_sheet.appendRow([candidate_names[c], candidate_scores[c], candidate_scores[c]/total_votes].concat(pref_matrix[c]));
- result_sheet.getRange(3 + c, 3).setNumberFormat("0.00");
- result_sheet.getRange(3 + c, 4 + c).setBackgroundRGB(180, 180, 180);
- }
- // find the top two
- top_a = -1;
- top_b = -1;
- score_a = -1;
- score_b = -1;
- top_a_options = [];
- top_b_options = [];
- for(c = 0; c < num_candidates; c++)
- {
- if(candidate_scores[c] > score_a)
- {
- score_a = candidate_scores[c];
- top_a_options = [c];
- }
- else if(candidate_scores[c] == score_a)
- top_a_options.push(c);
- else if(candidate_scores[c] > score_b)
- {
- score_b = candidate_scores[c];
- top_b_options = [c];
- }
- else if(candidate_scores[c] == score_b)
- top_b_options.push(c);
- }
- top_a = top_a_options[Math.floor(Math.random() * top_a_options.length)];
- if(top_a_options.length > 1)
- top_b_options = top_a_options.filter(function(ele){ return ele != top_a; });
- top_b = top_b_options[Math.floor(Math.random() * top_b_options.length)];
- result_sheet.getRange(3 + top_a, 1, 1, 2).setBackgroundRGB(128, 255, 128);
- result_sheet.getRange(3 + top_b, 1, 1, 2).setBackgroundRGB(128, 255, 128);
- result_sheet.getRange(3 + top_a, 4 + top_b).setBackgroundRGB(128, 255, 128);
- result_sheet.getRange(3 + top_b, 4 + top_a).setBackgroundRGB(128, 255, 128);
- result_sheet.appendRow([' ']);
- 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."]);
- if(pref_matrix[top_a][top_b] > pref_matrix[top_b][top_a])
- {
- winner = top_a;
- loser = top_b;
- }
- else if(pref_matrix[top_a][top_b] < pref_matrix[top_b][top_a])
- {
- winner = top_b;
- loser = top_a;
- }
- else if(candidate_scores[top_a] > candidate_scores[top_b])
- {
- winner = top_a;
- loser = top_b;
- }
- else if(candidate_scores[top_a] < candidate_scores[top_b])
- {
- winner = top_b;
- loser = top_a;
- }
- else if(Math.random() < 0.5)
- {
- winner = top_a;
- loser = top_b;
- }
- else
- {
- winner = top_b;
- loser = top_a;
- }
- result_sheet.appendRow([candidate_names[winner] + " was preferred over " + candidate_names[loser] + ", " + pref_matrix[winner][loser] + " to " + pref_matrix[loser][winner]+ "."] );
- result_sheet.appendRow([' ']);
- result_sheet.appendRow(['The winner is: ' + candidate_names[winner]]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement