Google Forms Script ItemResponse getScore() returns null

我是研究僧i 提交于 2020-08-07 09:44:18

问题


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

回答1:


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!
  }
}


来源:https://stackoverflow.com/questions/48191827/google-forms-script-itemresponse-getscore-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!