Tác vụ trên Drive

Các đối tượng Action cho phép bạn tạo các lớp có tính tương tác hành vi vào Tiện ích bổ sung của Google Workspace. Chúng xác định điều gì xảy ra khi người dùng tương tác với một tiện ích (ví dụ: một nút) trong giao diện người dùng tiện ích bổ sung.

Một hành động được đính kèm vào một tiện ích cụ thể bằng một hàm trình xử lý tiện ích, mã này cũng xác định điều kiện kích hoạt hành động đó. Khi được kích hoạt, sẽ thực thi một hành động đã chỉ định. callback function (hàm callback). Hàm callback được truyền một đối tượng sự kiện mang thông tin về các hoạt động tương tác của người dùng ở phía máy khách. Bạn phải triển khai hàm callback và yêu cầu hàm này trả về một đối tượng phản hồi cụ thể.

Ví dụ: giả sử bạn muốn một nút tạo và hiển thị thẻ mới khi đã nhấp vào. Để thực hiện điều này, bạn phải tạo một tiện ích nút mới và sử dụng tiện ích nút hàm trình xử lý setOnClickAction(action) để thiết lập Action tạo thẻ. Chiến lược phát hành đĩa đơn Action mà bạn xác định sẽ chỉ định một Apps Script hàm callback thực thi khi người dùng nhấp vào nút này. Trong trường hợp này, bạn triển khai hàm callback để tạo thẻ bạn muốn và trả về một ActionResponse . Đối tượng phản hồi yêu cầu tiện ích bổ sung hiển thị thẻ lệnh gọi lại tạo.

Trang này mô tả các thao tác dành riêng cho tiện ích của Drive mà bạn có thể đưa vào tiện ích bổ sung.

Thúc đẩy tương tác

Các tiện ích bổ sung của Google Workspace giúp mở rộng Drive có thể bao gồm một thao tác bổ sung dành cho tiện ích dành riêng cho Drive. Để thực hiện thao tác này, bạn cần liên kết hành động hàm callback để trả về một đối tượng phản hồi chuyên biệt:

Đã cố gắng thực hiện thao tác Hàm gọi lại cần trả về
Yêu cầu quyền truy cập vào tệp đối với các tệp đã chọn DriveItemsSelectedActionResponse

Để sử dụng các thao tác và đối tượng phản hồi này trên tiện ích, hãy làm theo tất cả các bước sau phải đúng:

  • Hành động này được kích hoạt khi người dùng chọn một hoặc nhiều mục trên Drive.
  • Tiện ích bổ sung này bao gồm https://www.googleapis.com/auth/drive.file Phạm vi Drive trong tệp kê khai.

Yêu cầu quyền truy cập vào tệp cho các tệp đã chọn

Ví dụ sau đây trình bày cách tạo giao diện theo bối cảnh cho Google Drive được kích hoạt khi người dùng chọn một hoặc nhiều mục trên Drive. Chiến lược phát hành đĩa đơn ví dụ: kiểm thử từng mục để xem tiện ích bổ sung đã được cấp quyền truy cập chưa; nếu không, nó sẽ sử dụng DriveItemsSelectedActionResponse để yêu cầu người dùng cấp quyền đó. Sau khi được cấp quyền cho một mục, tiện ích bổ sung sẽ hiển thị hạn mức sử dụng Drive của mục đó.

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