Action অবজেক্ট আপনাকে Google Workspace অ্যাড-অনগুলিতে ইন্টারেক্টিভ আচরণ তৈরি করতে দেয়। কোনও ব্যবহারকারী অ্যাড-অন UI-তে কোনও উইজেটের (যেমন, একটি বোতাম) সাথে ইন্টারঅ্যাক্ট করলে কী ঘটে তা এগুলি নির্ধারণ করে।
একটি অ্যাকশন একটি নির্দিষ্ট উইজেটের সাথে একটি উইজেট হ্যান্ডলার ফাংশন ব্যবহার করে সংযুক্ত করা হয়, যা অ্যাকশনটিকে ট্রিগার করার শর্তটিও সংজ্ঞায়িত করে। ট্রিগার করা হলে, অ্যাকশনটি একটি নির্দিষ্ট কলব্যাক ফাংশন কার্যকর করে। কলব্যাক ফাংশনটি একটি ইভেন্ট অবজেক্ট পাস করে যা ব্যবহারকারীর ক্লায়েন্ট-সাইড ইন্টারঅ্যাকশন সম্পর্কে তথ্য বহন করে। আপনাকে কলব্যাক ফাংশনটি বাস্তবায়ন করতে হবে এবং এটিকে একটি নির্দিষ্ট প্রতিক্রিয়া অবজেক্ট ফেরত দিতে হবে।
উদাহরণস্বরূপ, ধরুন আপনি এমন একটি বোতাম চান যা ক্লিক করলে একটি নতুন কার্ড তৈরি করে এবং প্রদর্শন করে। এর জন্য, আপনাকে একটি নতুন বোতাম উইজেট তৈরি করতে হবে এবং কার্ড-বিল্ডিং Action সেট করতে বোতাম উইজেট হ্যান্ডলার ফাংশন setOnClickAction(action) ব্যবহার করতে হবে। আপনার সংজ্ঞায়িত Action একটি অ্যাপস স্ক্রিপ্ট কলব্যাক ফাংশন নির্দিষ্ট করে যা বোতামটি ক্লিক করার সময় কার্যকর হয়। এই ক্ষেত্রে, আপনি আপনার পছন্দসই কার্ড তৈরি করতে কলব্যাক ফাংশনটি বাস্তবায়ন করেন এবং একটি ActionResponse অবজেক্ট ফেরত দেন। রেসপন্স অবজেক্ট অ্যাড-অনকে কলব্যাক ফাংশনটি তৈরি করা কার্ডটি প্রদর্শন করতে বলে।
এই পৃষ্ঠাটি ড্রাইভ-নির্দিষ্ট উইজেট অ্যাকশনগুলি বর্ণনা করে যা আপনি আপনার অ্যাড-অনে অন্তর্ভুক্ত করতে পারেন।
ড্রাইভ ইন্টারঅ্যাকশন
Google Workspace অ্যাড-অনগুলি যা Drive প্রসারিত করে, সেগুলিতে একটি অতিরিক্ত Drive-নির্দিষ্ট উইজেট অ্যাকশন অন্তর্ভুক্ত থাকতে পারে। এই অ্যাকশনের জন্য একটি বিশেষ প্রতিক্রিয়া অবজেক্ট ফেরত দেওয়ার জন্য সংশ্লিষ্ট অ্যাকশন কলব্যাক ফাংশনের প্রয়োজন:
| পদক্ষেপ নেওয়ার চেষ্টা করা হয়েছে | কলব্যাক ফাংশনটি ফিরে আসা উচিত |
|---|---|
| নির্বাচিত ফাইলগুলির জন্য ফাইল অ্যাক্সেসের অনুরোধ করুন | DriveItemsSelectedActionResponse |
এই উইজেট অ্যাকশন এবং রেসপন্স অবজেক্টগুলি ব্যবহার করার জন্য, নিম্নলিখিত সমস্তগুলি সত্য হতে হবে:
- ব্যবহারকারীর এক বা একাধিক ড্রাইভ আইটেম নির্বাচন করা হলে অ্যাকশনটি ট্রিগার করা হয়।
- অ্যাড-অনটির ম্যানিফেস্টে
https://www.googleapis.com/auth/drive.fileড্রাইভ স্কোপ অন্তর্ভুক্ত রয়েছে।
নির্বাচিত ফাইলগুলির জন্য ফাইল অ্যাক্সেসের অনুরোধ করুন
নিম্নলিখিত উদাহরণে দেখানো হয়েছে কিভাবে Google Drive-এর জন্য একটি প্রাসঙ্গিক ইন্টারফেস তৈরি করতে হয় যা ব্যবহারকারী যখন এক বা একাধিক Drive আইটেম নির্বাচন করে তখন ট্রিগার হয়। উদাহরণটি প্রতিটি আইটেম পরীক্ষা করে দেখে যে অ্যাড-অনটিকে অ্যাক্সেসের অনুমতি দেওয়া হয়েছে কিনা; যদি না হয়, তাহলে এটি ব্যবহারকারীর কাছ থেকে সেই অনুমতির জন্য একটি 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;
}
}