Not sure if it is a bug or not, but the getScore()
method (https://developers.google.com/apps-script/reference/forms/item-response#getscore) always returns null
for me, while it is said it should return a double
.
The form is a Quiz, does accept answers, the score is set to 2
to all questions, the score is being registered in the responses in the Google Form.
// Code.gs
function onOpen (e) {
setOnSubmitTrigger();
}
function setOnSubmitTrigger () {
var form = FormApp.getActiveForm();
ScriptApp.newTrigger(respondToFormSubmit)
.forForm(form)
.onFormSubmit()
.create();
}
function respondToFormSubmit (e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var response = allResponses[allResponses.length - 1];
var itemResponses = response.getItemResponses();
console.log(form.isQuiz()); // yields true
for (var i in itemResponses) {
var itemResponse = itemResponses[i];
Logger.log(itemResponse.getScore()); // (!) always yields null
}
}
The topic is mentioned in the following questions, but they are neither focused specifically on the issue, nor provide a solution:
A response to one question item within a form. Item responses can be accessed from FormResponse and created from any Item that asks the respondent to answer a question. // Open a form by ID and log the responses to each question.
If this method is called multiple times for the same item, only the last grade is retained. If the ItemResponse contains no grades, this method will remove grades for the item. Scripts that use this method require authorization with one or more of the following scopes:
Those responses are identified by the IF method if (itemResponse.getItem ().getId () == itemtosearch). NB; these scripts have been drafted on the basis of the project being bound to the relevant Google Form.
Though form.getItemById (id) returns a given item, it is a path to getting more information about item properties. It is NOT a path to item responses. In order to analyse responses, you need to start with form.getResponses (), and drill down to formResponse.getItemResponses ();.
Not sure if this is documented in Google Script Reference, but here is a solution:
// Code.gs
// ...
function respondToFormSubmit (e) {
// ...
var itemResponses =
response.getGradableItemResponses(); // NOT .getItemResponses()
console.log(form.isQuiz()); // yields true
for (var i in itemResponses) {
var itemResponse = itemResponses[i];
Logger.log(itemResponse.getScore()); // yields number!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With