问题
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