Eylem hedefleme

Action nesneleri Google Workspace Eklentileri'nde etkileşimli davranışlar geliştirmenizi sağlar. Bunlar, bir kullanıcı eklenti kullanıcı arayüzündeki bir widget'la (örneğin, bir düğme) etkileşimde bulunduğunda ne olacağını tanımlar.

İşlem, işlemi tetikleyen koşulu da tanımlayan bir widget işleyici işlevi kullanılarak belirli bir widget'a eklenir. Tetiklendiğinde, özel bir geri çağırma işlevi yürütür. Geri çağırma işlevi, kullanıcının istemci tarafı etkileşimleri hakkında bilgi taşıyan bir etkinlik nesnesi geçirilir. Geri çağırma işlevini uygulamanız ve belirli bir yanıt nesnesi döndürmesini sağlamanız gerekir.

Örneğin, tıklandığında yeni bir kart oluşturan ve görüntüleyen bir düğme istediğinizi varsayalım. Bunun için yeni bir düğme widget'ı oluşturmanız ve bir kart oluşturma Action ayarlamak için düğme widget işleyici işlevini setOnClickAction(action) kullanmanız gerekir. Tanımladığınız Action, düğme tıklandığında yürütülecek bir Apps Komut Dosyası geri çağırma işlevini belirtir. Bu durumda, istediğiniz kartı derlemek ve bir ActionResponse nesnesi döndürmek için geri çağırma işlevini uygularsınız. Yanıt nesnesi, eklentiye geri çağırma işlevinin oluşturduğu kartı görüntülemesini bildirir.

Bu sayfada, eklentinize ekleyebileceğiniz Drive'a özel widget işlemleri açıklanmaktadır.

Etkileşimleri artırma

Drive'ı genişleten Google Workspace Eklentileri, Drive'a özel ek bir widget işlemi içerebilir. Bu işlem, özel bir yanıt nesnesi döndürmesi için ilişkili işlem geri çağırma işlevini gerektirir:

İşlem yapılmaya çalışıldı Geri çağırma işlevi şu sonucu döndürmelidir:
Seçili dosyalar için dosya erişimi isteme DriveItemsSelectedActionResponse

Bu widget eylemlerinden ve yanıt nesnelerinden yararlanmak için aşağıdakilerin tümünün doğru olması gerekir:

  • İşlem, kullanıcı bir veya daha fazla Drive öğesi seçiliyken tetiklenir.
  • Eklenti, manifest dosyasında https://www.googleapis.com/auth/drive.file Drive kapsamını içerir.

Seçili dosyalar için dosya erişimi iste

Aşağıdaki örnekte, kullanıcı bir veya daha fazla Drive öğesi seçtiğinde tetiklenen Google Drive için bağlamsal bir arayüzün nasıl oluşturulacağı gösterilmektedir. Örnekte, eklentiye erişim izni verilip verilmediğini görmek için her bir öğe test edilir. Erişim izni yoksa, kullanıcıdan bu izni istemek için bir DriveItemsSelectedActionResponse nesnesi kullanılır. Bir öğe için izin verildiğinde, eklenti, söz konusu öğenin Drive kota kullanımını görüntüler.

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