吸引用户采取行动

借助 Action 对象,您可以将互动行为内置到 Google Workspace 插件中。它们定义了用户与插件界面中的微件(例如按钮)互动时发生的情况。

使用微件处理程序函数将操作附加到给定微件,该函数还定义了触发操作的条件。触发后,操作会执行指定的回调函数。系统会向回调函数传递一个对象对象,该对象包含用户的客户端互动的相关信息。您必须实现该回调函数,并使其返回特定的响应对象。

例如,假设您想要一个按钮,让该按钮在点击后显示新卡片。为此,您必须创建新的按钮微件,并使用按钮微件处理程序函数 setOnClickAction(action) 来设置卡片构建方式 Action。您定义的 Action 指定点击按钮时执行的 Apps 脚本回调函数。在这种情况下,您将实现该回调函数来构建所需的卡并返回 ActionResponse 对象。响应对象会告知插件显示回调函数构建的卡片。

本页介绍了您可以在插件中包含的云端硬盘专用微件操作。

吸引用户互动

用于扩展云端硬盘的 Google Workspace 插件可以包含针对云端硬盘的额外微件操作。此操作需要关联的操作回调函数来返回专门的响应对象:

已尝试操作 回调函数应返回
请求访问所选文件的文件 DriveItemsSelectedActionResponse

如需使用这些微件操作和响应对象,必须满足以下所有条件:

  • 系统会在用户选择一个或多个云端硬盘内容时触发操作。
  • 该插件的清单中包含 https://www.googleapis.com/auth/drive.file 云端硬盘范围

请求访问所选文件

以下示例展示了如何为 Google 云端硬盘构建上下文界面,当用户选择一项或多项云端硬盘内容时触发。该示例会测试每个项目,看看是否已向插件授予相应权限;如果未授予,则使用 DriveItemsSelectedActionResponse 对象向用户请求该权限。获得一项权限后,该插件会显示该项的云端硬盘配额用量。

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