फ़ॉर्म या क्विज़ को अपडेट करना

किसी फ़ॉर्म में कॉन्टेंट जोड़ने या सेटिंग, मेटाडेटा या कॉन्टेंट अपडेट करने के लिए, batchUpdate() तरीके का इस्तेमाल करें. यह तरीका, बदलावों को एक बैच में ग्रुप करता है, ताकि अगर कोई अनुरोध पूरा न हो, तो अन्य (संभावित तौर पर निर्भर) बदलाव न लिखे जाएं.

batchUpdate() तरीका, रिस्पॉन्स का मुख्य हिस्सा दिखाता है. इसमें हर अनुरोध के लिए एक रिस्पॉन्स होता है. हर रिस्पॉन्स, उससे जुड़े अनुरोध के इंडेक्स पर ही दिखता है. जिन अनुरोधों के लिए कोई रिस्पॉन्स लागू नहीं होता उनके इंडेक्स पर कोई रिस्पॉन्स नहीं दिखता.

शुरू करने से पहले

इस पेज पर दिए गए टास्क पूरे करने से पहले, ये टास्क पूरे करें:

  • अर्ली अडॉप्टर प्रोग्राम के निर्देशों के मुताबिक, अनुमति/पुष्टि और क्रेडेंशियल सेटअप पूरा करें.

मेटाडेटा, सेटिंग या आइटम अपडेट करना

यहां दिए गए उदाहरण में, किसी फ़ॉर्म का मेटाडेटा अपडेट करने का तरीका बताया गया है. हालांकि, कॉन्टेंट और सेटिंग के लिए भी यही स्ट्रक्चर इस्तेमाल किया जाता है. इनमें updateFormInfo के बजाय, updateItem या updateSettings अनुरोधों का इस्तेमाल किया जाता है. हर अनुरोध के लिए, आपको बदले जाने वाले फ़ील्ड का नाम और अपडेट की गई वैल्यू देनी होगी. साथ ही, updateMask वैल्यू भी देनी होगी, ताकि बदलाव सिर्फ़ आपके बताए गए फ़ील्ड में ही हों.

REST

फ़ॉर्म का ब्यौरा अपडेट करने के लिए, फ़ॉर्म आईडी और अपडेट की गई जानकारी की वैल्यू के साथ batchUpdate() तरीके को कॉल करें.

अनुरोध के मुख्य हिस्से का उदाहरण

    "requests": [{
        "updateFormInfo": {
            "info": {
                "description": "Please complete this quiz based on this week's readings for class."
            },
            "updateMask": "description"
        }
    }]

Python

forms/snippets/update_form.py
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

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)

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

form = {
    "info": {
        "title": "Update metadata example for Forms API!",
    }
}

# Creates the initial Form
createResult = form_service.forms().create(body=form).execute()

# Request body to add description to a Form
update = {
    "requests": [
        {
            "updateFormInfo": {
                "info": {
                    "description": (
                        "Please complete this quiz based on this week's"
                        " readings for class."
                    )
                },
                "updateMask": "description",
            }
        }
    ]
}

# Update the form with a description
question_setting = (
    form_service.forms()
    .batchUpdate(formId=createResult["formId"], body=update)
    .execute()
)

# Print the result to see it now has a description
getresult = form_service.forms().get(formId=createResult["formId"]).execute()
print(getresult)

Node.js

forms/snippets/update_form.js
import path from 'node:path';
import {authenticate} from '@google-cloud/local-auth';
import {forms} from '@googleapis/forms';

/**
 * Creates a new form and then updates it to add a description.
 */
async function updateForm() {
  // Authenticate with Google and get an authorized client.
  const authClient = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/drive',
  });

  // Create a new Forms API client.
  const formsClient = forms({
    version: 'v1',
    auth: authClient,
  });

  // The initial form to be created.
  const newForm = {
    info: {
      title: 'Creating a new form for batchUpdate in Node',
    },
  };

  // Create the new form.
  const createResponse = await formsClient.forms.create({
    requestBody: newForm,
  });

  if (!createResponse.data.formId) throw new Error('Form ID not returned.');

  console.log(`New formId was: ${createResponse.data.formId}`);

  // Request body to add a description to the form.
  const update = {
    requests: [
      {
        updateFormInfo: {
          info: {
            description:
              "Please complete this quiz based on this week's readings for class.",
          },
          // The updateMask specifies which fields to update.
          updateMask: 'description',
        },
      },
    ],
  };

  // Send the batch update request to update the form.
  const result = await formsClient.forms.batchUpdate({
    formId: createResponse.data.formId,
    requestBody: update,
  });

  console.log(result.data);
  return result.data;
}

