Tác vụ trên Drive

Đối tượng Action cho phép bạn xây dựng hành vi tương tác vào Tiện ích bổ sung của Google Workspace. Chúng xác định điều gì sẽ 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 của tiện ích bổ sung.

Một thao tác được đính kèm vào một tiện ích nhất định bằng cách sử dụng hàm trình xử lý tiện ích. Hàm này cũng xác định điều kiện kích hoạt thao tác đó. Khi được kích hoạt, hành động sẽ thực thi một hàm gọi lại được chỉ định. Hàm callback được truyền một đối tượng sự kiện chứa thông tin về hoạt động tương tác phía máy khách của người dùng. 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 được nhấp vào. Để làm được điều này, bạn phải tạo một tiện ích nút mới và sử dụng hàm xử lý tiện ích nút setOnClickAction(action) để đặt Action tạo thẻ. Action mà bạn đã xác định sẽ chỉ định hàm callback Apps Script sẽ 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 sẽ triển khai hàm callback để tạo thẻ bạn muốn và trả về một đối tượng ActionResponse. Đối tượng phản hồi yêu cầu tiện ích bổ sung hiển thị thẻ mà hàm gọi lại đã tạo.

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

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. Cần có hàm gọi lại cho thao tác liên kết để trả về một đối tượng phản hồi chuyên biệt:

Hành động đã cố gắng thực hiện Hàm callback sẽ 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 trong tiện ích và đối tượng phản hồi này, bạn phải đáp ứng tất cả những điều sau:

  • 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 phạm vi Drive https://www.googleapis.com/auth/drive.file trong tệp kê khai.

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

Ví dụ sau đây cho biết cách tạo giao diện theo ngữ cảnh cho Google Drive. Giao diện 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. Ví dụ này sẽ kiểm tra từng mục để xem tiện ích bổ sung đã được cấp quyền truy cập hay chưa; nếu chưa, tiện ích bổ sung sẽ sử dụng đối tượ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;
  }
}