pythongoogle-apigoogle-formsgoogle-forms-api

How do I use the Google Forms API with Python to update the correct answers of an existing form?


I have a Google form with two short answer questions and use response validation. How can I use Python to update the answers, and can I also update the response validation?

i try set one question no response validation and i use service.forms().get check two questions, haven't find differences. I don't know what I should modify?

from __future__ import print_function

from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools
from googleapiclient.discovery import build

SCOPES = "https://www.googleapis.com/auth/forms.body"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"

store = file.Storage('token.json')
creds = None
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
    creds = tools.run_flow(flow, store)


service = discovery.build('forms', 'v1', http=creds.authorize(
    Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)

form_id = '1q4pJMDtiLxQ2cjmLXxowqJ5Vy6REe9PpLAPfI68bUUo'
form = service.forms().get(formId=form_id).execute()

Do I still need to use this method to update?

req = {
    "requests": [
        {
            "updateItem": {
                "item": {
                    "itemId": topItem[1],
                    "videoItem": {
                        "video": {
                            "youtubeUri": after,
                        }
                    },
                },
                "location": {"index": topItem[0]},
                "updateMask": "videoItem.video.youtubeUri",
            }
        }
    ]
}
service.forms().batchUpdate(formId=formId, body=req).execute()

Solution

  • From I have a Google form with two short answer questions and use response validation. How can I use Python to update the answers, and can I also update the response validation?, I believe your goal is as follows.

    Issue and solution:

    Regarding updating the answer to the question, this can be achieved in the current Google Forms API.

    Regarding managing the response validation, In the current stage, it seems that there are no methods for managing the response validation in Google Forms API. This has already been mentioned in the Google issue tracker. Ref I would like to believe that this will be added in the future update. In the current stage, if you are required to manage this, I think that Google Apps Script can be used. When Google Apps Script is used, for example, Class TextValidationBuilder can be used. In the current stage, it seems that this cannot be used with Google Forms API.

    In this answer, using Google Forms API with googleapis for python, I would like to propose a sample script for updating the answer to the question.

    Sample script:

    service =   # Please use your client
    
    formId = "###" # Please set your Google Form ID.
    updatedAnswer = "updated" # This is a sample updated answer.
    
    res = service.forms().get(formId=formId).execute()
    itemIds = [[i, e["itemId"]] for i, e in enumerate(res.get("items")) if "questionItem" in e]
    topItem = itemIds[0]
    req = {
        "requests": [
            {
                "updateItem": {
                    "item": {
                        "itemId": topItem[1],
                        "questionItem": {
                            "question": {
                                "grading": {"correctAnswers": {"answers": [{"value": updatedAnswer}]}
                                }
                            }
                        },
                    },
                    "location": {"index": topItem[0]},
                    "updateMask": "questionItem.question.grading.correctAnswers",
                }
            }
        ]
    }
    service.forms().batchUpdate(formId=formId, body=req).execute()
    

    References: