Dorong tindakan

Objek Action memungkinkan Anda membuat aplikasi perilaku pengguna yang berbeda ke Add-on Google Workspace. {i>Sprint<i} menentukan apa yang terjadi ketika pengguna berinteraksi dengan widget (misalnya, tombol) di UI add-on.

Suatu tindakan dilampirkan ke widget tertentu menggunakan fungsi pengendali widget, yang juga menentukan kondisi yang memicu tindakan. Saat dipicu, tindakan menjalankan tindakan fungsi callback. Fungsi callback diberi objek peristiwa yang membawa informasi tentang interaksi {i>client-side<i} pengguna. Anda harus menerapkan fungsi callback dan membuatnya menampilkan objek respons tertentu.

Misalnya, Anda menginginkan tombol yang membuat dan menampilkan kartu baru saat diklik. Untuk melakukannya, Anda harus membuat widget tombol baru dan menggunakan widget tombol tersebut fungsi pengendali setOnClickAction(action) untuk menetapkan Action pembuatan kartu. Tujuan Action yang Anda tentukan menentukan Apps Script fungsi callback yang dieksekusi saat tombol diklik. Dalam hal ini, Anda mengimplementasikan fungsi callback untuk membangun kartu yang Anda inginkan dan menampilkan ActionResponse . Objek respons memberi tahu add-on untuk menampilkan kartu yang akan menampilkan callback fungsi build.

Halaman ini menjelaskan tindakan widget khusus Drive yang dapat Anda sertakan dalam {i>add-on<i}.

Mendorong interaksi

Add-on Google Workspace yang memperluas Drive dapat mencakup tindakan widget khusus Drive tambahan. Tindakan ini memerlukan tindakan tindakan callback function untuk menampilkan objek respons khusus:

Tindakan dicoba Fungsi callback akan ditampilkan
Meminta akses file untuk file yang dipilih DriveItemsSelectedActionResponse

Untuk memanfaatkan tindakan widget dan objek respons ini, semua hal harus benar:

  • Tindakan akan dipicu saat pengguna memilih satu atau beberapa item Drive.
  • Add-on ini menyertakan https://www.googleapis.com/auth/drive.file Cakupan Drive dalam manifes.

Minta akses file untuk file yang dipilih

Contoh berikut menunjukkan cara membuat antarmuka kontekstual untuk Google Drive yang dipicu saat pengguna memilih satu atau beberapa item Drive. Tujuan contoh menguji setiap item untuk melihat apakah add-on telah diberi izin akses; jika tidak, metode ini akan menggunakan DriveItemsSelectedActionResponse untuk meminta izin itu dari pengguna. Setelah izin akses diberikan untuk item, add-on akan menampilkan penggunaan kuota Drive untuk item tersebut.

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