I need a Google script (GAS) to retrieve both emails and total scores from a Google Form that was turned into a quiz.
Within GAS you can get the score for each question, but I need to get the score of all questions and then average out the final score (each quiz has 20 questions). I also need the email of each person who finished the quiz. These quizzes are done within institutions so the "Collect email addresses" is selected.
I need this and NOT the "responses spreadsheet" because I will be making hundreds of quizzes and I need to put all the grades in one spreadsheet, as a summary. I do not want to have hundreds of response spreadsheets.
What I have up to now is below, but I cannot seem to get the average score of each quiz and put it beside the email in a spreadsheet. Any help would be appreciated.
function getPoints() {
var form = FormApp.openById('ID');
var formResponses = form.getResponses();
var formItems = form.getItems();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var email = formResponse.getRespondentEmail();
/* I need to get all emails from those who responded,
not just one and put them in column A.*/
var s = SpreadsheetApp.openById("ID").getSheetByName("Sheet1");
var sr = s.getRange("A:A").setValues(email);
}
for (var j = 0; j < formItems.length; j++) {
var item = formItems[i];
if (item.getType() === item.getType().TEXT){
var points = item.asTextItem().getPoints();
var itemResponse = formResponse.getGradableResponseForItem(item);
var answer = itemResponse.getResponse();
var sc = itemResponse.getScore();
/* I need to get all the scores, not just one, and then average
them, and them put them in column B, beside the corresponding
email in column A. */
var s = SpreadsheetApp.openById("ID").getSheetByName("Sheet1");
var sr = s.getRange("B:B").setValues(sc);
}
}
}
Your script is very close.
As there isn't a method to get at once all the item scores, one alternative is to build an array of item scores, then calculate the average for the each quiz (submitted response).
Example:
The following script, intended to be used as a bounded script, include three functions:
function onOpen(e) {
var ui = FormApp.getUi();
var menu = ui.createMenu('My Menu')
.addItem('Average', 'showAverage')
.addToUi();
}
function showAverage(){
// Works for bounded scripts called from custom menus or the script editor
var form = FormApp.getActiveForm();
Logger.log(calculateAverage(form));
}
/**
* Calculate the average score for each submitted response
*
* @param {Object} form The form to be processed.
* @return {Array} 2D array. First column respondent email,
* second column response average.
*/
function calculateAverage(form){
var formResponses = form.getResponses();
// If there aren't submitted responses, exit.
if(formResponses.length == 0) return 'No responses';
// Initialize output
var output = [];
for(var i = 0; i < formResponses.length ; i++){
var itemResponses = formResponses[i].getGradableItemResponses();
// Initialize scores array. Later it will be used to calculate the score average.
var scores = [];
for(var j = 0; j < itemResponses.length; j++){
var score = itemResponses[j].getScore();
scores.push(score);
}
// Average calculation. Reference https://stackoverflow.com/a/10624256/1595451
var sum = scores.reduce(function(a, b) { return a + b; });
var avg = sum / scores.length;
// Add row
var email = formResponses[i].getRespondentEmail();
output.push([email,avg]);
}
return output;
}
Reference