تشجيع العملاء على اتخاذ إجراءات

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

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

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

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

زيادة التفاعلات

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

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

للاستفادة من إجراءات التطبيقات المصغّرة وعناصر الاستجابة هذه، يجب أن يكون كل ما يلي صحيحًا:

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

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

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

/**
 * 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;
  }
}