I am attempting to grab the answer key from the short answer in Google forms.
I am attempting to grab the following answer key from a short answer question (refer to the screenshot below). Below the Short answer text, it shows you the correct answer.
However, when reading the documentation, there seems there is no way to do so. Is there an alternative way to grab the answer key from the short answer in Google Forms?
The following is the script I am using to grab the information from the quiz:
function extractFormData(fullNme) {
var form = FormApp.getActiveForm();
var items = form.getItems();
var quizAdded = new Date().toLocaleString("en-US");
var uniqueQuizID = Utilities.getUuid(), question_listID = uniqueQuizID.concat("-questions");
var constructedJSON = {};
var answer_val = false;
var errorJSON = {};
var email = form.getEditors()[0].getEmail();
var quizInfo = {
"quiz_name": form.getTitle(),
"quiz_owner": fullNme,
"quiz_description": form.getDescription(),
"quiz_owner_email": email,
"quiz_submited_date":quizAdded
};
//console.log(JSON.stringify(quizInfo));
for (var i = 0; i < items.length; i ) {
var item = items[i];
switch(item.getType()) {
case FormApp.ItemType.MULTIPLE_CHOICE:
var question = item.asMultipleChoiceItem();
var ques = question.getTitle();
var question_type = "Multiple Choice";
var optns = [];
var answr;
var answers = question.getChoices();
answer_val = false;
for (var j = 0; j < answers.length; j ) {
var clean = answers[j].getValue();
optns.push(clean);
if(answers[j].isCorrectAnswer()){
answr = answers[j].getValue();
for(var x = 0; x < optns.length; x ){
if(answr == optns[x]){
answer_val = true;
break;
}
}
}
}
var multiJSON = makeJSON(ques, question_type, optns, answr);
constructedJSON[i 1] = multiJSON;
break;
case FormApp.ItemType.CHECKBOX:
var question = item.asCheckboxItem();
var ques = question.getTitle();
var question_type = "CheckBox";
var optns = [];
var answr = [];
var answers = question.getChoices();
for (var j = 0; j < answers.length; j ) {
var clean = answers[j].getValue();
optns.push(clean);
if(answers[j].isCorrectAnswer()){
answr.push(answers[j].getValue());
}
}
var checkJSON = makeJSON(ques, question_type, optns, answr);
constructedJSON[i 1] = checkJSON;
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
var question = item.asParagraphTextItem();
var ques = question.getTitle();
var question_type = "free response";
var optns = [];
var answr;
var paraJSON = makeJSON(ques, question_type, optns, answr);
constructedJSON[i 1] = paraJSON;
break;
case FormApp.ItemType.TEXT:
var question = item.asTextItem();
var ques = question.getTitle();
var question_type = "free response";
var optns = "";
var answr = "";
var textJSON = makeJSON(ques, question_type, optns, answr);
constructedJSON[i 1] = textJSON;
break;
}
if(!answer_val){
errorJSON = {"Question":ques, "Options":optns, "Answer": answr, "Sucess": false};
//error(ques);
break;
}
}
if(answer_val){
notifyUser();
} else {
return errorJSON;
}
}
CodePudding user response:
I believe your goal is as follows.
- You want to retrieve the correct answer from each question of Google Form using Google Apps Script.
In this case, when I checked the method for directly achieving your goal using Google Form service (FormApp), unfortunately, I couldn't find it. But fortunately, I confirmed that when Google Forms API is used, your goal can be achieved. So, in this answer, I would like to propose to achieve your goal using Google Forms API.
Usage:
1. Linking Google Cloud Platform Project to Google Apps Script Project for New IDE.
In order to use Forms API, please link Google Cloud Platform Project to Google Apps Script Project for New IDE, and please enable Forms API at API console. Ref
2. Sample script.
function myFunction() {
const formId = "###"; // Please set your Google Form ID.
const url = "https://forms.googleapis.com/v1/forms/" formId "?fields=*";
const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " ScriptApp.getOAuthToken() } });
const obj = JSON.parse(res.getContentText());
const values = obj.items.map(({ title, questionItem }) => (
{ title, point: questionItem.question.grading.pointValue, answer: questionItem.question.grading.correctAnswers ? questionItem.question.grading.correctAnswers.answers.map(({ value }) => value) : [] }
));
console.log(values)
}
- In this script, please include the scope of
https://www.googleapis.com/auth/forms.body.readonly
.
3. Testing.
When this script is run to the Google Form, the following sample value is obtained.
[
{"title":"sample question 1","point":5,"answer":["sample1","sample2"]},
{"title":"sample question 2","point":3,"answer":["Option 2","Option 3"]},
{"title":"sample question 3","point":3,"answer":["Option 2"]}
]
- This is a sample value.
Note:
When you want to simply check this, you can also use "Try this method" of Forms API.
When the correct answer is not set, an error occurred. So I modified it. By this, when the correct answer is not set,
[]
is returned.
References:
CodePudding user response:
The method that you are looking for is not yet supported in Apps Script.
Therefore, you have two options in this situation:
Create a feature request on Google's Issue Tracker here and provide all the necessary details.
Make use of Forms API by using the Forms API advanced service in Apps Script and try Tanaike's proposed solution.
If you check the documentation, you can see that resources of type CorrectAnswer
have a field available:
{
"value": string
}
A single correct answer for a question. For multiple-valued (CHECKBOX) questions, several CorrectAnswers may be needed to represent a single correct response option.