कुछ करने के लिए प्रेरित करें

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

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

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

इस पेज में Drive से जुड़ी उन खास कार्रवाइयों की जानकारी दी गई है जिन्हें आप अपने फ़ोल्डर में शामिल कर सकते हैं ऐड-ऑन.

इंटरैक्शन बढ़ाएं

Drive का स्टोरेज बढ़ाने वाले Google Workspace ऐड-ऑन में ये शामिल हो सकते हैं Drive से जुड़े विजेट के लिए अतिरिक्त कार्रवाई. इस कार्रवाई के लिए असोसिएट की गई ऐक्शन कॉलबैक फ़ंक्शन कोई विशेष रिस्पॉन्स ऑब्जेक्ट लौटाने के लिए:

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

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

  • यह कार्रवाई तब ट्रिगर होती है, जब उपयोगकर्ता ने Drive में एक या उससे ज़्यादा आइटम चुने होते हैं.
  • ऐड-ऑन में https://www.googleapis.com/auth/drive.file अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है Drive का दायरा, मेनिफ़ेस्ट.

चुनी गई फ़ाइलों के लिए, फ़ाइल का ऐक्सेस मांगें

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

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