استخدِم عناصر الإجراء لإنشاء سلوك تفاعلي في إضافات Google Workspace.
تحدّد عناصر الإجراء ما يحدث عندما يتفاعل المستخدم مع أداة (مثل زر) في واجهة المستخدم الخاصة بالإضافة.
إضافة إجراء إلى تطبيق مصغّر
لربط إجراء بتطبيق مصغّر، استخدِم دالة معالجة التطبيق المصغّر، التي تحدّد أيضًا الشرط الذي يؤدّي إلى تنفيذ الإجراء. عندما يتم تشغيل الإجراء، ينفّذ دالّة ردّ اتصال معيّنة. يتم تمرير عنصر حدث إلى دالة معاودة الاتصال الذي يحمل معلومات حول تفاعلات المستخدم من جهة العميل. يجب تنفيذ دالة رد الاتصال وجعلها تعرض عنصر استجابة محدّدًا.
مثال: عرض بطاقة جديدة عند النقر على زر
إذا أردت إضافة زر إلى الإضافة ينشئ بطاقة جديدة ويعرضها عند النقر عليه، اتّبِع الخطوات التالية:
- أنشئ تطبيقًا مصغَّرًا للزر.
- لضبط إجراء إنشاء بطاقة، أضِف دالة معالج التطبيق المصغّر للزر
setOnClickAction(action)
. - أنشئ دالة ردّ اتصال في "برمجة تطبيقات 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 // allows the user to 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
يمكن أن تتضمّن إضافات Google Workspace التي توسّع نطاق "المحرّرين" وتستخدم واجهات REST API إجراءً إضافيًا خاصًا بأداة طلب الوصول إلى الملفات. يتطلّب هذا الإجراء أن تعرض دالة رد الاتصال المرتبطة بالإجراء كائن رد مخصّصًا:
الإجراء الذي تمت محاولة تنفيذه | يجب أن تعرض دالة رد الاتصال |
---|---|
طلب الوصول إلى ملف current_document | 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 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 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 allows the user to 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
// 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");
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();
}