pythongoogle-forms-api

How can I use the batchupdate function of the google form api with python to modify the url and answers?


Here is an example of my form. How can I use python to modify the url and answer for the first question, as I am not familiar with using batchupdate?

I can use "get" to retrieve information from the form.

{'formId': '1q4pJMDtiLxQ2cjmLXxowqJ5VPfI68bUUo', 
'info': {'title': 'PIXEL ', 'documentTitle': 'daily'}, 
'settings': {'quizSettings': {'isQuiz': True}},
 'revisionId': '00000067', 
'responderUri': 'https://docs.google.com/forms/d/e/1FAIpQLScap6ZdpOnWIyxWqZNXjlfWW9DgPe-Wv_CUtziWw/viewform', 
'items': [{'itemId': '7c0ddb37', 'pageBreakItem': {}}, 
        {'itemId': '2870b06c', 'videoItem': {'video': {'youtubeUri': 'www.youtube.com/watch?v=Lt5HqPvM-eI', 'properties': {'alignment': 'LEFT', 'width': 320}}}},
        {'itemId': '381aedf6', 'questionGroupItem': {'questions': [{'questionId': '4d7f011e', 'required': True, 'rowQuestion': {'title': 'pick'}}], 'grid': {'columns': {'type': 'RADIO',         'options': [{'value': '1'}, {'value': '2'}, {'value': '3'}]}}}, 'title': 'pay'}, 
          {'itemId': '0f9dc00b', 'title': 'number', 'questionItem': {'question': {'questionId': '39523976', 'required': True, 
          'grading': {'correctAnswers': {'answers': [{'value':'1115'}]}}, 'textQuestion': {}}}}, 
          {'itemId': '0a12a42e', 'pageBreakItem': {}}, 
          {'itemId': '19640fea', 'videoItem': {'video': {'youtubeUri': 'www.youtube.com/watch?v=Lt5HqPvM-eI', 'properties': {'alignment': 'LEFT', 'width': 320}}}}, 
          {'itemId': '685ba545', 'questionGroupItem': {'questions': [{'questionId': '044f9f9b', 'required': True, 'rowQuestion': {'title': 'pick'}}], 'grid': {'columns': {'type': 'RADIO',           'options': [{'value': '1'}, {'value': '2'}, {'value': '3'}]}}}, 'title': 'pay'}, 
          {'itemId': '6a9d1b88', 'title': 'number', 'questionItem': {'question': {'questionId': '2199beb0', 'required': True, 
            'grading': {'correctAnswers': {'answers': [{'value': '1115'}]}}, 'textQuestion': {}}}}]}

The official documentation has too few examples for me to understand how to apply it to my form.


update = {
    "requests": [{
        "updateItem": {
            "item": {
                "title": "Homework video",
                "description": "Quizzes in Google Forms",
                "videoItem": {
                    "video": {
                        "youtubeUri": "https://www.youtube.com/watch?v=Lt5HqPvM-eI"
                    }
                }
            },"location": {
                "index": 0},
            "updateMask": "description,youtubeUri"
        }
    }]
}

question_setting = service.forms().batchUpdate(
    formId=form_id, body=update).execute()

Solution

  • From your following reply,

    I want to update the youtubeUri item and use a new URL. How can I do this? i have two question use the video,how do i update the first question URL ?

    I understood your question is as follows.

    In this case, how about the following sample script?

    Sample script:

    service =   # Please use your client
    
    formId = "###" # Please set your Google Form ID.
    after = "https://www.youtube.com/watch?v=###" # Please set YouTube URL you want to replace. In this sample, the existing URL is changed to this URL.
    
    res = service.forms().get(formId=formId).execute()
    itemIds = [[i, e["itemId"]] for i, e in enumerate(res.get("items")) if "videoItem" in e]
    topItem = itemIds[0] # From your question, `youtubeUri` of the 1st question.
    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()
    

    Note:

    Reference: