Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Forms Script ItemResponse getScore() returns null

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:

  • How to check if a response is correct?
  • Google forms get score of respondent's answer
like image 691
igorpavlov Avatar asked Jan 10 '18 16:01

igorpavlov


People also ask

How do I get a response to a form item?

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.

What happens if the itemresponse contains no grades for the item?

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:

How to identify the response type of a Google form?

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.

Is form getitembyid(ID) a path to item responses?

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 ();.


1 Answers

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!
  }
}
like image 65
igorpavlov Avatar answered Nov 15 '22 05:11

igorpavlov