ড্রাইভ কর্ম

Action অবজেক্ট আপনাকে Google Workspace অ্যাড-অনগুলিতে ইন্টারেক্টিভ আচরণ তৈরি করতে দেয়। অ্যাড-অন UI-তে কোনও ব্যবহারকারী উইজেটের (উদাহরণস্বরূপ, একটি বোতাম) সাথে ইন্টারঅ্যাক্ট করলে কী ঘটে তা তারা সংজ্ঞায়িত করে।

একটি উইজেট হ্যান্ডলার ফাংশন ব্যবহার করে একটি প্রদত্ত উইজেটের সাথে একটি অ্যাকশন সংযুক্ত করা হয়, যা ক্রিয়াটিকে ট্রিগার করে এমন অবস্থাকেও সংজ্ঞায়িত করে। ট্রিগার করা হলে, কর্মটি একটি মনোনীত কলব্যাক ফাংশন চালায়। কলব্যাক ফাংশনটি একটি ইভেন্ট অবজেক্ট পাস করা হয় যা ব্যবহারকারীর ক্লায়েন্ট-সাইড মিথস্ক্রিয়া সম্পর্কে তথ্য বহন করে। আপনাকে অবশ্যই কলব্যাক ফাংশন বাস্তবায়ন করতে হবে এবং এটিকে একটি নির্দিষ্ট প্রতিক্রিয়া বস্তু ফিরিয়ে দিতে হবে।

উদাহরণস্বরূপ, বলুন আপনি একটি বোতাম চান যা ক্লিক করার সময় একটি নতুন কার্ড তৈরি করে এবং প্রদর্শন করে। এর জন্য, আপনাকে অবশ্যই একটি নতুন বোতাম উইজেট তৈরি করতে হবে এবং একটি কার্ড-বিল্ডিং Action সেট করতে বোতাম উইজেট হ্যান্ডলার ফাংশন setOnClickAction(action) ব্যবহার করতে হবে। আপনি যে Action সংজ্ঞায়িত করেছেন তা একটি Apps স্ক্রিপ্ট কলব্যাক ফাংশন নির্দিষ্ট করে যা বোতামটি ক্লিক করার সময় কার্যকর করে৷ এই ক্ষেত্রে, আপনি যে কার্ডটি চান তা তৈরি করতে এবং একটি ActionResponse অবজেক্ট ফেরত দিতে আপনি কলব্যাক ফাংশনটি বাস্তবায়ন করেন। রেসপন্স অবজেক্ট অ্যাড-অনকে বলে যে কার্ডটি কলব্যাক ফাংশন তৈরি করে প্রদর্শন করতে।

এই পৃষ্ঠাটি ড্রাইভ-নির্দিষ্ট উইজেট অ্যাকশনগুলি বর্ণনা করে যা আপনি আপনার অ্যাড-অনে অন্তর্ভুক্ত করতে পারেন।

ড্রাইভ মিথস্ক্রিয়া

Google Workspace অ্যাড-অনগুলি যেগুলি ড্রাইভকে প্রসারিত করে তাতে একটি অতিরিক্ত ড্রাইভ-নির্দিষ্ট উইজেট অ্যাকশন অন্তর্ভুক্ত থাকতে পারে। এই ক্রিয়াটির জন্য একটি বিশেষ প্রতিক্রিয়া বস্তু ফেরত দেওয়ার জন্য সংশ্লিষ্ট কর্ম কলব্যাক ফাংশন প্রয়োজন:

অ্যাকশনের চেষ্টা করা হয়েছে কলব্যাক ফাংশন ফিরে আসা উচিত
নির্বাচিত ফাইলগুলির জন্য ফাইল অ্যাক্সেসের অনুরোধ করুন DriveItemsSelectedActionResponse

এই উইজেট ক্রিয়া এবং প্রতিক্রিয়া বস্তুগুলি ব্যবহার করার জন্য, নিম্নলিখিতগুলি অবশ্যই সত্য হতে হবে:

  • ব্যবহারকারীর এক বা একাধিক ড্রাইভ আইটেম নির্বাচন করার সময় ক্রিয়াটি ট্রিগার হয়৷
  • অ্যাড-অনটি এর ম্যানিফেস্টে https://www.googleapis.com/auth/drive.file ড্রাইভ স্কোপ অন্তর্ভুক্ত করে।

