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