吸引用户采取行动

Action 对象可让您构建交互式 Google Workspace 插件的行为。它们定义了 当用户在 Google Play 中 插件界面

使用 widget 处理程序函数, 这也定义了触发操作的条件。触发后, 执行指定的 回调函数。 系统会向该回调函数传递 事件对象,它包含 有关用户客户端互动情况的信息您必须实现 回调函数并使其返回特定的响应对象。

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

本页面介绍了您可以在 Google 云端硬盘 插件。

促进互动

可扩展云端硬盘的 Google Workspace 插件可能包括 一项额外的云端硬盘专用 widget 操作。此操作需要 操作回调函数 返回专用响应对象:

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

要使用这些 widget 操作和响应对象,必须设置以下 必须为 true:

  • 当用户选择了一项或多项云端硬盘内容时,就会触发操作。
  • 该插件包含 https://www.googleapis.com/auth/drive.file 云端硬盘范围 清单。

请求对所选文件的文件访问权限

以下示例展示了如何为 Google Cloud 用户选择一项或多项云端硬盘内容时触发的云端硬盘。通过 示例测试每个项目,查看插件是否已获得访问权限; 否则,它使用 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 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;
  }
}