Düzenleyici işlemleri

Etkileşimli derlemek için Action nesnelerini kullanın Google Workspace eklentilerine aktarmanızı sağlar.

İşlem nesneleri, kullanıcı bir widget ile etkileşimde bulunduğunda ne olacağını tanımlar (örneğin, bir düğme) girin.

Widget'a işlem ekleme

Bir widget'a işlem eklemek için widget işleyici işlevini kullanın. Bu değer, işlemi tetikleyen koşulu da tanımlar. Tetiklendiğinde, özel bir geri çağırma işlevi yürütür. Geri çağırma işlevine bir etkinlik nesnesi iletildi Kullanıcının istemci taraflı etkileşimleri hakkında bilgi taşıyan bir tarayıcıdır. Şunu yapmalısınız: geri çağırma işlevini uygulayın ve belirli bir yanıt nesnesini döndürmesini sağlayın.

Örnek: Bir düğme tıklandığında yeni bir kart gösterme

Eklentinize yeni bir kart oluşturan ve gösteren bir düğme eklemek istiyorsanız tıkladığınızda aşağıdaki adımları uygulayın:

  1. Widget düğmesi oluşturun.
  2. Kart oluşturma işlemi ayarlamak için düğme widget'ı işleyici işlevini ekleyin setOnClickAction(action).
  3. Yürütmek için bir Apps Komut Dosyası geri çağırma işlevi oluşturun ve bunu (action) widget'ı kullanabilirsiniz. Bu örnekte, istediğiniz kartı derlemeli ve bir ActionResponse nesnesi döndürmelidir. Yanıt nesnesi, eklentiye, geri çağırma işlevinin oluşturduğu kartı görüntülemesini söyler.

Aşağıdaki örnekte düğme widget'ının oluşturulması gösterilmektedir. İşlemin istekleri eklenti adına geçerli dosya için drive.file kapsamını belirleyin.

/**
 * Adds a section to the Card Builder that displays a "REQUEST PERMISSION" button.
 * When it's clicked, the callback triggers file scope permission flow. This is used in
 * the add-on when the home-page displays basic data.
 */
function addRequestFileScopeButtonToBuilder(cardBuilder) {
    var buttonSection = CardService.newCardSection();
    // 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.
    var buttonAction = CardService.newAction()
      .setFunctionName("onRequestFileScopeButtonClickedInEditor");

    var button = CardService.newTextButton()
      .setText("Request permission")
      .setBackgroundColor("#4285f4")
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
      .setOnClickAction(buttonAction);

    buttonSection.addWidget(button);
    cardBuilder.addSection(buttonSection);
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the 
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the documents ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

REST API'leri için dosya erişimi etkileşimleri

Düzenleyicilerin kapsamını genişleten ve REST API'leri kullanan Google Workspace Eklentileri ek widget işlemi kullanarak dosya erişimi isteyin. Bu işlem, özel bir yanıt nesnesi döndürmek için ilişkili işlem geri çağırma işlevi:

Yapılmak istenen işlem Geri çağırma işlevi şunu döndürmelidir:
current_document için dosya erişimi iste EditorFileScopeActionResponse

Bu widget işlemi ve yanıt nesnesinden yararlanmak için aşağıdakilerin tümü doğru olmalıdır:

  • Eklenti, REST API'leri kullanır.
  • Eklenti, istek dosyası kapsamı iletişim kutusunu sunar (CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build(); yöntemini kullanarak).
  • Eklenti https://www.googleapis.com/auth/drive.file düzenleyici kapsamı ve Manifest'inde onFileScopeGranted tetikleyicisi.

Geçerli doküman için dosya erişimi iste

Geçerli dokümana dosya erişimi istemek için aşağıdaki adımları uygulayın:

  1. Eklentinin drive.file kapsamına sahip olup olmadığını kontrol eden bir ana sayfa kartı oluşturun.
  2. Eklentiye drive.file kapsamı verilmemiş durumlarda kullanıcılardan geçerli dokümana drive.file kapsamı vermelerini istemenin bir yoludur.

Örnek: Google Dokümanlar'da geçerli dokümana erişim elde etme

Aşağıdaki örnekte Google Dokümanlar için bir arayüz oluşturur ve Google Dokümanlar'da yeni bir belge oluşturun. Eklentinin drive.file yetkilendirmesi yoksa Düğmede, dosya kapsamı yetkilendirmesini başlatan bir düğme gösterilir.

/**
 * 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 information about the
 *   current document.
 * @return {Card}
 */
function onDocsHomepage(e) {
  return createAddOnView(e);
}

function onFileScopeGranted(e) {
  return createAddOnView(e);
}

/**
 * For the current document, display either its quota information or
 * a button that allows the user to provide permission to access that
 * file to retrieve its quota details.
 *
 * @param e The event containing information about the current document
 * @return {Card}
 */
function createAddOnView(e) {
  var docsEventObject = e['docs'];
  var builder =  CardService.newCardBuilder();

  var cardSection = CardService.newCardSection();
  if (docsEventObject['addonHasFileScopePermission']) {
    cardSection.setHeader(docsEventObject['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 the add-on has access permission, read and display its quota.
    cardSection.addWidget(
      CardService.newTextParagraph().setText(
          "This file takes up: " + getQuotaBytesUsed(docsEventObject['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");

    var button = CardService.newTextButton()
      .setText("Request permission")
      .setOnClickAction(buttonAction);

    cardSection.addWidget(button);
  }
  return builder.addSection(cardSection).build();
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClicked(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();
}