Create an Identification Question WITH Answer, in Google Forms Using Google Script

Question:

Feature to be accessed via Google App Scripts

I’ve been trying so hard to understand javascript as I know only Python.

I generate javascript codes using Python, to run it on Google Scripts.

I was able to automate creation of any number of Multiple Choice Items by generating codes to be used as script, but I could not figure out how to make a script for Identification Questions (by Identification means answers to questions will be in words). I could make TextItem, but could not "put" the answer to it.

function myFunction() {
var form = FormApp.openById('xxx');
form.setTitle('title')
    .setDescription('description')
    .setIsQuiz(true)
    .setLimitOneResponsePerUser(true)
;
var item = form.addMultipleChoiceItem();
item.setTitle('This is a MultiChoice question.')
    .setChoices([
        item.createChoice('option'),
        item.createChoice('answer', true),
        item.createChoice('option2'),
    ])
    .setPoints(1)
    .setRequired(true)
;
var item = form.addTextItem();
item.setTitle('What is the capital of Japan?')
    .setPoints(1)
    .setRequired(true)
var itemresponse = item.createResponse('Tokyo')
;
}

Answers:

Assigning correct answers to text items cannot be achieved programmatically

It is a popular feature request (see https://issuetracker.google.com/117437423 and https://issuetracker.google.com/71637498), but has not been implemented so far.

Unfortunately you cannot do much about it apart from "starring" the issues to give them more visibility and / or changing your questions to multiple choice.

Answered By: ziganotschka

Pattern 1:

When you want to create the Quiz with the choice items using Google Form, I think that you can achieve this using the Google Form service (FormApp). The sample script is as follows.

Sample script:

function sample1() {
  const formTitle = "sample1"; // This is a form title.

  // This is an object for creating quizzes.
  const obj = [{ "question": "sample question 1", "answers": ["answer1", "answer2", "answer3"], "correct": ["answer1"], "point": 1 }];

  const form = FormApp.create(formTitle).setIsQuiz(true).setTitle("Sample questions");
  obj.forEach(({ question, answers, correct, point }) => {
    const choice = form.addMultipleChoiceItem();
    const choices = answers.map(e => choice.createChoice(e, correct.includes(e) ? true : false));
    choice.setTitle(question).setPoints(point).setChoices(choices);
  });
}
  • When this script is run, a new Google Form is created. And, the Google Form includes a question with the choice items and the answer.

Pattern 2:

When you want to create the Quiz with the text item using Google Form, I think that you can achieve this using Google Forms API. The flow for using the sample script is as follows.

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 sample2() {
  const formTitle = "sample2"; // This is a form title.

  const form = FormApp.create(formTitle);
  form.setIsQuiz(true).setTitle("Sample question");
  const formId = form.getId();

  // Create request body for Google Forms API.
  const url = `https://forms.googleapis.com/v1/forms/${formId}:batchUpdate`;
  const requests = { requests: [{ createItem: { item: { questionItem: { question: { textQuestion: { paragraph: false }, grading: { correctAnswers: { answers: [{ value: "Sample answer 1" }] } } } }, title: "Sample question 1" }, location: { index: 0 } } }] };

  // Request to Google Forms API.
  const params = {
    method: "post",
    contentType: "application/json",
    headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() },
    payload: JSON.stringify(requests),
  };
  const res = UrlFetchApp.fetch(url, params);
  console.log(res.getContentText())
}
  • In this script, please include the scope of https://www.googleapis.com/auth/forms.body.

  • When this script is run, a new Google Form is created. And, the Google Form includes a question with a text item and the answer text.

  • In this sample script, I proposed to use the batchUpdate method by guessing a case that you want to add the question to the existing Google Form. If you want to use the existing Google Form, please modify formId.

References:

Answered By: Tanaike