একটি ফর্ম বা কুইজ আপডেট করুন

একটি ফর্মে বিষয়বস্তু যোগ করতে বা সেটিংস, মেটাডেটা বা বিষয়বস্তু আপডেট করতে, batchUpdate() পদ্ধতিটি ব্যবহার করুন, যা একটি ব্যাচে একসাথে পরিবর্তন করে যাতে একটি অনুরোধ ব্যর্থ হলে, অন্য কোনটি (সম্ভাব্যভাবে নির্ভরশীল) পরিবর্তন লেখা হয় না।

batchUpdate() পদ্ধতি একটি প্রতিক্রিয়া বডি প্রদান করে, যার মধ্যে প্রতিটি অনুরোধের জন্য একটি প্রতিক্রিয়া থাকে। প্রতিটি প্রতিক্রিয়া সংশ্লিষ্ট অনুরোধের মতো একই সূচক দখল করে; কোন প্রযোজ্য প্রতিক্রিয়া ছাড়া অনুরোধের জন্য, সেই সূচকে প্রতিক্রিয়া খালি হবে।

আপনি শুরু করার আগে

এই পৃষ্ঠার কাজগুলির সাথে এগিয়ে যাওয়ার আগে নিম্নলিখিত কাজগুলি সম্পাদন করুন:

  • প্রারম্ভিক অ্যাডপ্টার প্রোগ্রাম নির্দেশাবলীতে সম্পূর্ণ অনুমোদন/প্রমাণিকরণ এবং শংসাপত্র সেটআপ করুন

মেটাডেটা, সেটিংস বা আইটেম আপডেট করুন

নিম্নলিখিত উদাহরণটি দেখায় কিভাবে একটি ফর্মের মেটাডেটা আপডেট করতে হয়, কিন্তু কাঠামোটি বিষয়বস্তু এবং সেটিংসের জন্য একই- তারা updateFormInfo এর পরিবর্তে updateItem বা updateSettings অনুরোধগুলি ব্যবহার করে। প্রতিটি অনুরোধের জন্য, আপনি আপনার নির্দিষ্ট করা ক্ষেত্রগুলিতে পরিবর্তন সীমিত করতে একটি updateMask মান সহ পরিবর্তন করার জন্য ফিল্ডের নাম এবং আপডেট করা মান সরবরাহ করেন।

বিশ্রাম

ফর্মের বিবরণ আপডেট করতে, ফর্ম আইডি এবং আপডেট করা বর্ণনা মান সহ batchUpdate() পদ্ধতিতে কল করুন।

নমুনা অনুরোধ শরীর

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

পাইথন

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 সহ একটি অবস্থান ফর্মের শুরুতে বিষয়বস্তু সন্নিবেশ করবে।

বিশ্রাম

ফর্মটিতে একটি আইটেম যুক্ত করতে, ফর্ম আইডি এবং আইটেমের তথ্য এবং পছন্দসই অবস্থান সহ 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
        }
}]

পাইথন

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 অনুরোধে দুটি createItem সাব-রিকোয়েস্ট সহ একটি requests অ্যারে রয়েছে। সাব-রিকোয়েস্ট A-এ location.index 0 আছে এবং সাব-রিকোয়েস্ট B-এ location.index 1 আছে। requests অ্যারে [A, B] হলে, batchUpdate সফল হবে। যদি অ্যারেটি [B, A] হয়, batchUpdate ব্যর্থ হবে, যেহেতু location.index 1 বৈধ নয় যদি না ফর্মটিতে ইতিমধ্যেই সূচক 0-এ একটি আইটেম থাকে৷