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

इंटरैक्टिव बनाने के लिए, Action ऑब्जेक्ट का इस्तेमाल करना के लिए इस्तेमाल किया जा सकता है.

ऐक्शन ऑब्जेक्ट से पता चलता है कि जब कोई उपयोगकर्ता किसी विजेट से इंटरैक्ट करता है, तो क्या होता है (उदाहरण के लिए, एक बटन).

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

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

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

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

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

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

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

कार्रवाई की कोशिश की गई कॉलबैक फ़ंक्शन वापस आना चाहिए
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();
}