एडिटर की कार्रवाइयां

Google Workspace ऐड-ऑन में इंटरैक्टिव व्यवहार बनाने के लिए, ऐक्शन ऑब्जेक्ट का इस्तेमाल करें.

ऐक्शन ऑब्जेक्ट से यह तय होता है कि जब कोई उपयोगकर्ता, ऐड-ऑन यूज़र इंटरफ़ेस (यूआई) में किसी विजेट (जैसे, एक बटन) के साथ इंटरैक्ट करता है, तब क्या होता है.

विजेट में कोई कार्रवाई जोड़ें

किसी विजेट में कोई कार्रवाई अटैच करने के लिए, विजेट हैंडलर फ़ंक्शन का इस्तेमाल करें जो कार्रवाई को ट्रिगर करने वाली स्थिति भी तय करता है. ट्रिगर होने पर, वह कार्रवाई तय किया गया कॉलबैक फ़ंक्शन लागू करती है. कॉलबैक फ़ंक्शन एक इवेंट ऑब्जेक्ट पास किया जाता है जो उपयोगकर्ता के क्लाइंट-साइड इंटरैक्शन के बारे में जानकारी देता है. आपको कॉलबैक फ़ंक्शन को लागू करना होगा और इससे कोई खास रिस्पॉन्स ऑब्जेक्ट दिखाना होगा.

उदाहरण: बटन पर क्लिक करने पर नया कार्ड दिखाना

अगर आपको अपने ऐड-ऑन में कोई ऐसा बटन जोड़ना है जो क्लिक करने पर नया कार्ड बनाता है और दिखाता है, तो नीचे दिया गया तरीका अपनाएं:

  1. बटन विजेट बनाएं.
  2. कार्ड बनाने की कार्रवाई सेट करने के लिए, बटन विजेट हैंडलर फ़ंक्शन setOnClickAction(action) जोड़ें.
  3. एक्ज़ीक्यूट करने के लिए, Apps Script कॉलबैक फ़ंक्शन बनाएं और इसे विजेट हैंडलर फ़ंक्शन में (action) के तौर पर बताएं. इस मामले में, कॉलबैक फ़ंक्शन को आपकी पसंद का कार्ड बनाना चाहिए और ActionResponse ऑब्जेक्ट दिखाना चाहिए. रिस्पॉन्स ऑब्जेक्ट, ऐड-ऑन को बनाए गए कॉलबैक फ़ंक्शन को दिखाने के लिए कहता है.

नीचे दिए गए उदाहरण में, बटन विजेट बनाने का तरीका बताया गया है. कार्रवाई, ऐड-ऑन की ओर से मौजूदा फ़ाइल के लिए drive.file दायरे का अनुरोध करती है.

/**
 * 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 document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

REST API के लिए, फ़ाइल ऐक्सेस करने से जुड़े इंटरैक्शन

REST API का इस्तेमाल करने वाले और एडिटर की अवधि बढ़ाने वाले Google Workspace ऐड-ऑन में, फ़ाइल के ऐक्सेस का अनुरोध करने के लिए विजेट के लिए एक और कार्रवाई शामिल की जा सकती है. इस कार्रवाई के लिए एक खास रिस्पॉन्स ऑब्जेक्ट दिखाने के लिए, जुड़े ऐक्शन कॉलबैक फ़ंक्शन की ज़रूरत होती है:

कार्रवाई की कोशिश की गई कॉलबैक फ़ंक्शन को वापस आना चाहिए
current_document के लिए फ़ाइल ऐक्सेस करने का अनुरोध करना EditorFileScopeActionResponse

इस विजेट ऐक्शन और रिस्पॉन्स ऑब्जेक्ट का इस्तेमाल करने के लिए, ये सभी चीज़ें सही होनी चाहिए:

  • ऐड-ऑन, REST API का इस्तेमाल करता है.
  • ऐड-ऑन, CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build(); तरीके का इस्तेमाल करके, अनुरोध फ़ाइल के दायरे वाला डायलॉग दिखाता है.
  • ऐड-ऑन के मेनिफ़ेस्ट में, https://www.googleapis.com/auth/drive.file एडिटर स्कोप और onFileScopeGranted ट्रिगर शामिल है.

मौजूदा दस्तावेज़ के लिए फ़ाइल ऐक्सेस करने का अनुरोध करें

अगर आपको मौजूदा दस्तावेज़ की फ़ाइल ऐक्सेस करने का अनुरोध करना है, तो यह तरीका अपनाएं:

  1. कोई होम पेज कार्ड बनाएं, जो यह जांचे कि ऐड-ऑन का स्कोप drive.file है या नहीं.
  2. जिन मामलों में ऐड-ऑन को drive.file का स्कोप नहीं दिया गया है, वहां उपयोगकर्ताओं से अनुरोध करने का तरीका बनाएं कि वे मौजूदा दस्तावेज़ के लिए drive.file का स्कोप दें.

उदाहरण: Google Docs में दस्तावेज़ का मौजूदा ऐक्सेस पाएं

नीचे दिए गए उदाहरण में, Google Docs के लिए एक इंटरफ़ेस बनाया गया है, जो मौजूदा दस्तावेज़ का साइज़ दिखाता है. अगर ऐड-ऑन को drive.file की अनुमति नहीं है, तो यह फ़ाइल के दायरे की अनुमति देने वाला बटन दिखाता है.

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