إجراءات Google Drive

تتيح لك كائنات Action إنشاء سلوك تفاعلي في إضافات Google Workspace. وهي تحدّد ما يحدث عندما يتفاعل المستخدم مع أداة (مثل زر) في واجهة مستخدم الإضافة.

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

على سبيل المثال، لنفترض أنّك تريد زرًا ينشئ بطاقة جديدة ويعرضها عند النقر عليها. لتحقيق ذلك، عليك إنشاء أداة زر جديدة واستخدام دالة معالج أداة الزر setOnClickAction(action) لضبط Action لإنشاء البطاقة. يحدّد Action الذي تحدّده دالة رد الاتصال في برمجة تطبيقات يتم تنفيذها عند النقر على الزر. في هذه الحالة، يمكنك تنفيذ دالة رد الاتصال لإنشاء البطاقة التي تريدها وعرض ActionResponse كائن. يطلب كائن الاستجابة من الإضافة عرض البطاقة التي أنشأتها دالة رد الاتصال.

توضّح هذه الصفحة إجراءات الأدوات الخاصة بـ Google Drive التي يمكنك تضمينها في الإضافة.

تفاعلات Drive

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

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

لاستخدام إجراءات الأدوات وكائنات الاستجابة هذه، يجب استيفاء جميع الشروط التالية:

  • يتم تشغيل الإجراء أثناء اختيار المستخدم لملف واحد أو أكثر من ملفات Drive.
  • تتضمّن الإضافة نطاق Drive https://www.googleapis.com/auth/drive.file في بيانها.

طلب الوصول إلى الملفات المحدّدة

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

/**
 * Builds 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 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 lets the user 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 doesn't 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")
          .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;
  }
}