कोई आइटम जोड़ें

यहां दिए गए उदाहरण में, किसी फ़ॉर्म में नया कॉन्टेंट जोड़ने का तरीका बताया गया है. नया कॉन्टेंट जोड़ते समय, आपको इंडेक्स के साथ कोई जगह बतानी होगी, जहां नया कॉन्टेंट जोड़ा जाना चाहिए. उदाहरण के लिए, इंडेक्स 0 वाली जगह पर कॉन्टेंट जोड़ने पर, वह फ़ॉर्म की शुरुआत में दिखेगा.

REST

फ़ॉर्म में कोई आइटम जोड़ने के लिए, फ़ॉर्म आईडी और आइटम की जानकारी और चुनी गई जगह के साथ batchUpdate() तरीके को कॉल करें.

अनुरोध के मुख्य हिस्से का उदाहरण

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

Python

forms/snippets/add_item.py
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools

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)

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

form = {
    "info": {
        "title": "Update item example for Forms API",
    }
}

# Creates the initial Form
createResult = form_service.forms().create(body=form).execute()

# Request body to add a video item to a Form
update = {
    "requests": [
        {
            "createItem": {
                "item": {
                    "title": "Homework video",
                    "description": "Quizzes in Google Forms",
                    "videoItem": {
                        "video": {
                            "youtubeUri": (
                                "https://www.youtube.com/watch?v=Lt5HqPvM-eI"
                            )
                        }
                    },
                },
                "location": {"index": 0},
            }
        }
    ]
}

# Add the video to the form
question_setting = (
    form_service.forms()
    .batchUpdate(formId=createResult["formId"], body=update)
    .execute()
)

# Print the result to see it now has a video
result = form_service.forms().get(formId=createResult["formId"]).execute()
print(result)

Node.js

forms/snippets/add_item.js
import path from 'node:path';
import {authenticate} from '@google-cloud/local-auth';
import {forms} from '@googleapis/forms';

/**
 * Creates a new form and adds a video item to it.
 */
async function addItem() {
  // Authenticate with Google and get an authorized client.
  const authClient = await authenticate({
    keyfilePath: path.join(__dirname, 'credentials.json'),
    scopes: 'https://www.googleapis.com/auth/drive',
  });

  // Create a new Forms API client.
  const formsClient = forms({
    version: 'v1',
    auth: authClient,
  });

  // The initial form to be created.
  const newForm = {
    info: {
      title: 'Creating a new form for batchUpdate in Node',
    },
  };

  // Create the new form.
  const createResponse = await formsClient.forms.create({
    requestBody: newForm,
  });

  if (!createResponse.data.formId) {
    throw new Error('Form ID not returned.');
  }

  console.log(`New formId was: ${createResponse.data.formId}`);

  // Request body to add a video item to the form.
  const update = {
    requests: [
      {
        createItem: {
          item: {
            title: 'Homework video',
            description: 'Quizzes in Google Forms',
            videoItem: {
              video: {
                youtubeUri: 'https://www.youtube.com/watch?v=Lt5HqPvM-eI',
              },
            },
          },
          // The location to insert the new item.
          location: {
            index: 0,
          },
        },
      },
    ],
  };

  // Send the batch update request to add the item to the form.
  const updateResponse = await formsClient.forms.batchUpdate({
    formId: createResponse.data.formId,
    requestBody: update,
  });

  console.log(updateResponse.data);
  return updateResponse.data;
}

अनुरोध का क्रम

batchUpdate() तरीका, सब-अनुरोधों की एक कलेक्शन स्वीकार करता है. जैसे, createItem और updateItem. सब-अनुरोधों की पुष्टि, दिए गए क्रम में एक-एक करके की जाती है.

उदाहरण: batchUpdate अनुरोध में, requests कलेक्शन में दो createItem सब-अनुरोध हैं. सब-अनुरोध A में location.index 0 है और सब-अनुरोध B में location.index 1 है. अगर requests कलेक्शन [A, B] है, तो batchUpdate पूरा हो जाएगा. अगर कलेक्शन [B, A] है, तो batchUpdate पूरा नहीं होगा. ऐसा इसलिए, क्योंकि location.index 1 तब तक मान्य नहीं है, जब तक फ़ॉर्म में इंडेक्स 0 पर कोई आइटम न हो.