إجراءات المحرِّر

استخدام كائنات الإجراء لإنشاء محتوى تفاعلي السلوك في إضافات Google Workspace.

تحدِّد كائنات الإجراءات ما يحدث عندما يتفاعل المستخدم مع أداة (على سبيل المثال، زر) في واجهة مستخدم الإضافة.

إضافة إجراء إلى تطبيق مصغّر

لإرفاق إجراء بأداة ما، استخدِم دالة معالج الأدوات، والتي تحدد أيضًا الشرط الذي يؤدي إلى الإجراء. عند تشغيله، يؤدي الإجراء إلى تنفيذ دالة استدعاء معيّنة. يتم تمرير دالة الاستدعاء كائن حدث يتضمن معلومات حول تفاعلات المستخدم من جانب العميل. يجب تنفيذ دالة الاستدعاء وجعلها تُرجع كائن استجابة محددًا.

مثال: عرض بطاقة جديدة عند النقر على زر

إذا أردت إضافة زر إلى إضافتك لإنشاء بطاقة جديدة وعرضها عند النقر عليه، اتبع الخطوات أدناه:

  1. أنشِئ أداة للزر.
  2. لضبط إجراء إنشاء بطاقة، أضِف وظيفة معالج التطبيقات المصغّرة للأزرار. setOnClickAction(action)
  3. أنشئ دالة استدعاء لبرمجة التطبيقات لتنفيذها وتحديدها على أنها (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 documents ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

تفاعلات الوصول إلى الملفات لواجهات برمجة تطبيقات REST

يمكن أن تتضمن إضافات Google Workspace التي توسّع نطاق "المحرّرين" وتستخدم واجهات برمجة تطبيقات REST تطبيق مصغّر إضافي لطلب الوصول إلى الملف. يتطلب هذا الإجراء دالة استدعاء إجراء مرتبطة لإرجاع كائن استجابة متخصص:

تمت محاولة تنفيذ الإجراء يجب أن تعرض دالة معاودة الاتصال
طلب الوصول إلى ملف current_document EditorFileScopeActionResponse

للاستفادة من الإجراء المصغّر وكائن الاستجابة، عليك تنفيذ ما يلي: أن تكون true:

  • تستخدم الإضافة واجهات برمجة تطبيقات REST.
  • تعرض الإضافة مربّع حوار نطاق ملف الطلب. باستخدام الطريقة CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();.
  • تتضمن الإضافة نطاق المحرِّر https://www.googleapis.com/auth/drive.file المُشغِّل onFileScopeGranted في ملف البيان الخاص به.

طلب الوصول إلى الملف للمستند الحالي

لطلب الوصول إلى الملف في المستند الحالي، اتبع الخطوات التالية:

  1. أنشِئ بطاقة صفحة رئيسية تتحقّق مما إذا كانت الإضافة تتضمّن نطاق drive.file.
  2. بالنسبة إلى الحالات التي لم يتم فيها منح الإضافة نطاق 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();
}