استخدِم عناصر Action لإنشاء سلوك تفاعلي في إضافات Google Workspace.
تحدّد عناصر الإجراء ما يحدث عندما يتفاعل المستخدم مع أداة (مثل زر) في واجهة المستخدم الخاصة بالإضافة.
إضافة إجراء إلى تطبيق مصغّر
لربط إجراء بتطبيق مصغّر، استخدِم دالة معالجة التطبيق المصغّر، التي تحدّد أيضًا الشرط الذي يؤدي إلى تنفيذ الإجراء. عندما يتم تشغيل الإجراء، ينفّذ دالة ردّ اتصال معيّنة. يتم تمرير عنصر حدث إلى دالّة رد الاتصال، ويحمل هذا العنصر معلومات حول تفاعلات المستخدم من جهة العميل. يجب تنفيذ دالّة رد الاتصال وإرجاع عنصر استجابة محدّد.
مثال: عرض بطاقة جديدة عند النقر على زر
إذا أردت إضافة زر إلى الإضافة ينشئ بطاقة جديدة ويعرضها عند النقر عليه، اتّبِع الخطوات التالية:
- أنشئ تطبيقًا مصغَّرًا على شكل زر.
- لضبط إجراء إنشاء بطاقة، أضِف دالة معالج أداة الزر
setOnClickAction. - أنشئ دالة ردّ اتصال في "برمجة تطبيقات Google" لتنفيذها وحدِّدها كـ
actionضمن دالة معالج التطبيق المصغّر. في هذه الحالة، يجب أن تنشئ دالة رد الاتصال البطاقة التي تريدها وتعرض كائنActionResponse. يطلب عنصر الرد من الإضافة عرض البطاقة التي أنشأتها دالة رد الاتصال.
يوضّح المثال التالي كيفية إنشاء أداة زر. يطلب الإجراء النطاق drive.file للملف الحالي نيابةً عن الإضافة.
/**
* Adds a section to the Card Builder that displays a "REQUEST PERMISSION"
* button. When it's clicked, the callback triggers file scope permission flow.
* This is used in the add-on when the home-page displays basic data.
*/
function addRequestFileScopeButtonToBuilder(cardBuilder) {
var buttonSection = CardService.newCardSection();
// If the add-on does not have access permission, add a button that
// lets the user provide that permission on a per-file basis.
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClickedInEditor");
var button = CardService.newTextButton()
.setText("Request permission")
.setBackgroundColor("#4285f4")
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setOnClickAction(buttonAction);
buttonSection.addWidget(button);
cardBuilder.addSection(buttonSection);
}
/**
* Callback function for a button action. Instructs Docs to display a
* permissions dialog to the user, requesting `drive.file` scope for the
* current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document's ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClickedInEditor(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
}
تفاعلات الوصول إلى الملفات لواجهات REST API
يمكن أن تتضمّن إضافات Google Workspace التي توسّع نطاق "أدوات التحرير" وتستخدم واجهات REST البرمجية إجراءً إضافيًا خاصًا بأداة طلب الوصول إلى الملفات. يتطلّب هذا الإجراء أن تعرض دالة رد الاتصال المرتبطة بالإجراء كائن رد مخصّصًا:
| الإجراء الذي تمت محاولة تنفيذه | يجب أن تعرض دالة رد الاتصال |
|---|---|
| طلب الوصول إلى الملف | EditorFileScopeActionResponse |
لاستخدام إجراء الأداة وعنصر الرد هذا، يجب استيفاء جميع الشروط التالية:
- تستخدم الإضافة واجهات REST API.
- تعرض الإضافة مربّع حوار نطاق ملف الطلب باستخدام الطريقة
CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();. - يتضمّن البيان الخاص بالإضافة النطاق
https://www.googleapis.com/auth/drive.fileالخاص بالمحرّر والمشغّلonFileScopeGranted.
طلب الوصول إلى الملف للمستند الحالي
لطلب إذن الوصول إلى الملف للمستند الحالي، اتّبِع الخطوات التالية:
- أنشئ بطاقة في الصفحة الرئيسية تتحقّق مما إذا كانت الإضافة تتضمّن نطاق
drive.file. - في الحالات التي لم يتم فيها منح النطاق
drive.fileللإضافة، يجب إنشاء طريقة لطلب أن يمنح المستخدمون النطاقdrive.fileللمستند الحالي.
مثال: الحصول على إذن الوصول إلى المستند الحالي في "مستندات Google"
ينشئ المثال التالي واجهة لمستندات Google تعرض حجم المستند الحالي. إذا لم تكن الإضافة
تتضمّن drive.file إذنًا، ستعرض زرًا لبدء عملية منح الإذن
بالوصول إلى نطاق الملف.
/**
* Build a 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 information about the
* current document.
* @return {Card}
*/
function onDocsHomepage(e) {
return createAddOnView(e);
}
function onFileScopeGranted(e) {
return createAddOnView(e);
}
/**
* For the current document, display either its quota information or
* a button that lets the user provide permission to access that
* file to retrieve its quota details.
*
* @param e The event containing information about the current document
* @return {Card}
*/
function createAddOnView(e) {
var docsEventObject = e['docs'];
var builder = CardService.newCardBuilder();
var cardSection = CardService.newCardSection();
if (docsEventObject['addonHasFileScopePermission']) {
cardSection.setHeader(docsEventObject['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 the add-on has access permission, read and display its quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"This file takes up: " +
getQuotaBytesUsed(docsEventObject['id'])));
} else {
// If the add-on does not have access permission, add a button that
// lets the user 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");
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
return builder.addSection(cardSection).build();
}
/**
* Callback function for a button action. Instructs Docs to
* display a permissions dialog to the user, requesting `drive.file` scope for
* the current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document's ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClicked(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
}