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

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

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

उदाहरण के लिए, मान लें कि आपको ऐसा बटन चाहिए जिस पर क्लिक करने पर नया कार्ड बने और दिखता हो. इसके लिए, आपको एक नया बटन विजेट बनाना होगा और कार्ड-बिल्डिंग Action सेट करने के लिए, बटन विजेट हैंडलर फ़ंक्शन setOnClickAction(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;
  }
}