নির্বাচিত ফাইলগুলির জন্য ফাইল অ্যাক্সেসের অনুরোধ করুন

নিম্নলিখিত উদাহরণটি দেখায় কিভাবে Google ড্রাইভের জন্য একটি প্রাসঙ্গিক ইন্টারফেস তৈরি করতে হয় যা ব্যবহারকারী যখন এক বা একাধিক ড্রাইভ আইটেম নির্বাচন করে তখন ট্রিগার হয়৷ অ্যাড-অন অ্যাক্সেসের অনুমতি দেওয়া হয়েছে কিনা তা দেখার জন্য উদাহরণ প্রতিটি আইটেম পরীক্ষা করে; যদি না হয়, এটি ব্যবহারকারীর কাছ থেকে অনুমতির অনুরোধ করতে একটি DriveItemsSelectedActionResponse অবজেক্ট ব্যবহার করে। একবার একটি আইটেমের জন্য অনুমতি দেওয়া হলে, অ্যাড-অন সেই আইটেমের ড্রাইভ কোটা ব্যবহার প্রদর্শন করে।

/**
 * Build a simple card that checks selected items' quota usage. Checking
 * quota usage requires user-permissions, so this add-on provides a button
 * to request `drive.file` scope for items the add-on doesn't yet have
 * permission to access.
 *
 * @param e The event object passed containing contextual information about
 *    the Drive items selected.
 * @return {Card}
 */
function onDriveItemsSelected(e) {
  var builder =  CardService.newCardBuilder();

  // For each item the user has selected in Drive, display either its
  // quota information or a button that allows the user to provide
  // permission to access that file to retrieve its quota details.
  e['drive']['selectedItems'].forEach(
    function(item){
      var cardSection = CardService.newCardSection()
          .setHeader(item['title']);

      // This add-on uses the recommended, limited-permission `drive.file`
      // scope to get granular per-file access permissions.
      // See: https://developers.google.com/drive/api/v2/about-auth
      if (item['addonHasFileScopePermission']) {
        // If the add-on has access permission, read and display its
        // quota.
        cardSection.addWidget(
          CardService.newTextParagraph().setText(
              "This file takes up: " + getQuotaBytesUsed(item['id'])));
      } else {
        // If the add-on does not have access permission, add a button
        // that allows the user to provide that permission on a per-file
        // basis.
        cardSection.addWidget(
          CardService.newTextParagraph().setText(
              "The add-on needs permission to access this file's quota."));

        var buttonAction = CardService.newAction()
          .setFunctionName("onRequestFileScopeButtonClicked")
          .setParameters({id: item.id});

        var button = CardService.newTextButton()
          .setText("Request permission")
          .setOnClickAction(buttonAction);

        cardSection.addWidget(button);
      }

      builder.addSection(cardSection);
    });

  return builder.build();
}

/**
 * Callback function for a button action. Instructs Drive to display a
 * permissions dialog to the user, requesting `drive.file` scope for a
 * specific item on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the item's
 *   Drive ID.
 * @return {DriveItemsSelectedActionResponse}
 */
function onRequestFileScopeButtonClicked (e) {
  var idToRequest = e.parameters.id;
  return CardService.newDriveItemsSelectedActionResponseBuilder()
      .requestFileScope(idToRequest).build();
}

/**
 * Use the Advanced Drive Service
 * (See https://developers.google.com/apps-script/advanced/drive),
 * with `drive.file` scope permissions to request the quota usage of a
 * specific Drive item.
 *
 * @param {string} itemId The ID of the item to check.
 * @return {string} A description of the item's quota usage, in bytes.
 */
function getQuotaBytesUsed(itemId) {
  try {
    return Drive.Files.get(itemId,{fields: "quotaBytesUsed"})
        .quotaBytesUsed + " bytes";
  } catch (e) {
    return "Error fetching how much quota this item uses. Error: " + e;
  }
}